diff --git a/src/app/lib/actions/entityManagement/post/postActions.ts b/src/app/lib/actions/entityManagement/post/postActions.ts index f2cba7d..772750c 100644 --- a/src/app/lib/actions/entityManagement/post/postActions.ts +++ b/src/app/lib/actions/entityManagement/post/postActions.ts @@ -6,6 +6,7 @@ import { Attributes, where } from "@sequelize/core"; import { getCookieAuth, userIsAdmin } from '../../actions'; import { ActionResult } from '../../ActionResult'; import { PostAttributesWithBuckets } from '@/models'; +import { inspect } from 'util'; export async function deletePost(postID: number): Promise> { await dbSync; @@ -30,7 +31,7 @@ export type GetPostsAttributes = { export async function getPostsWithBucketsAndProject(): Promise> { await dbSync; if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."} - const posts = await Post.findAll({ + const posts:Post[] = await Post.findAll({ include: [ {association: Post.associations.buckets, include: Bucket.associations.attachments}, {association: Post.associations.user}, @@ -38,22 +39,7 @@ export async function getPostsWithBucketsAndProject(): Promise{ - 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 + return inspect(e) })))}; } diff --git a/src/components/client/admin/PostEditor.tsx b/src/components/client/admin/PostEditor.tsx index 36938ee..4b0e547 100644 --- a/src/components/client/admin/PostEditor.tsx +++ b/src/components/client/admin/PostEditor.tsx @@ -1,296 +1,243 @@ -import { GetPostsAttributes } from "@/app/lib/actions/entityManagement/post/postActions"; +import { GetPostsAttributes } from "@/app/lib/actions/entitymanagement/post/postActions"; import { Post, Project, Bucket, PostBucket, Attachment } from "@/models"; import { Attributes } from "@sequelize/core"; import { UUID } from "crypto"; -import { EntityEditorTextArea } from '../input/EntityEditorTextArea'; +import { EntityEditorTextArea } from "../input/EntityEditorTextArea"; +import { ChangeEvent, ChangeEventHandler, MouseEventHandler, useState } from "react"; import { - ChangeEventHandler, - MouseEventHandler, - useState, -} from "react"; -import { - Accordion, - AccordionBody, - AccordionHeader, - AccordionItem, + Accordion, + AccordionBody, + AccordionHeader, + AccordionItem, } from "react-bootstrap"; export type PostTableCallbacks = { - savePost: (p: Partial>) => void; - refetch: () => any; - closeEditor: () => any; - uploadAttachment: () => any; + savePost: (p: Partial>) => void; + refetch: () => any; + closeEditor: () => any; + uploadAttachment: () => any; }; export type EditorState = { - isEditorOpen: boolean; - editorPost: GetPostsAttributes; + isEditorOpen: boolean; + editorPost: GetPostsAttributes; }; export type EditorProps = { - editorPost: GetPostsAttributes; - projectList: { - id: number; - readableIdentifier: string; - }[]; - callbacks: PostTableCallbacks; + editorPost: GetPostsAttributes; + projectList: { + id: number; + readableIdentifier: string; + }[]; + callbacks: PostTableCallbacks; }; export default function PostEditor({ - editorPost, - projectList, - callbacks, + editorPost, + projectList, + callbacks, }: EditorProps) { - // Setup State Hooks - let [postContentState, setPostContentState] = useState(editorPost.content); - let [postTitleState, setPostTitleState] = useState(editorPost.title); - let [postProjectIDState, setPostProjectIDState] = useState( - editorPost.project.id - ); + // Setup State Hooks + let [postContentState, setPostContentState] = useState(editorPost.content); + let [postTitleState, setPostTitleState] = useState(editorPost.title); + let [postProjectIDState, setPostProjectIDState] = useState( + editorPost.project.id + ); - // Handle changing the selected project using the dropdown select - const projectSelectionChange: ChangeEventHandler = (e) => - setPostProjectIDState(parseInt(e.target.value)); - // Handle clicking the save button - const onClickSaveButton: MouseEventHandler = (e) => { - callbacks?.savePost({ - id: editorPost?.id as number, - content: postContentState as string, - title: postTitleState as string, - project_id: postProjectIDState, - }); - }; - const onClickCancelButton: MouseEventHandler = (e) => { - callbacks?.closeEditor(); - }; + // Handle changing the selected project using the dropdown select + const projectSelectionChange: ChangeEventHandler = (e) => + setPostProjectIDState(parseInt(e.target.value)); + // Handle clicking the save button + const onClickSaveButton: MouseEventHandler = (e) => { + callbacks?.savePost({ + id: editorPost?.id as number, + content: postContentState as string, + title: postTitleState as string, + project_id: postProjectIDState, + }); + }; + const onClickCancelButton: MouseEventHandler = (e) => { + callbacks?.closeEditor(); + }; - return ( - <> -
-

- Edit Post -

-

- Title -

- setPostTitleState(e.target.value)} - type="text" - className="m-2" - /> -

- Content -

- - -

- Project -

- -

Attachments

- - - - - - - - {editorPost?.buckets ? ( - (() => { - let bucketMap: Map< - UUID, - Attributes - > = new Map( - editorPost.buckets.map((b) => [ - b.id as UUID, - b, - ]) - ); - let bucketList = [ - ...editorPost.buckets.map((b) => b.id), - ]; - return bucketList.map((e) => { - return ( - <> - - - - - {e} - - -
    -
  • - { - for ( - let index = 0; - index < - (( - e - .target - .files as FileList - ) - .length as number); - index++ - ) { - const element = - ( - e - .target - .files as FileList - )[ - index - ]; - const fReader = - new FileReader(); - fReader.readAsDataURL( - element - ); - fReader.onloadend = - ( - event - ) => { - console.log( - event - .target - ?.result - ); - }; - } - }} - /> -
  • - {(() => { - const bucket = - bucketMap.get( - e as UUID - ); - return bucket && - bucket.attachments ? ( - bucket.attachments.map( - ( - attachment - ) => ( -
  • - { - attachment.filename - } -
  • - ) - ) - ) : ( - <> - ); - })()} -
-
-
-
- - - ); - }); - })() - ) : ( - <> - )} - -
Buckets
- - - -
- - ); + return ( + <> +
+

+ Edit Post +

+

+ Title +

+ setPostTitleState(e.target.value)} + type="text" + className="m-2" + /> +

+ Content +

+ +

+ Project +

+ +

Attachments

+ + + + + + + + {editorPost?.buckets ? ( + (() => { + let bucketMap: Map> = new Map( + editorPost.buckets.map((b) => [b.id as UUID, b]) + ); + let bucketList:UUID[] = [...editorPost.buckets.map((b) => b.id)]; + return bucketList.map((bucketID,i) => { + return ( + <> + + + + {bucketID} + +
    +
  • + +
  • + {(() => { + const bucket: Attributes | undefined = bucketMap.get(bucketID); + return bucket && bucket.attachments && ( + bucket.attachments.map((attachment) => ( +
  • + {attachment.filename} +
  • + )) + ); + })()} +
+
+
+
+ + + ); + }); + })() + ) : ( + <> + )} + +
Buckets
+ + + +
+ + ); } type EditorRendererProps = { - editorPost: GetPostsAttributes; - headings: string[]; - editorState: EditorState; - editorControls: any; - callbacks: any; - projects: any; + editorPost: GetPostsAttributes; + headings: string[]; + editorState: EditorState; + editorControls: any; + callbacks: any; + projects: any; }; -export function EditorRenderer({ - editorPost, - headings, - editorState, - callbacks, - projects, -}: EditorRendererProps) { - return editorState.isEditorOpen && - editorState.editorPost && - editorState.editorPost.id == editorPost.id ? ( - - - - - - ) : ( - "" - ); +function onFileInputChange(): ChangeEventHandler { + return (e: ChangeEvent) => { + for (let index = 0; index < + ((e.target.files as FileList) + .length as number); index++) { + const element = ( + e.target.files as FileList + )[index]; + const fReader = new FileReader(); + fReader.readAsDataURL(element); + fReader.onloadend = (event) => { + console.log(event.target?.result); + }; + } + }; +} + +export function EditorRenderer({ + editorPost, + headings, + editorState, + callbacks, + projects, +}: EditorRendererProps) { + return editorState.isEditorOpen && + editorState.editorPost && + editorState.editorPost.id == editorPost.id ? ( + + + + + + ) : ( + "" + ); }