From 51c9c1f4f35a54f741717563775edb7866997871 Mon Sep 17 00:00:00 2001 From: Andreas Date: Fri, 28 Jun 2024 08:16:28 +0200 Subject: [PATCH] Refactoring --- src/components/client/admin/PostTable.tsx | 2 +- src/util/state/stateUtils.ts | 15 +++++++++++++++ src/util/types/StateHook.tsx | 9 --------- src/views/admin/ClientPostView.tsx | 17 ++++------------- src/views/admin/PostView.tsx | 8 +++----- 5 files changed, 23 insertions(+), 28 deletions(-) create mode 100644 src/util/state/stateUtils.ts delete mode 100644 src/util/types/StateHook.tsx diff --git a/src/components/client/admin/PostTable.tsx b/src/components/client/admin/PostTable.tsx index e3f39e4..7d858c6 100644 --- a/src/components/client/admin/PostTable.tsx +++ b/src/components/client/admin/PostTable.tsx @@ -14,7 +14,7 @@ import { } from "@/app/lib/actions/entityManagement/postActions"; import { PostViewProps } from "@/views/admin/ClientPostView"; import { aifa } from "@/util/Utils"; -import { StateHook } from "../../../util/types/StateHook"; +import { StateHook } from "@/util/state/stateUtils"; export type PostTableStateProps = { posts:StateHook, diff --git a/src/util/state/stateUtils.ts b/src/util/state/stateUtils.ts new file mode 100644 index 0000000..f688fdf --- /dev/null +++ b/src/util/state/stateUtils.ts @@ -0,0 +1,15 @@ +import { Dispatch, SetStateAction } from "react"; + +export type StateHook = { + state: T; + setState: Dispatch>; +}; +export type StateHookArray = [ + T,Dispatch> +] + +export function parseStateHook(hook:StateHookArray):StateHook{ + const stateHook:Partial> = {}; + [stateHook.state, stateHook.setState] = hook; + return stateHook as StateHook +} \ No newline at end of file diff --git a/src/util/types/StateHook.tsx b/src/util/types/StateHook.tsx deleted file mode 100644 index c7350d8..0000000 --- a/src/util/types/StateHook.tsx +++ /dev/null @@ -1,9 +0,0 @@ -import { Dispatch, SetStateAction } from "react"; - -export type StateHook = { - state: T; - setState: Dispatch>; -}; -export type StateHookArray = [ - T,Dispatch> -] \ No newline at end of file diff --git a/src/views/admin/ClientPostView.tsx b/src/views/admin/ClientPostView.tsx index 8509695..42eb2f5 100644 --- a/src/views/admin/ClientPostView.tsx +++ b/src/views/admin/ClientPostView.tsx @@ -3,7 +3,6 @@ import PostTable, { PostTableStateProps, } from "@/components/client/admin/PostTable"; -import { PostTableProps } from "../../components/client/admin/PostTable"; import { GetPostsAttributes, PostServerActions, @@ -12,7 +11,7 @@ import { EditorState } from "@/components/client/admin/PostEditor"; import { Project } from "@/models"; import { Dispatch, ReactNode, SetStateAction, useState } from "react"; import { Attributes } from "@sequelize/core"; -import { StateHookArray } from "@/util/types/StateHook"; +import { parseStateHook, StateHookArray } from "@/util/state/stateUtils"; export type PostViewProps = { children?: ReactNode; @@ -23,7 +22,7 @@ export type PostViewProps = { }; export function ClientPostView({ - children, + // children, headings, posts, projects, @@ -35,17 +34,9 @@ export function ClientPostView({ editorPost: posts[0], }; // Set up required state hooks - const [postsState, setPostsState]: StateHookArray = useState(posts); - const [editorState, setEditorState]: StateHookArray = useState(initEditorState); const state: PostTableStateProps = { - posts: { - state: postsState, - setState: setPostsState, - }, - editor: { - state: editorState, - setState: setEditorState, - }, + posts: parseStateHook(useState(posts)), + editor: parseStateHook(useState(initEditorState)), }; return ( diff --git a/src/views/admin/PostView.tsx b/src/views/admin/PostView.tsx index 8df5241..080407e 100644 --- a/src/views/admin/PostView.tsx +++ b/src/views/admin/PostView.tsx @@ -14,8 +14,6 @@ import { getProjects } from "@/app/lib/actions/entityManagement/projectActions"; import { Bucket, Project, Post, dbSync, Attachment } from "@/models"; import { handleActionResult } from "../../app/lib/actions/clientActionHandler"; import { ClientPostView } from "./ClientPostView"; -import { readFileSync } from "fs"; -import path from "path"; type Props = { children?: ReactNode; @@ -41,11 +39,11 @@ export default async function PostView(props: Props) { // uploadAttachment:tryCreateAttachment }; - const posts: GetPostsAttributes[] | undefined = handleActionResult( + const posts: GetPostsAttributes[]| undefined = handleActionResult( await getPostsWithBucketsAndProject() ); const projects = await Project.findAll().then((projects) => - projects.map((e) => JSON.parse(JSON.stringify(e))) + projects.map((project) => JSON.parse(JSON.stringify(project))) ); return ( @@ -56,7 +54,7 @@ export default async function PostView(props: Props) { projects={projects} headings={headings} actions={actions} - > + /> );