Refactoring and reworking dbsetup route
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
"use client";
|
||||
|
||||
import PostTable, {
|
||||
PostTableStateProps,
|
||||
} from "@/components/client/admin/PostTable";
|
||||
import { PostTableProps } from "../../components/client/admin/PostTable";
|
||||
import {
|
||||
GetPostsAttributes,
|
||||
PostServerActions,
|
||||
} from "@/app/lib/actions/entityManagement/postActions";
|
||||
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";
|
||||
|
||||
export type PostViewProps = {
|
||||
children?: ReactNode;
|
||||
headings: Array<string>;
|
||||
posts: GetPostsAttributes[];
|
||||
projects: Attributes<Project>[];
|
||||
actions: PostServerActions;
|
||||
};
|
||||
|
||||
export function ClientPostView({
|
||||
children,
|
||||
headings,
|
||||
posts,
|
||||
projects,
|
||||
actions,
|
||||
}: PostViewProps) {
|
||||
// Init editor state. Make sure it is not opened by default
|
||||
const initEditorState: EditorState = {
|
||||
isEditorOpen: false,
|
||||
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,
|
||||
},
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<PostTable
|
||||
headings={headings}
|
||||
posts={posts}
|
||||
projects={projects}
|
||||
actions={actions}
|
||||
state={state}
|
||||
></PostTable>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -2,25 +2,29 @@
|
||||
cache: "no-store";
|
||||
|
||||
import { ReactNode, useEffect } from "react";
|
||||
import PostTable, {
|
||||
PostTableServerActions,
|
||||
} from "@/components/client/admin/PostTable";
|
||||
import PostTable from "@/components/client/admin/PostTable";
|
||||
import {
|
||||
deletePost,
|
||||
getPostsWithBucketsAndProject,
|
||||
GetPostsAttributes,
|
||||
updatePost,
|
||||
GetPostsAttributes,
|
||||
PostServerActions,
|
||||
} from "@/app/lib/actions/entityManagement/postActions";
|
||||
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;
|
||||
};
|
||||
|
||||
async function getHeadings() {
|
||||
let headings: string[] = [
|
||||
export default async function PostView(props: Props) {
|
||||
const sync = await dbSync;
|
||||
|
||||
const headings:string[] = [
|
||||
"#",
|
||||
"Title",
|
||||
"Content",
|
||||
@@ -29,15 +33,7 @@ async function getHeadings() {
|
||||
"UpdatedAt",
|
||||
];
|
||||
|
||||
return headings;
|
||||
}
|
||||
|
||||
export default async function PostView(props: Props) {
|
||||
const sync = await dbSync;
|
||||
|
||||
const headings = await getHeadings();
|
||||
|
||||
const actions: PostTableServerActions = {
|
||||
const actions: PostServerActions = {
|
||||
deletePost: deletePost,
|
||||
getPosts: getPostsWithBucketsAndProject,
|
||||
getProjects: getProjects,
|
||||
@@ -55,12 +51,12 @@ export default async function PostView(props: Props) {
|
||||
return (
|
||||
<div className="w-[100%] min-h-fit bg-gray-100 overflow-scroll">
|
||||
<div className="w-[100%] m-auto">
|
||||
<PostTable
|
||||
<ClientPostView
|
||||
posts={posts ? posts : []}
|
||||
projects={projects}
|
||||
headings={headings}
|
||||
actions={actions}
|
||||
></PostTable>
|
||||
></ClientPostView>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -1,64 +1,70 @@
|
||||
cache: 'no-store'
|
||||
cache: "no-store";
|
||||
|
||||
import { tryFetchPosts } from "@/app/api/post/route";
|
||||
import { constructAPIUrl } from "@/util/Utils";
|
||||
import { ReactNode, useEffect } from "react";
|
||||
import EntityManagementTable from "../../components/client/EntityManagementTable";
|
||||
import PostTable from "@/components/client/admin/PostTable";
|
||||
import { deletePost, getPostsWithBucketsAndProject, updatePost } from "@/app/lib/actions/entityManagement/postActions";
|
||||
import {
|
||||
deletePost,
|
||||
getPostsWithBucketsAndProject,
|
||||
updatePost,
|
||||
} from "@/app/lib/actions/entityManagement/postActions";
|
||||
import { getProjects } from "@/app/lib/actions/entityManagement/projectActions";
|
||||
import { Bucket, Project, Post, dbSync, Attachment } from "@/models";
|
||||
import { tryCreateAttachment } from "@/app/api/attachment/route";
|
||||
import { Attributes } from '@sequelize/core';
|
||||
import * as util from 'util'
|
||||
import { Attributes } from "@sequelize/core";
|
||||
import * as util from "util";
|
||||
|
||||
type Props = {
|
||||
children?:ReactNode
|
||||
children?: ReactNode;
|
||||
};
|
||||
|
||||
async function getHeadings() {
|
||||
let headings: string[] = [];
|
||||
for (const key in Project.getAttributes()) {
|
||||
headings.push(key);
|
||||
}
|
||||
return headings;
|
||||
}
|
||||
|
||||
async function getHeadings(){
|
||||
let headings:string[] = []
|
||||
for (const key in Project.getAttributes())
|
||||
{
|
||||
headings.push(key)
|
||||
}
|
||||
return headings
|
||||
}
|
||||
export default async function ProjectView(props: Props) {
|
||||
const sync = await dbSync;
|
||||
|
||||
export default async function ProjectView(props:Props){
|
||||
const sync = await dbSync;
|
||||
|
||||
// const headings = [
|
||||
// '#',
|
||||
// 'Title',
|
||||
// 'Content',
|
||||
// 'Project',
|
||||
// 'Date Created',
|
||||
// 'Date Modified',
|
||||
// 'Edit',
|
||||
// 'Delete',
|
||||
// ]
|
||||
// const actions = {
|
||||
// deletePost: deletePost,
|
||||
// getPosts:getPostsWithBuckets,
|
||||
// getProjects:getProjects,
|
||||
// savePost:updatePost,
|
||||
// // uploadAttachment:tryCreateAttachment
|
||||
// };
|
||||
|
||||
const posts:Post[] = await Post.findAll({include: {model: Bucket, include: {model: Attachment}}}).then(posts=>posts.map((e)=>JSON.parse(JSON.stringify(e))));
|
||||
const projects = await Project.findAll().then(projects=>projects.map((e)=>JSON.parse(JSON.stringify(e))));
|
||||
const headings = await getHeadings();
|
||||
return <>
|
||||
<div className="w-[100%] min-h-fit bg-gray-100 overflow-scroll">
|
||||
<div className="w-[100%] m-auto">
|
||||
<EntityManagementTable entityName="Project" headings={headings}><></></EntityManagementTable>
|
||||
{/* <PostTable data={posts}
|
||||
const actions = {
|
||||
deletePost: deletePost,
|
||||
getProjects:getProjects,
|
||||
savePost:updatePost,
|
||||
// uploadAttachment:tryCreateAttachment
|
||||
};
|
||||
const projects: Project[] = await Project.findAll().then((projects) =>
|
||||
projects.map((e) => JSON.parse(JSON.stringify(e)))
|
||||
);
|
||||
return (
|
||||
<>
|
||||
<div className="w-[100%] min-h-fit bg-gray-100 overflow-scroll">
|
||||
<div className="w-[100%] m-auto">
|
||||
<EntityManagementTable
|
||||
entityName="Project"
|
||||
headings={[
|
||||
'#',
|
||||
'Title',
|
||||
'Content',
|
||||
'Project',
|
||||
'Date Created',
|
||||
'Date Modified',
|
||||
'Edit',
|
||||
'Delete',
|
||||
]}>
|
||||
{""}
|
||||
</EntityManagementTable>
|
||||
{/* <PostTable data={posts}
|
||||
projects={projects}
|
||||
headings={headings}
|
||||
actions={actions}>
|
||||
</PostTable> */}
|
||||
</div>
|
||||
</div>
|
||||
</>;
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user