From 92bdb03836dc0f5f7293f6bb9dbcb7caa3540336 Mon Sep 17 00:00:00 2001 From: Andreas Date: Mon, 24 Jun 2024 02:58:26 +0200 Subject: [PATCH] Fixed Type Issues --- src/app/api/attachment/route.ts | 2 +- .../actions/entityManagement/postActions.ts | 13 +++++- src/components/client/admin/PostEditor.tsx | 31 +++++++++---- src/components/client/admin/PostTable.tsx | 46 ++++++++++--------- .../server/admin/views/PostView.tsx | 10 ++-- src/model/Post.ts | 12 ++++- 6 files changed, 76 insertions(+), 38 deletions(-) diff --git a/src/app/api/attachment/route.ts b/src/app/api/attachment/route.ts index 1f2488b..7469b71 100644 --- a/src/app/api/attachment/route.ts +++ b/src/app/api/attachment/route.ts @@ -66,7 +66,7 @@ async function addToExistingBucket(bucketid: number): Promise { } -async function tryCreateAttachment(request: Request) { +export async function tryCreateAttachment(request: Request) { // Make sure the DB is ready await dbSync; diff --git a/src/app/lib/actions/entityManagement/postActions.ts b/src/app/lib/actions/entityManagement/postActions.ts index d7574d2..1314175 100644 --- a/src/app/lib/actions/entityManagement/postActions.ts +++ b/src/app/lib/actions/entityManagement/postActions.ts @@ -6,6 +6,7 @@ import { Attributes, where } from "@sequelize/core"; import { cookies } from 'next/headers'; import { getCookieAuth, userIsAdmin } from '../actions'; import { ActionResult } from '../ActionResult'; +import { PostAttributesWithBuckets } from '@/model/Post'; @@ -20,13 +21,23 @@ export async function deletePost(postID: number): Promise> } -export async function getPosts(): Promise[]>> { +export async function getPostsWithBuckets(): 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}},); return {result:JSON.parse(JSON.stringify(posts))}; } + + +export async function getPosts(): Promise[]>> { + await dbSync; + if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."} + const posts = await Post.findAll(); + return {result:JSON.parse(JSON.stringify(posts))}; +} + + export async function updatePost(postAttributes: Partial>): Promise[]>> { await dbSync; if(! await userIsAdmin()) return {error:"Unauthorized, not updating Post."} diff --git a/src/components/client/admin/PostEditor.tsx b/src/components/client/admin/PostEditor.tsx index 28be387..093b54c 100644 --- a/src/components/client/admin/PostEditor.tsx +++ b/src/components/client/admin/PostEditor.tsx @@ -1,14 +1,16 @@ import { ActionResult } from "@/app/lib/actions/ActionResult"; import { handleActionResult } from "@/app/lib/actions/clientActionHandler"; import { Post, Project, Bucket, PostBucket, Attachment } from "@/model/Models"; +import { PostAttributesWithBuckets } from "@/model/Post"; 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>)=>any; + savePost: (p:Partial)=>void; closeEditor: ()=>any; + uploadAttachment : ()=>any; } type PostEditorBucket = Partial> & { attachments:Partial>[] @@ -20,7 +22,7 @@ export type PostEditorPost = Attributes & { export type EditorProps = { open:boolean; - post:PostEditorPost; + post:Partial; projects?:Attributes[]; callbacks:PostTableCallbacks; } @@ -79,8 +81,9 @@ export default function PostEditor(props:EditorProps){ { - (()=>{ - let bucketMap:Map = new Map(props.post.buckets.map((b)=>[b.id as UUID,b])); + props.post.buckets + ? (()=>{ + let bucketMap:Map> = new Map(props.post.buckets.map((b)=>[b.id as UUID,b])); let bucketList = [...props.post.buckets.map((b)=>b.id)]; return bucketList.map((e)=>{ return <> @@ -90,16 +93,28 @@ export default function PostEditor(props:EditorProps){
    -
  • - { bucketMap.get(e as UUID)?.attachments.map((attachment)=>
  • {attachment.filename}
  • ) } +
  • { + 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}
  • ) : <> + })()}
}) - })() + })() + : <> } - diff --git a/src/components/client/admin/PostTable.tsx b/src/components/client/admin/PostTable.tsx index 249bd06..c4d225c 100644 --- a/src/components/client/admin/PostTable.tsx +++ b/src/components/client/admin/PostTable.tsx @@ -8,19 +8,21 @@ import toast from "react-hot-toast" import { ActionResult } from "@/app/lib/actions/ActionResult"; import { handleActionResult } from "@/app/lib/actions/clientActionHandler"; import PostEditor, { EditorProps, PostEditorPost } from "./PostEditor"; -import { getPosts } from "@/app/lib/actions/entityManagement/postActions"; +import { getPostsWithBuckets } from "@/app/lib/actions/entityManagement/postActions"; +import { PostAttributesWithBuckets } from "@/model/Post"; type Actions = { deletePost: (id: number) => Promise> - getPosts: () => Promise> + getPosts: () => Promise> getProjects: () => Promise[]>> savePost: (data: Partial>) => Promise[]>> + // uploadAttachment: ()=> Promise> } type Props = { children?: ReactNode; headings: Array; - data: PostEditorPost[]; + data: Partial[]; projects: Attributes[]; actions: Actions; } @@ -33,7 +35,7 @@ export default function PostTable(props: Props) { }) } - function showEditor(entry: PostEditorPost): void { + function showEditor(entry: Partial): void { setEditor({ open: true, post: entry @@ -49,15 +51,16 @@ export default function PostTable(props: Props) { const [projects, setProjects] = useState(props.projects); - function deletePost(entry: PostEditorPost) { + function deletePost(entry: Partial) { + if(! entry.id) return; props.actions?.deletePost(entry.id) - .then(actionResult => { - const result = handleActionResult(actionResult); - if (!result) return; - posts.splice(posts.indexOf(entry), 1); - setPosts([...posts]) - toast.success('Removed Post:' + entry.id); - }); + .then(actionResult => { + const result = handleActionResult(actionResult); + if (!result) return; + posts.splice(posts.indexOf(entry), 1); + setPosts([...posts]) + toast.success('Removed Post:' + entry.id); + }); } const aifa = (a: ReactNode, b: ReactNode) => a ? a : b @@ -73,29 +76,28 @@ export default function PostTable(props: Props) { }) } - function savePost(e: Partial) { + function savePost(e: Partial) { props.actions.savePost({ id: e.id, content: e.content, title: e.title, project_id: e.project_id }) - .then(res => handleActionResult(res)) - .then(getPosts) - .then(res => { - 1 - if (!res.error) setPosts(res.result as PostEditorPost[]); - }) + .then(res => handleActionResult(res)) + .then(getPostsWithBuckets) + .then(res => { + if (!res.error && res.result) setPosts(res.result); + }) closeEditor(); }; return <> - {posts.map((d: PostEditorPost) => <> + {posts.map((d: Partial) => <> {d.id} {d.title} - {d.content.length < 255 ? d.content : `${d.content.substring(0, 255)}...`} + {d.content? (d.content.length < 255 ? d.content : `${d.content.substring(0, 255)}...`) : "No Content Found"} { aifa((projects.find((e) => { return (e.id == d.project_id) @@ -108,7 +110,7 @@ export default function PostTable(props: Props) { { (editor.open && editor.post && editor.post.id == d.id) - ? + ?{} }} open={editor.open} post={editor.post} projects={projects} > : "" } )} diff --git a/src/components/server/admin/views/PostView.tsx b/src/components/server/admin/views/PostView.tsx index 7dab099..1c4a401 100644 --- a/src/components/server/admin/views/PostView.tsx +++ b/src/components/server/admin/views/PostView.tsx @@ -5,9 +5,10 @@ import { constructAPIUrl } from "@/util/Utils"; import { ReactNode, useEffect } from "react"; import TableGen from "../../../client/TableGen"; import PostTable from "@/components/client/admin/PostTable"; -import { deletePost, getPosts, updatePost } from "@/app/lib/actions/entityManagement/postActions"; +import { deletePost, getPostsWithBuckets, 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"; type Props = { children?:ReactNode @@ -29,9 +30,10 @@ export default async function PostView(props:Props){ ] const actions = { deletePost: deletePost, - getPosts:getPosts, getProjects: - getProjects, - savePost:updatePost + 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)))); diff --git a/src/model/Post.ts b/src/model/Post.ts index e519dc8..3c11a0c 100644 --- a/src/model/Post.ts +++ b/src/model/Post.ts @@ -1,4 +1,4 @@ -import { Association, BelongsToGetAssociationMixin, BelongsToManyAddAssociationMixin, BelongsToManyAssociation, BelongsToManyCreateAssociationMixin, BelongsToManyGetAssociationsMixin, BelongsToManyRemoveAssociationMixin, BelongsToManySetAssociationsMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";import { User } from "./User"; +import { Association, Attributes, BelongsToGetAssociationMixin, BelongsToManyAddAssociationMixin, BelongsToManyAssociation, BelongsToManyCreateAssociationMixin, BelongsToManyGetAssociationsMixin, BelongsToManyRemoveAssociationMixin, BelongsToManySetAssociationsMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";import { User } from "./User"; import { SqliteDialect } from '@sequelize/sqlite3'; import { Attribute, AutoIncrement, BelongsTo, BelongsToMany, CreatedAt, Default, HasMany, NotNull, PrimaryKey, Unique, UpdatedAt } from "@sequelize/core/decorators-legacy"; @@ -9,7 +9,15 @@ import { Attachment } from "./Attachment"; import { Bucket } from "./Bucket"; import { UUID } from "crypto"; -export class Post extends Model, InferCreationAttributes> { +type PostAttributes = InferAttributes; + +type PostCreationAttributes = InferCreationAttributes; + +export type PostAttributesWithBuckets = PostAttributes & { + buckets:Attributes[] +} + +export class Post extends Model { // Attributes