Fixed Type Issues

This commit is contained in:
2024-06-24 02:58:26 +02:00
parent 39f3c02e7c
commit 92bdb03836
6 changed files with 76 additions and 38 deletions
+24 -22
View File
@@ -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<ActionResult<boolean>>
getPosts: () => Promise<ActionResult<PostEditorPost[]>>
getPosts: () => Promise<ActionResult<PostAttributesWithBuckets[]>>
getProjects: () => Promise<ActionResult<Attributes<Project>[]>>
savePost: (data: Partial<Attributes<Post>>) => Promise<ActionResult<Attributes<Post>[]>>
// uploadAttachment: ()=> Promise<ActionResult<any>>
}
type Props = {
children?: ReactNode;
headings: Array<string>;
data: PostEditorPost[];
data: Partial<PostAttributesWithBuckets>[];
projects: Attributes<Project>[];
actions: Actions;
}
@@ -33,7 +35,7 @@ export default function PostTable(props: Props) {
})
}
function showEditor(entry: PostEditorPost): void {
function showEditor(entry: Partial<PostAttributesWithBuckets>): 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<PostAttributesWithBuckets>) {
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<PostEditorPost>) {
function savePost(e: Partial<PostAttributesWithBuckets>) {
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 <>
<TableGen headings={props.headings}>
{posts.map((d: PostEditorPost) => <>
{posts.map((d: Partial<PostAttributesWithBuckets>) => <>
<tr key={d.id}>
<th key={`row${d.id}`} scope="row">{d.id}</th>
<td key='titlefield'>{d.title}</td>
<td key='contentfield'>{d.content.length < 255 ? d.content : `${d.content.substring(0, 255)}...`}</td>
<td key='contentfield'>{d.content? (d.content.length < 255 ? d.content : `${d.content.substring(0, 255)}...`) : "No Content Found"}</td>
<td key='project'>{
aifa((projects.find((e) => {
return (e.id == d.project_id)
@@ -108,7 +110,7 @@ export default function PostTable(props: Props) {
</tr>
{
(editor.open && editor.post && editor.post.id == d.id)
?<tr key={'activeEditor'}><th scope="row" colSpan={props.headings.length}><PostEditor callbacks={{ savePost: savePost, closeEditor: closeEditor }} open={editor.open} post={editor.post} projects={projects} ></PostEditor></th></tr>
?<tr key={'activeEditor'}><th scope="row" colSpan={props.headings.length}><PostEditor callbacks={{ savePost: savePost, closeEditor: closeEditor, uploadAttachment: ()=>{} }} open={editor.open} post={editor.post} projects={projects} ></PostEditor></th></tr>
: ""
}
</>)}