changed stuff

This commit is contained in:
2024-06-27 07:58:29 +02:00
parent de212c367f
commit 2dca4a5a80
31 changed files with 153 additions and 136 deletions
+80 -81
View File
@@ -11,17 +11,17 @@ import React, {
} from "react";
import EntityManagementTable from "../EntityManagementTable";
import toast from "react-hot-toast";
import PostEditor, { EditorProps } from "./PostEditor";
import PostEditor, { EditorProps, EditorRenderer } from "./PostEditor";
import {
Attributes,
CreationAttributes
} from "@sequelize/core";
import { useState } from "react";
import { Project, Post, Bucket } from "@/model/Models";
import { Project, Post, Bucket } from "@/models";
import { ActionResult } from "@/app/lib/actions/ActionResult";
import { handleActionResult } from "@/app/lib/actions/clientActionHandler";
import { getPostsWithBucketsAndProject, GetPostsAttributes } from "@/app/lib/actions/entityManagement/postActions";
import { PostAttributesWithBuckets } from "@/model/Post";
import { PostAttributesWithBuckets } from "@/models";
export type PostTableActions = {
deletePost: (id: number) => Promise<ActionResult<boolean>>
@@ -43,29 +43,34 @@ const aifa = (a: ReactNode, b: ReactNode) => a ? a : b
export default function PostTable(props: Props) {
const initEditorState: Partial<EditorProps> = {
editorOpenState: false
}
const initEditorState: EditorProps = {
...{
editorOpenState: false
}, ...{
callbacks: undefined,
openedPost: undefined,
projects: undefined,
}
};
// Set up required state hooks
const [posts, setPosts] = useState(props.data);
const [editor, setEditor] = useState(initEditorState)
const [editorState, setEditorState] = useState(initEditorState)
const [projects, setProjects] = useState(props.projects);
// Define editor controls
function closeEditor(): void {
setEditor({
editorOpenState: false
})
}
function showEditor(entry: GetPostsAttributes): void {
setEditor({
editorOpenState: true,
openedPost: entry
})
const editorControls = {
closeEditor: () => {
setEditorState({
editorOpenState: false
})
},
showEditor: (entry: GetPostsAttributes) => {
setEditorState({
editorOpenState: true,
openedPost: entry
})
}
}
function deletePost(entry: GetPostsAttributes) {
@@ -85,7 +90,8 @@ export default function PostTable(props: Props) {
.then((p) => {
const result = handleActionResult(p)
if (result) setPosts(result)
}).then(props.actions.getProjects)
})
.then(props.actions.getProjects)
.then(e => {
const result = handleActionResult(e);
if (result) setProjects(result)
@@ -105,68 +111,61 @@ export default function PostTable(props: Props) {
const result = handleActionResult(res);
if (result) setPosts(result);
})
closeEditor();
editorControls.closeEditor();
};
function TableFields({ postData }: { postData: GetPostsAttributes }) {
return [
{
key: 'post-title',
content: postData.title
}, {
key: 'post-content',
content:
!postData.content ? "No Content Found" :
postData.content.length < 255 ? postData.content :
`${postData.content.substring(0, 255)}...`
}, {
key: 'post-project',
content: aifa((projects.find((e) => {
console.log(e.id)
return (e.id == postData.project.id)
})?.readableIdentifier), 'uncategorized')
}, {
key: 'post-createdat',
content: postData.createdAt?.toString()
}, {
key: 'post-updatedat',
content: postData.updatedAt?.toString()
}, {
key: 'post-editbutton',
content: <button key="editbutton" type="button" className="btn btn-primary" onClick={() => showEditor(postData)}>Edit</button>
}, {
key: 'post-deletebutton',
content: <button key="deletebutton" type="button" className="btn btn-danger" onClick={() => deletePost(postData)}> Delete</button>
}
].map(field =>
<td key={field.key}>{field.content}</td>
);
}
function TableRow(props: { postData: GetPostsAttributes, headings: string | any[] }) {
return <><tr key={props.postData.id}>
<th key={`rowheading-post-${props.postData.id}`} scope="row">{props.postData.id}</th>
<TableFields key={`tableFields-${props.postData.id}`} postData={props.postData} />
</tr>
{(editor.editorOpenState
&& editor.openedPost
&& editor.openedPost.id == props.postData.id)
? <tr>
<th scope="row" colSpan={props.headings.length}>
<PostEditor callbacks={{
savePost: savePost,
closeEditor: closeEditor,
uploadAttachment: () => { }
}}
editorOpenState={editor.editorOpenState}
openedPost={editor.openedPost}
projects={projects} />
</th>
</tr>
: ""}
function TableRow(props: { postData: GetPostsAttributes, headings: string[] }) {
return <>
<tr key={props.postData.id}>
<th key={`rowheading-post-${props.postData.id}`} scope="row">{props.postData.id}</th>
<td key='title'>{props.postData.title}</td>
<td key='content'>
{
!props.postData.content ? "No Content Found" :
props.postData.content.length < 255 ? props.postData.content :
`${props.postData.content.substring(0, 255)}...`
}
</td>
<td key='project'>
{
aifa((projects.find((e) => {
console.log(e.id)
return (e.id == props.postData.project.id)
})?.readableIdentifier), 'uncategorized')
}
</td>
<td key='createdAt'>
{
props.postData.createdAt?.toString()
}
</td>
<td key='updatedAt'>
{
props.postData.updatedAt?.toString()
}
</td>
<td key='edit'>
{
<button key="editbutton" type="button" className="btn btn-primary" onClick={() => editorControls.showEditor(props.postData)}>Edit</button>
}
</td>
<td key='delete'>
{
<button key="deletebutton" type="button" className="btn btn-danger" onClick={() => deletePost(props.postData)}> Delete</button>
}
</td>
</tr>
<EditorRenderer headings={props.headings}
postData={props.postData}
editorControls={editorControls}
editorState={editorState}
callbacks={{
savePost: savePost,
closeEditor: editorControls.closeEditor,
uploadAttachment: () => { }
}}
projects={projects}
/>
</>
}