Refactoring
This commit is contained in:
parent
665da4be29
commit
51c9c1f4f3
@ -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<GetPostsAttributes[]>,
|
||||
|
||||
15
src/util/state/stateUtils.ts
Normal file
15
src/util/state/stateUtils.ts
Normal 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>
|
||||
}
|
||||
@ -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>>
|
||||
]
|
||||
@ -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<GetPostsAttributes[]> = useState(posts);
|
||||
const [editorState, setEditorState]: StateHookArray<EditorState> = useState(initEditorState);
|
||||
const state: PostTableStateProps = {
|
||||
posts: {
|
||||
state: postsState,
|
||||
setState: setPostsState,
|
||||
},
|
||||
editor: {
|
||||
state: editorState,
|
||||
setState: setEditorState,
|
||||
},
|
||||
posts: parseStateHook(useState(posts)),
|
||||
editor: parseStateHook(useState(initEditorState)),
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@ -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}
|
||||
></ClientPostView>
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user