Refactoring

This commit is contained in:
Andreas 2024-06-28 08:16:28 +02:00
parent 665da4be29
commit 51c9c1f4f3
5 changed files with 23 additions and 28 deletions

View File

@ -14,7 +14,7 @@ import {
} from "@/app/lib/actions/entityManagement/postActions"; } from "@/app/lib/actions/entityManagement/postActions";
import { PostViewProps } from "@/views/admin/ClientPostView"; import { PostViewProps } from "@/views/admin/ClientPostView";
import { aifa } from "@/util/Utils"; import { aifa } from "@/util/Utils";
import { StateHook } from "../../../util/types/StateHook"; import { StateHook } from "@/util/state/stateUtils";
export type PostTableStateProps = { export type PostTableStateProps = {
posts:StateHook<GetPostsAttributes[]>, posts:StateHook<GetPostsAttributes[]>,

View File

@ -0,0 +1,15 @@
import { Dispatch, SetStateAction } from "react";
export type StateHook<T> = {
state: T;
setState: Dispatch<SetStateAction<T>>;
};
export type StateHookArray<T> = [
T,Dispatch<SetStateAction<T>>
]
export function parseStateHook<T>(hook:StateHookArray<T>):StateHook<T>{
const stateHook:Partial<StateHook<T>> = {};
[stateHook.state, stateHook.setState] = hook;
return stateHook as StateHook<T>
}

View File

@ -1,9 +0,0 @@
import { Dispatch, SetStateAction } from "react";
export type StateHook<T> = {
state: T;
setState: Dispatch<SetStateAction<T>>;
};
export type StateHookArray<T> = [
T,Dispatch<SetStateAction<T>>
]

View File

@ -3,7 +3,6 @@
import PostTable, { import PostTable, {
PostTableStateProps, PostTableStateProps,
} from "@/components/client/admin/PostTable"; } from "@/components/client/admin/PostTable";
import { PostTableProps } from "../../components/client/admin/PostTable";
import { import {
GetPostsAttributes, GetPostsAttributes,
PostServerActions, PostServerActions,
@ -12,7 +11,7 @@ import { EditorState } from "@/components/client/admin/PostEditor";
import { Project } from "@/models"; import { Project } from "@/models";
import { Dispatch, ReactNode, SetStateAction, useState } from "react"; import { Dispatch, ReactNode, SetStateAction, useState } from "react";
import { Attributes } from "@sequelize/core"; import { Attributes } from "@sequelize/core";
import { StateHookArray } from "@/util/types/StateHook"; import { parseStateHook, StateHookArray } from "@/util/state/stateUtils";
export type PostViewProps = { export type PostViewProps = {
children?: ReactNode; children?: ReactNode;
@ -23,7 +22,7 @@ export type PostViewProps = {
}; };
export function ClientPostView({ export function ClientPostView({
children, // children,
headings, headings,
posts, posts,
projects, projects,
@ -35,17 +34,9 @@ export function ClientPostView({
editorPost: posts[0], editorPost: posts[0],
}; };
// Set up required state hooks // Set up required state hooks
const [postsState, setPostsState]: StateHookArray<GetPostsAttributes[]> = useState(posts);
const [editorState, setEditorState]: StateHookArray<EditorState> = useState(initEditorState);
const state: PostTableStateProps = { const state: PostTableStateProps = {
posts: { posts: parseStateHook(useState(posts)),
state: postsState, editor: parseStateHook(useState(initEditorState)),
setState: setPostsState,
},
editor: {
state: editorState,
setState: setEditorState,
},
}; };
return ( return (

View File

@ -14,8 +14,6 @@ import { getProjects } from "@/app/lib/actions/entityManagement/projectActions";
import { Bucket, Project, Post, dbSync, Attachment } from "@/models"; import { Bucket, Project, Post, dbSync, Attachment } from "@/models";
import { handleActionResult } from "../../app/lib/actions/clientActionHandler"; import { handleActionResult } from "../../app/lib/actions/clientActionHandler";
import { ClientPostView } from "./ClientPostView"; import { ClientPostView } from "./ClientPostView";
import { readFileSync } from "fs";
import path from "path";
type Props = { type Props = {
children?: ReactNode; children?: ReactNode;
@ -41,11 +39,11 @@ export default async function PostView(props: Props) {
// uploadAttachment:tryCreateAttachment // uploadAttachment:tryCreateAttachment
}; };
const posts: GetPostsAttributes[] | undefined = handleActionResult( const posts: GetPostsAttributes[]| undefined = handleActionResult(
await getPostsWithBucketsAndProject() await getPostsWithBucketsAndProject()
); );
const projects = await Project.findAll().then((projects) => const projects = await Project.findAll().then((projects) =>
projects.map((e) => JSON.parse(JSON.stringify(e))) projects.map((project) => JSON.parse(JSON.stringify(project)))
); );
return ( return (
@ -56,7 +54,7 @@ export default async function PostView(props: Props) {
projects={projects} projects={projects}
headings={headings} headings={headings}
actions={actions} actions={actions}
></ClientPostView> />
</div> </div>
</div> </div>
); );