This commit is contained in:
2024-06-26 10:40:09 +02:00
parent a492bf4ac0
commit de212c367f
5 changed files with 101 additions and 56 deletions
+44 -38
View File
@@ -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<ActionResult<boolean>>
getPosts: () => Promise<ActionResult<PostAttributesWithBuckets[]>>
getPosts: () => Promise<ActionResult<GetPostsAttributes[]>>
getProjects: () => Promise<ActionResult<Attributes<Project>[]>>
savePost: (data: Partial<Attributes<Post>>) => Promise<ActionResult<Attributes<Post>[]>>
// uploadAttachment: ()=> Promise<ActionResult<any>>
@@ -34,9 +34,9 @@ type Actions = {
type Props = {
children?: ReactNode;
headings: Array<string>;
data: Partial<PostAttributesWithBuckets>[];
data: GetPostsAttributes[];
projects: Attributes<Project>[];
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<PostAttributesWithBuckets>): void {
function showEditor(entry: GetPostsAttributes): void {
setEditor({
editorOpenState: true,
openedPost: entry
})
}
function deletePost(entry: Partial<PostAttributesWithBuckets>) {
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<PostAttributesWithBuckets>) {
function savePost(e: Partial<Attributes<Post>>) {
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<PostAttributesWithBuckets> }) {
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: <button key="deletebutton" type="button" className="btn btn-danger" onClick={() => deletePost(postData)}> Delete</button>
}
].map(field =>
].map(field =>
<td key={field.key}>{field.content}</td>
);
}
function TableRow(props: {postData: Partial<PostAttributesWithBuckets>, 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} />
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>
{(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>
: ""}
</>
: ""}
</>
}
return <>
<EntityManagementTable entityName="Post" key="der-table" headings={props.headings}>
{posts.map((postData: Partial<PostAttributesWithBuckets>) => <TableRow headings={props.headings}
postData={postData}
key={postData.id}/> )}
<EntityManagementTable entityName="Post"
key="der-table"
headings={props.headings}>
{posts.map((postData: GetPostsAttributes) =>
<TableRow headings={props.headings}
postData={postData}
key={postData.id} />
)}
</EntityManagementTable>
</>
}