From d8d0aaa90e18244b1c3393753e7f6f269082db86 Mon Sep 17 00:00:00 2001 From: Andreas Date: Sun, 28 Jul 2024 23:42:32 +0200 Subject: [PATCH] push work --- .../entityManagement/post/projectActions.ts | 9 +- src/components/client/admin/PostTable.tsx | 276 +++++++++--------- src/components/views/admin/post/CPostView.tsx | 67 +++-- src/components/views/admin/post/PostView.tsx | 76 ++--- 4 files changed, 212 insertions(+), 216 deletions(-) diff --git a/src/app/lib/actions/entityManagement/post/projectActions.ts b/src/app/lib/actions/entityManagement/post/projectActions.ts index 01dff89..964e31b 100644 --- a/src/app/lib/actions/entityManagement/post/projectActions.ts +++ b/src/app/lib/actions/entityManagement/post/projectActions.ts @@ -1,11 +1,14 @@ -'use server'; +"use server"; import { Project } from "@/models"; import { ActionResult } from "../../ActionResult"; import { Attributes } from "@sequelize/core"; import { userIsAdmin } from "../../actions"; -export async function getProjects(): Promise[]>> { - if (! await userIsAdmin()) return { error: 'Unauthorized, not fetching Projects' } +export async function getProjects(): Promise< + ActionResult[]> +> { + if (!(await userIsAdmin())) + return { error: "Unauthorized, not fetching Projects" }; const posts = await Project.findAll(); return { result: JSON.parse(JSON.stringify(posts)) }; } diff --git a/src/components/client/admin/PostTable.tsx b/src/components/client/admin/PostTable.tsx index 3974d2f..e27a106 100644 --- a/src/components/client/admin/PostTable.tsx +++ b/src/components/client/admin/PostTable.tsx @@ -5,161 +5,155 @@ import toast from "react-hot-toast"; import { EditorRenderer, EditorState, PostTableCallbacks } from "./PostEditor"; import { Attributes, InferAttributes } from "@sequelize/core"; import { - Project, - Post, - Bucket, - User, - PostAttributesWithBuckets, + Project, + Post, + Bucket, + User, + PostAttributesWithBuckets, } from "@/models"; import { handleActionResult } from "@/app/lib/actions/clientActionHandler"; - import { - getPostsWithBucketsAndProject, - GetPostsAttributes, + getPostsWithBucketsAndProject, + GetPostsAttributes, } from "@/app/lib/actions/entitymanagement/post/postActions"; - import { PostViewProps } from "@/components/views/admin/post"; import { StateHook } from "@/util/"; export type PostTableStateProps = { - posts: StateHook; - editor: StateHook; + posts: StateHook; + editor: StateHook; }; export type PostTableProps = PostViewProps & { state: PostTableStateProps }; export default function PostTable({ - headings, - posts, - projects, - actions, - state, + headings, + posts, + projects, + actions, + state, }: PostTableProps) { - // Define editor controls - const editorControls = { - closeEditor: () => { - state.editor.setState({ - isEditorOpen: false, - editorPost: posts[0], - }); - }, - showEditor: (entry: GetPostsAttributes) => { - state.editor.setState({ - isEditorOpen: true, - editorPost: entry, - }); - }, - }; + // Define editor controls + const editorControls = { + closeEditor: () => { + state.editor.setState({ + isEditorOpen: false, + editorPost: posts[0], + }); + }, + showEditor: (entry: GetPostsAttributes) => { + state.editor.setState({ + isEditorOpen: true, + editorPost: entry, + }); + }, + }; - const postActions = { - deletePost: (entry: GetPostsAttributes) => { - if (!entry.id) return; - actions.deletePost(entry.id).then((actionResult) => { - const result = handleActionResult(actionResult); - if (!result) return; - state.posts.state.splice(state.posts.state.indexOf(entry), 1); - state.posts.setState([...state.posts.state]); - toast.success("Removed Post:" + entry.id); - }); - }, - refetch: () => { - actions - .getPosts() // Get Posts From Server - .then((getPostsServerActionResult) => { - // Handle Result and toast error on failure - const result = handleActionResult( - getPostsServerActionResult - ); - // Set Posts state - if (result) state.posts.setState(result); - }); - }, - savePost: (e: Partial>) => { - actions - .savePost({ - id: e.id, - content: e.content, - title: e.title, - project_id: e.project_id, - }) - .then((res) => handleActionResult(res)) - .then(getPostsWithBucketsAndProject) - .then((res) => { - const result = handleActionResult(res); - if (result) state.posts.setState(result); - }) - .then(editorControls.closeEditor) - .then(postActions.refetch); - }, - }; + const postActions = { + deletePost: (entry: GetPostsAttributes) => { + if (!entry.id) return; + actions.deletePost(entry.id).then((actionResult) => { + const result = handleActionResult(actionResult); + if (!result) return; + state.posts.state.splice(state.posts.state.indexOf(entry), 1); + state.posts.setState([...state.posts.state]); + toast.success("Removed Post:" + entry.id); + }); + }, + refetch: () => { + actions + .getPosts() // Get Posts From Server + .then((getPostsServerActionResult) => { + // Handle Result and toast error on failure + const result = handleActionResult(getPostsServerActionResult); + // Set Posts state + if (result) state.posts.setState(result); + }); + }, + savePost: (e: Partial>) => { + actions + .savePost({ + id: e.id, + content: e.content, + title: e.title, + project_id: e.project_id, + }) + .then((res) => handleActionResult(res)) + .then(getPostsWithBucketsAndProject) + .then((res) => { + const result = handleActionResult(res); + if (result) state.posts.setState(result); + }) + .then(editorControls.closeEditor) + .then(postActions.refetch); + }, + }; - return ( - - {state.posts.state.map((post: GetPostsAttributes) => ( - - - - {post.id} - - {post.title} - - {!post.content - ? "No Content Found" - : post.content.length < 255 - ? post.content - : `${post.content.substring(0, 255)}...`} - - - {projects.find((e) => { - console.log(e.id); - return e.id == post.project.id; - })?.readableIdentifier || "uncategorized"} - - {post.createdAt?.toString()} - {post.updatedAt?.toString()} - - { - - } - - - { - - } - - - {}, - }} - projects={projects} - /> - - ))} - - ); + return ( + + {state.posts.state.map((post: GetPostsAttributes) => ( + + + + {post.id} + + {post.title} + + {!post.content + ? "No Content Found" + : post.content.length < 255 + ? post.content + : `${post.content.substring(0, 255)}...`} + + + {projects.find((e) => { + console.log(e.id); + return e.id == post.project.id; + })?.readableIdentifier || "uncategorized"} + + {post.createdAt?.toString()} + {post.updatedAt?.toString()} + + { + + } + + + { + + } + + + {}, + }} + projects={projects} + /> + + ))} + + ); } diff --git a/src/components/views/admin/post/CPostView.tsx b/src/components/views/admin/post/CPostView.tsx index 80f48b6..f5b8852 100644 --- a/src/components/views/admin/post/CPostView.tsx +++ b/src/components/views/admin/post/CPostView.tsx @@ -1,11 +1,10 @@ "use client"; - import PostTable, { - PostTableStateProps, + PostTableStateProps, } from "@/components/client/admin/PostTable"; import { - GetPostsAttributes, - PostServerActions, + GetPostsAttributes, + PostServerActions, } from "@/app/lib/actions/entitymanagement/post/postActions"; import { EditorState } from "@/components/client/admin/PostEditor"; import { Project } from "@/models"; @@ -14,38 +13,38 @@ import { Attributes } from "@sequelize/core"; import { parseStateHook } from "@/util/state"; export type PostViewProps = { - children?: ReactNode; - headings: Array; - posts: GetPostsAttributes[]; - projects: Attributes[]; - actions: PostServerActions; + children?: ReactNode; + headings: Array; + posts: GetPostsAttributes[]; + projects: Attributes[]; + actions: PostServerActions; }; export function CPostView({ - // children, - headings, - posts, - projects, - actions, + // 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 state: PostTableStateProps = { - posts: parseStateHook(useState(posts)), - editor: parseStateHook(useState(initEditorState)), - }; - // render out the post table - return ( - - ); + // 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 state: PostTableStateProps = { + posts: parseStateHook(useState(posts)), + editor: parseStateHook(useState(initEditorState)), + }; + // render out the post table + return ( + + ); } diff --git a/src/components/views/admin/post/PostView.tsx b/src/components/views/admin/post/PostView.tsx index ba9756c..b38eb19 100644 --- a/src/components/views/admin/post/PostView.tsx +++ b/src/components/views/admin/post/PostView.tsx @@ -3,11 +3,11 @@ cache: "no-store"; import { ReactNode } from "react"; import { - deletePost, - getPostsWithBucketsAndProject, - updatePost, - GetPostsAttributes, - PostServerActions, + deletePost, + getPostsWithBucketsAndProject, + updatePost, + GetPostsAttributes, + PostServerActions, } from "@/app/lib/actions/entitymanagement/post/postActions"; import { getProjects } from "@/app/lib/actions/entitymanagement/post/projectActions"; import { Bucket, Project, Post, dbSync, Attachment } from "@/models"; @@ -15,43 +15,43 @@ import { handleActionResult } from "@/app/lib/actions/clientActionHandler"; import { CPostView } from "@/components/views/admin/post"; type Props = { - children?: ReactNode; + children?: ReactNode; }; export async function PostView(props: Props) { - const sync = await dbSync; + const sync = await dbSync; - const headings: string[] = [ - "#", - "Title", - "Content", - "Project", - "CreatedAt", - "UpdatedAt", - ]; + const headings: string[] = [ + "#", + "Title", + "Content", + "Project", + "CreatedAt", + "UpdatedAt", + ]; - const posts: GetPostsAttributes[] | undefined = handleActionResult( - await getPostsWithBucketsAndProject() - ); - const projects = await Project.findAll().then((projects) => - projects.map((project) => JSON.parse(JSON.stringify(project))) - ); + const posts: GetPostsAttributes[] | undefined = handleActionResult( + await getPostsWithBucketsAndProject() + ); + const projects = await Project.findAll().then((projects) => + projects.map((project) => JSON.parse(JSON.stringify(project))) + ); - return ( -
-
- -
-
- ); + return ( +
+
+ +
+
+ ); }