From de212c367f9d50705c220298bc90937e9de0df1b Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 26 Jun 2024 10:40:09 +0200 Subject: [PATCH] ya --- src/app/lib/actions/clientActionHandler.ts | 2 +- .../actions/entityManagement/postActions.ts | 45 ++++++++-- src/components/client/admin/PostEditor.tsx | 10 ++- src/components/client/admin/PostTable.tsx | 82 ++++++++++--------- .../server/admin/views/PostView.tsx | 18 ++-- 5 files changed, 101 insertions(+), 56 deletions(-) diff --git a/src/app/lib/actions/clientActionHandler.ts b/src/app/lib/actions/clientActionHandler.ts index f9fed42..2efacb7 100644 --- a/src/app/lib/actions/clientActionHandler.ts +++ b/src/app/lib/actions/clientActionHandler.ts @@ -1,4 +1,4 @@ -'use client' + import toast from "react-hot-toast"; import { ActionResult } from "./ActionResult"; diff --git a/src/app/lib/actions/entityManagement/postActions.ts b/src/app/lib/actions/entityManagement/postActions.ts index 03c1f16..f0b2f10 100644 --- a/src/app/lib/actions/entityManagement/postActions.ts +++ b/src/app/lib/actions/entityManagement/postActions.ts @@ -1,7 +1,7 @@ 'use server'; import { revalidatePath, revalidateTag } from 'next/cache' -import { Bucket, Post, PostBucket, User, dbSync } from "@/model/Models"; +import { Bucket, Post, PostBucket, Project, User, dbSync } from "@/model/Models"; import { Attributes, where } from "@sequelize/core"; import { getCookieAuth, userIsAdmin } from '../actions'; import { ActionResult } from '../ActionResult'; @@ -15,12 +15,46 @@ export async function deletePost(postID: number): Promise> return {result: true}; } -export async function getPostsWithBucketsAndProject(): Promise> { +export type GetPostsAttributes = { + id: number; + title: string; + content: string; + description: string; + buckets: Attributes[]; + user: Attributes; + project: Attributes; + createdAt: Date; + updatedAt: Date; +} + +export async function getPostsWithBucketsAndProject(): Promise> { await dbSync; if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."} - const posts = await Post.findAll({include: [{association: Post.associations.buckets, include: Bucket.associations.attachments}, {association: Post.associations.project}], nest: false}); - console.dir(JSON.parse(JSON.stringify(posts)),{depth: 10, colors: true}) - return {result:JSON.parse(JSON.stringify(posts))}; + const posts = await Post.findAll({ + include: [ + {association: Post.associations.buckets, include: Bucket.associations.attachments}, + {association: Post.associations.user}, + {association: Post.associations.project}], + nest: false + }); + return {result:JSON.parse(JSON.stringify(posts.map((e:Post)=>{ + return { + id: e.id, + title: e.title, + content: e.content, + description: e.description, + buckets: e.buckets ? e.buckets : [], + user: e.user, + project: { + id: e.project_id, + name: e.project ? e.project.name : "", + readableIdentifier: e.project ? e.project.readableIdentifier : "", + posts: e.project ? e.project.posts : [] + }, + createdAt: e.createdAt, + updatedAt: e.updatedAt + } as GetPostsAttributes + })))}; } export async function getPosts(): Promise[]>> { @@ -32,6 +66,7 @@ export async function getPosts(): Promise[]>> { export async function updatePost(postAttributes: Partial>): Promise[]>> { await dbSync; + console.log(`testing postattributes projectid ${postAttributes.project?.id} ${postAttributes.project_id}`) if(! await userIsAdmin()) return {error:"Unauthorized, not updating Post."} const post = await Post.update(postAttributes, {where:{id:postAttributes.id}}); return {result:JSON.parse(JSON.stringify(post))}; diff --git a/src/components/client/admin/PostEditor.tsx b/src/components/client/admin/PostEditor.tsx index c124f52..3648b88 100644 --- a/src/components/client/admin/PostEditor.tsx +++ b/src/components/client/admin/PostEditor.tsx @@ -1,21 +1,23 @@ import { ActionResult } from "@/app/lib/actions/ActionResult"; import { handleActionResult } from "@/app/lib/actions/clientActionHandler"; +import { GetPostsAttributes } from "@/app/lib/actions/entityManagement/postActions"; import { Post, Project, Bucket, PostBucket, Attachment } from "@/model/Models"; import { PostAttributesWithBuckets } from "@/model/Post"; +import { DeepPartial } from "@/util/DeepPartial"; import { Attributes } from "@sequelize/core"; import { UUID } from "crypto"; import { ChangeEventHandler, MouseEventHandler, useLayoutEffect, useRef, useState } from "react"; import { Accordion, AccordionBody, AccordionHeader, AccordionItem } from "react-bootstrap"; export type PostTableCallbacks = { - savePost: (p:Partial)=>void; + savePost: (p:Partial>)=>void; closeEditor: ()=>any; uploadAttachment : ()=>any; } export type EditorProps = { editorOpenState:boolean; - openedPost:Partial; + openedPost:GetPostsAttributes; projects?:Attributes[]; callbacks:PostTableCallbacks; } @@ -24,7 +26,7 @@ export type EditorProps = { export default function PostEditor(props:EditorProps){ let [content,setContent] = useState(props.openedPost?.content) let [title,setTitle] = useState(props.openedPost?.title) - let [projectID,setProjectID] = useState(props.openedPost?.project_id) + let [projectID,setProjectID] = useState(props.openedPost?.project?.id) let textbox:any = useRef(undefined); function adjustHeight(): void { @@ -42,7 +44,7 @@ export default function PostEditor(props:EditorProps){ id: props.openedPost.id as number, content:content as string, title:title as string, - project_id:projectID + project_id: projectID }); } const onClickCancelButton:MouseEventHandler = (e)=>{props.callbacks.closeEditor();} diff --git a/src/components/client/admin/PostTable.tsx b/src/components/client/admin/PostTable.tsx index 44b5230..47438bd 100644 --- a/src/components/client/admin/PostTable.tsx +++ b/src/components/client/admin/PostTable.tsx @@ -20,12 +20,12 @@ import { useState } from "react"; import { Project, Post, Bucket } from "@/model/Models"; import { ActionResult } from "@/app/lib/actions/ActionResult"; import { handleActionResult } from "@/app/lib/actions/clientActionHandler"; -import { getPostsWithBucketsAndProject } from "@/app/lib/actions/entityManagement/postActions"; +import { getPostsWithBucketsAndProject, GetPostsAttributes } from "@/app/lib/actions/entityManagement/postActions"; import { PostAttributesWithBuckets } from "@/model/Post"; -type Actions = { +export type PostTableActions = { deletePost: (id: number) => Promise> - getPosts: () => Promise> + getPosts: () => Promise> getProjects: () => Promise[]>> savePost: (data: Partial>) => Promise[]>> // uploadAttachment: ()=> Promise> @@ -34,9 +34,9 @@ type Actions = { type Props = { children?: ReactNode; headings: Array; - data: Partial[]; + data: GetPostsAttributes[]; projects: Attributes[]; - actions: Actions; + actions: PostTableActions; } const aifa = (a: ReactNode, b: ReactNode) => a ? a : b @@ -61,14 +61,14 @@ export default function PostTable(props: Props) { editorOpenState: false }) } - function showEditor(entry: Partial): void { + function showEditor(entry: GetPostsAttributes): void { setEditor({ editorOpenState: true, openedPost: entry }) } - function deletePost(entry: Partial) { + function deletePost(entry: GetPostsAttributes) { if (!entry.id) return; props.actions?.deletePost(entry.id) .then(actionResult => { @@ -92,7 +92,7 @@ export default function PostTable(props: Props) { }) } - function savePost(e: Partial) { + function savePost(e: Partial>) { props.actions.savePost({ id: e.id, content: e.content, @@ -102,12 +102,13 @@ export default function PostTable(props: Props) { .then(res => handleActionResult(res)) .then(getPostsWithBucketsAndProject) .then(res => { - if (!res.error && res.result) setPosts(res.result); + const result = handleActionResult(res); + if (result) setPosts(result); }) closeEditor(); }; - function TableFields({ postData }: { postData: Partial }) { + function TableFields({ postData }: { postData: GetPostsAttributes }) { return [ { key: 'post-title', @@ -122,7 +123,8 @@ export default function PostTable(props: Props) { }, { key: 'post-project', content: aifa((projects.find((e) => { - return (e.id == postData.project_id) + console.log(e.id) + return (e.id == postData.project.id) })?.readableIdentifier), 'uncategorized') }, { key: 'post-createdat', @@ -137,42 +139,46 @@ export default function PostTable(props: Props) { key: 'post-deletebutton', content: } - ].map(field => + ].map(field => {field.content} ); } - function TableRow(props: {postData: Partial, headings: string | any[]}){ - return <> - {props.postData.id} - + function TableRow(props: { postData: GetPostsAttributes, headings: string | any[] }) { + return <> + {props.postData.id} + + + {(editor.editorOpenState + && editor.openedPost + && editor.openedPost.id == props.postData.id) + + ? + + { } + }} + editorOpenState={editor.editorOpenState} + openedPost={editor.openedPost} + projects={projects} /> + - {(editor.editorOpenState - && editor.openedPost - && editor.openedPost.id == props.postData.id) - ? - - { } - }} - editorOpenState={editor.editorOpenState} - openedPost={editor.openedPost} - projects={projects} /> - - - - : ""} - + : ""} + } return <> - - {posts.map((postData: Partial) => )} + + {posts.map((postData: GetPostsAttributes) => + + )} } \ No newline at end of file diff --git a/src/components/server/admin/views/PostView.tsx b/src/components/server/admin/views/PostView.tsx index 9dbfef6..b468c4a 100644 --- a/src/components/server/admin/views/PostView.tsx +++ b/src/components/server/admin/views/PostView.tsx @@ -4,15 +4,17 @@ import { tryFetchPosts } from "@/app/api/post/route"; import { constructAPIUrl } from "@/util/Utils"; import { ReactNode, useEffect } from "react"; import EntityManagementTable from "../../../client/EntityManagementTable"; -import PostTable from "@/components/client/admin/PostTable"; -import { deletePost, getPostsWithBucketsAndProject, updatePost } from "@/app/lib/actions/entityManagement/postActions"; +import PostTable, { PostTableActions } from "@/components/client/admin/PostTable"; +import { deletePost, getPostsWithBucketsAndProject, GetPostsAttributes, updatePost } from "@/app/lib/actions/entityManagement/postActions"; import { getProjects } from "@/app/lib/actions/entityManagement/projectActions"; import { Bucket, Project, Post, dbSync, Attachment } from "@/model/Models"; import { tryCreateAttachment } from "@/app/api/attachment/route"; +import { handleActionResult } from '../../../../app/lib/actions/clientActionHandler'; type Props = { children?:ReactNode } + async function getHeadings(){ let headings:string[] = ['#', 'Title', 'Content', 'Project', 'CreatedAt', 'UpdatedAt'] @@ -24,7 +26,7 @@ export default async function PostView(props:Props){ const headings = await getHeadings(); - const actions = { + const actions:PostTableActions = { deletePost: deletePost, getPosts:getPostsWithBucketsAndProject, getProjects:getProjects, @@ -32,16 +34,16 @@ export default async function PostView(props:Props){ // 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 posts:GetPostsAttributes[]| undefined = handleActionResult(await getPostsWithBucketsAndProject()); const projects = await Project.findAll().then(projects=>projects.map((e)=>JSON.parse(JSON.stringify(e)))); return (
- +