push work
This commit is contained in:
		
							parent
							
								
									7312ab632c
								
							
						
					
					
						commit
						d8d0aaa90e
					
				| @ -1,11 +1,14 @@ | ||||
| 'use server'; | ||||
| "use server"; | ||||
| import { Project } from "@/models"; | ||||
| import { ActionResult } from "../../ActionResult"; | ||||
| import { Attributes } from "@sequelize/core"; | ||||
| import { userIsAdmin } from "../../actions"; | ||||
| 
 | ||||
| export async function getProjects(): Promise<ActionResult<Attributes<Project>[]>> { | ||||
|   if (! await userIsAdmin()) return { error: 'Unauthorized, not fetching Projects' } | ||||
| export async function getProjects(): Promise< | ||||
|   ActionResult<Attributes<Project>[]> | ||||
| > { | ||||
|   if (!(await userIsAdmin())) | ||||
|     return { error: "Unauthorized, not fetching Projects" }; | ||||
|   const posts = await Project.findAll(); | ||||
|   return { result: JSON.parse(JSON.stringify(posts)) }; | ||||
| } | ||||
|  | ||||
| @ -5,161 +5,155 @@ import toast from "react-hot-toast"; | ||||
| import { EditorRenderer, EditorState, PostTableCallbacks } from "./PostEditor"; | ||||
| import { Attributes, InferAttributes } from "@sequelize/core"; | ||||
| import { | ||||
| 	Project, | ||||
| 	Post, | ||||
| 	Bucket, | ||||
| 	User, | ||||
| 	PostAttributesWithBuckets, | ||||
|   Project, | ||||
|   Post, | ||||
|   Bucket, | ||||
|   User, | ||||
|   PostAttributesWithBuckets, | ||||
| } from "@/models"; | ||||
| import { handleActionResult } from "@/app/lib/actions/clientActionHandler"; | ||||
| 
 | ||||
| import { | ||||
| 	getPostsWithBucketsAndProject, | ||||
| 	GetPostsAttributes, | ||||
|   getPostsWithBucketsAndProject, | ||||
|   GetPostsAttributes, | ||||
| } from "@/app/lib/actions/entitymanagement/post/postActions"; | ||||
| 
 | ||||
| import { PostViewProps } from "@/components/views/admin/post"; | ||||
| import { StateHook } from "@/util/"; | ||||
| 
 | ||||
| export type PostTableStateProps = { | ||||
| 	posts: StateHook<GetPostsAttributes[]>; | ||||
| 	editor: StateHook<EditorState>; | ||||
|   posts: StateHook<GetPostsAttributes[]>; | ||||
|   editor: StateHook<EditorState>; | ||||
| }; | ||||
| 
 | ||||
| export type PostTableProps = PostViewProps & { state: PostTableStateProps }; | ||||
| 
 | ||||
| export default function PostTable({ | ||||
| 	headings, | ||||
| 	posts, | ||||
| 	projects, | ||||
| 	actions, | ||||
| 	state, | ||||
|   headings, | ||||
|   posts, | ||||
|   projects, | ||||
|   actions, | ||||
|   state, | ||||
| }: PostTableProps) { | ||||
| 	// Define editor controls
 | ||||
| 	const editorControls = { | ||||
| 		closeEditor: () => { | ||||
| 			state.editor.setState({ | ||||
| 				isEditorOpen: false, | ||||
| 				editorPost: posts[0], | ||||
| 			}); | ||||
| 		}, | ||||
| 		showEditor: (entry: GetPostsAttributes) => { | ||||
| 			state.editor.setState({ | ||||
| 				isEditorOpen: true, | ||||
| 				editorPost: entry, | ||||
| 			}); | ||||
| 		}, | ||||
| 	}; | ||||
|   // Define editor controls
 | ||||
|   const editorControls = { | ||||
|     closeEditor: () => { | ||||
|       state.editor.setState({ | ||||
|         isEditorOpen: false, | ||||
|         editorPost: posts[0], | ||||
|       }); | ||||
|     }, | ||||
|     showEditor: (entry: GetPostsAttributes) => { | ||||
|       state.editor.setState({ | ||||
|         isEditorOpen: true, | ||||
|         editorPost: entry, | ||||
|       }); | ||||
|     }, | ||||
|   }; | ||||
| 
 | ||||
| 	const postActions = { | ||||
| 		deletePost: (entry: GetPostsAttributes) => { | ||||
| 			if (!entry.id) return; | ||||
| 			actions.deletePost(entry.id).then((actionResult) => { | ||||
| 				const result = handleActionResult(actionResult); | ||||
| 				if (!result) return; | ||||
| 				state.posts.state.splice(state.posts.state.indexOf(entry), 1); | ||||
| 				state.posts.setState([...state.posts.state]); | ||||
| 				toast.success("Removed Post:" + entry.id); | ||||
| 			}); | ||||
| 		}, | ||||
| 		refetch: () => { | ||||
| 			actions | ||||
| 				.getPosts() // Get Posts From Server
 | ||||
| 				.then((getPostsServerActionResult) => { | ||||
| 					// Handle Result and toast error on failure
 | ||||
| 					const result = handleActionResult( | ||||
| 						getPostsServerActionResult | ||||
| 					); | ||||
| 					// Set Posts state
 | ||||
| 					if (result) state.posts.setState(result); | ||||
| 				}); | ||||
| 		}, | ||||
| 		savePost: (e: Partial<Attributes<Post>>) => { | ||||
| 			actions | ||||
| 				.savePost({ | ||||
| 					id: e.id, | ||||
| 					content: e.content, | ||||
| 					title: e.title, | ||||
| 					project_id: e.project_id, | ||||
| 				}) | ||||
| 				.then((res) => handleActionResult(res)) | ||||
| 				.then(getPostsWithBucketsAndProject) | ||||
| 				.then((res) => { | ||||
| 					const result = handleActionResult(res); | ||||
| 					if (result) state.posts.setState(result); | ||||
| 				}) | ||||
| 				.then(editorControls.closeEditor) | ||||
| 				.then(postActions.refetch); | ||||
| 		}, | ||||
| 	}; | ||||
|   const postActions = { | ||||
|     deletePost: (entry: GetPostsAttributes) => { | ||||
|       if (!entry.id) return; | ||||
|       actions.deletePost(entry.id).then((actionResult) => { | ||||
|         const result = handleActionResult(actionResult); | ||||
|         if (!result) return; | ||||
|         state.posts.state.splice(state.posts.state.indexOf(entry), 1); | ||||
|         state.posts.setState([...state.posts.state]); | ||||
|         toast.success("Removed Post:" + entry.id); | ||||
|       }); | ||||
|     }, | ||||
|     refetch: () => { | ||||
|       actions | ||||
|         .getPosts() // Get Posts From Server
 | ||||
|         .then((getPostsServerActionResult) => { | ||||
|           // Handle Result and toast error on failure
 | ||||
|           const result = handleActionResult(getPostsServerActionResult); | ||||
|           // Set Posts state
 | ||||
|           if (result) state.posts.setState(result); | ||||
|         }); | ||||
|     }, | ||||
|     savePost: (e: Partial<Attributes<Post>>) => { | ||||
|       actions | ||||
|         .savePost({ | ||||
|           id: e.id, | ||||
|           content: e.content, | ||||
|           title: e.title, | ||||
|           project_id: e.project_id, | ||||
|         }) | ||||
|         .then((res) => handleActionResult(res)) | ||||
|         .then(getPostsWithBucketsAndProject) | ||||
|         .then((res) => { | ||||
|           const result = handleActionResult(res); | ||||
|           if (result) state.posts.setState(result); | ||||
|         }) | ||||
|         .then(editorControls.closeEditor) | ||||
|         .then(postActions.refetch); | ||||
|     }, | ||||
|   }; | ||||
| 
 | ||||
| 	return ( | ||||
| 		<EntityManagementTable entityName="Post" headings={headings}> | ||||
| 			{state.posts.state.map((post: GetPostsAttributes) => ( | ||||
| 				<React.Fragment key={`postrow-${post.id}`}> | ||||
| 					<tr> | ||||
| 						<th key={`rowheading-post-${post.id}`} scope="row"> | ||||
| 							{post.id} | ||||
| 						</th> | ||||
| 						<td key="title">{post.title}</td> | ||||
| 						<td key="content"> | ||||
| 							{!post.content | ||||
| 								? "No Content Found" | ||||
| 								: post.content.length < 255 | ||||
| 								? post.content | ||||
| 								: `${post.content.substring(0, 255)}...`} | ||||
| 						</td> | ||||
| 						<td key="project"> | ||||
| 							{projects.find((e) => { | ||||
| 								console.log(e.id); | ||||
| 								return e.id == post.project.id; | ||||
| 							})?.readableIdentifier || "uncategorized"} | ||||
| 						</td> | ||||
| 						<td key="createdAt">{post.createdAt?.toString()}</td> | ||||
| 						<td key="updatedAt">{post.updatedAt?.toString()}</td> | ||||
| 						<td key="edit"> | ||||
| 							{ | ||||
| 								<button | ||||
| 									key="editbutton" | ||||
| 									type="button" | ||||
| 									className="btn btn-primary" | ||||
| 									onClick={() => | ||||
| 										editorControls.showEditor(post) | ||||
| 									} | ||||
| 								> | ||||
| 									Edit | ||||
| 								</button> | ||||
| 							} | ||||
| 						</td> | ||||
| 						<td key="delete"> | ||||
| 							{ | ||||
| 								<button | ||||
| 									key="deletebutton" | ||||
| 									type="button" | ||||
| 									onClick={() => postActions.deletePost(post)} | ||||
| 									className="btn btn-danger" | ||||
| 								> | ||||
| 									{" "} | ||||
| 									Delete | ||||
| 								</button> | ||||
| 							} | ||||
| 						</td> | ||||
| 					</tr> | ||||
| 					<EditorRenderer | ||||
| 						headings={headings} | ||||
| 						editorPost={post} | ||||
| 						editorControls={editorControls} | ||||
| 						editorState={state.editor.state} | ||||
| 						callbacks={{ | ||||
| 							savePost: postActions.savePost, | ||||
| 							closeEditor: editorControls.closeEditor, | ||||
| 							refetch: postActions.refetch, | ||||
| 							uploadAttachment: () => {}, | ||||
| 						}} | ||||
| 						projects={projects} | ||||
| 					/> | ||||
| 				</React.Fragment> | ||||
| 			))} | ||||
| 		</EntityManagementTable> | ||||
| 	); | ||||
|   return ( | ||||
|     <EntityManagementTable entityName="Post" headings={headings}> | ||||
|       {state.posts.state.map((post: GetPostsAttributes) => ( | ||||
|         <React.Fragment key={`postrow-${post.id}`}> | ||||
|           <tr> | ||||
|             <th key={`rowheading-post-${post.id}`} scope="row"> | ||||
|               {post.id} | ||||
|             </th> | ||||
|             <td key="title">{post.title}</td> | ||||
|             <td key="content"> | ||||
|               {!post.content | ||||
|                 ? "No Content Found" | ||||
|                 : post.content.length < 255 | ||||
|                 ? post.content | ||||
|                 : `${post.content.substring(0, 255)}...`} | ||||
|             </td> | ||||
|             <td key="project"> | ||||
|               {projects.find((e) => { | ||||
|                 console.log(e.id); | ||||
|                 return e.id == post.project.id; | ||||
|               })?.readableIdentifier || "uncategorized"} | ||||
|             </td> | ||||
|             <td key="createdAt">{post.createdAt?.toString()}</td> | ||||
|             <td key="updatedAt">{post.updatedAt?.toString()}</td> | ||||
|             <td key="edit"> | ||||
|               { | ||||
|                 <button | ||||
|                   key="editbutton" | ||||
|                   type="button" | ||||
|                   className="btn btn-primary" | ||||
|                   onClick={() => editorControls.showEditor(post)} | ||||
|                 > | ||||
|                   Edit | ||||
|                 </button> | ||||
|               } | ||||
|             </td> | ||||
|             <td key="delete"> | ||||
|               { | ||||
|                 <button | ||||
|                   key="deletebutton" | ||||
|                   type="button" | ||||
|                   onClick={() => postActions.deletePost(post)} | ||||
|                   className="btn btn-danger" | ||||
|                 > | ||||
|                   {" "} | ||||
|                   Delete | ||||
|                 </button> | ||||
|               } | ||||
|             </td> | ||||
|           </tr> | ||||
|           <EditorRenderer | ||||
|             headings={headings} | ||||
|             editorPost={post} | ||||
|             editorControls={editorControls} | ||||
|             editorState={state.editor.state} | ||||
|             callbacks={{ | ||||
|               savePost: postActions.savePost, | ||||
|               closeEditor: editorControls.closeEditor, | ||||
|               refetch: postActions.refetch, | ||||
|               uploadAttachment: () => {}, | ||||
|             }} | ||||
|             projects={projects} | ||||
|           /> | ||||
|         </React.Fragment> | ||||
|       ))} | ||||
|     </EntityManagementTable> | ||||
|   ); | ||||
| } | ||||
|  | ||||
| @ -1,11 +1,10 @@ | ||||
| "use client"; | ||||
| 
 | ||||
| import PostTable, { | ||||
| 	PostTableStateProps, | ||||
|   PostTableStateProps, | ||||
| } from "@/components/client/admin/PostTable"; | ||||
| import { | ||||
| 	GetPostsAttributes, | ||||
| 	PostServerActions, | ||||
|   GetPostsAttributes, | ||||
|   PostServerActions, | ||||
| } from "@/app/lib/actions/entitymanagement/post/postActions"; | ||||
| import { EditorState } from "@/components/client/admin/PostEditor"; | ||||
| import { Project } from "@/models"; | ||||
| @ -14,38 +13,38 @@ import { Attributes } from "@sequelize/core"; | ||||
| import { parseStateHook } from "@/util/state"; | ||||
| 
 | ||||
| export type PostViewProps = { | ||||
| 	children?: ReactNode; | ||||
| 	headings: Array<string>; | ||||
| 	posts: GetPostsAttributes[]; | ||||
| 	projects: Attributes<Project>[]; | ||||
| 	actions: PostServerActions; | ||||
|   children?: ReactNode; | ||||
|   headings: Array<string>; | ||||
|   posts: GetPostsAttributes[]; | ||||
|   projects: Attributes<Project>[]; | ||||
|   actions: PostServerActions; | ||||
| }; | ||||
| 
 | ||||
| export function CPostView({ | ||||
| 	// children,
 | ||||
| 	headings, | ||||
| 	posts, | ||||
| 	projects, | ||||
| 	actions, | ||||
|   // children,
 | ||||
|   headings, | ||||
|   posts, | ||||
|   projects, | ||||
|   actions, | ||||
| }: PostViewProps) { | ||||
| 	// Init editor state. Make sure it is not opened by default
 | ||||
| 	const initEditorState: EditorState = { | ||||
| 		isEditorOpen: false, | ||||
| 		editorPost: posts[0], | ||||
| 	}; | ||||
| 	// Set up required state hooks
 | ||||
| 	const state: PostTableStateProps = { | ||||
|         posts: parseStateHook(useState(posts)), | ||||
|         editor: parseStateHook(useState(initEditorState)), | ||||
| 	}; | ||||
| 	// render out the post table
 | ||||
| 	return ( | ||||
| 		<PostTable | ||||
| 			headings={headings} | ||||
| 			posts={posts} | ||||
| 			projects={projects} | ||||
| 			actions={actions} | ||||
| 			state={state} | ||||
| 		></PostTable> | ||||
| 	); | ||||
|   // Init editor state. Make sure it is not opened by default
 | ||||
|   const initEditorState: EditorState = { | ||||
|     isEditorOpen: false, | ||||
|     editorPost: posts[0], | ||||
|   }; | ||||
|   // Set up required state hooks
 | ||||
|   const state: PostTableStateProps = { | ||||
|     posts: parseStateHook(useState(posts)), | ||||
|     editor: parseStateHook(useState(initEditorState)), | ||||
|   }; | ||||
|   // render out the post table
 | ||||
|   return ( | ||||
|     <PostTable | ||||
|       headings={headings} | ||||
|       posts={posts} | ||||
|       projects={projects} | ||||
|       actions={actions} | ||||
|       state={state} | ||||
|     ></PostTable> | ||||
|   ); | ||||
| } | ||||
|  | ||||
| @ -3,11 +3,11 @@ cache: "no-store"; | ||||
| 
 | ||||
| import { ReactNode } from "react"; | ||||
| import { | ||||
| 	deletePost, | ||||
| 	getPostsWithBucketsAndProject, | ||||
| 	updatePost, | ||||
| 	GetPostsAttributes, | ||||
| 	PostServerActions, | ||||
|   deletePost, | ||||
|   getPostsWithBucketsAndProject, | ||||
|   updatePost, | ||||
|   GetPostsAttributes, | ||||
|   PostServerActions, | ||||
| } from "@/app/lib/actions/entitymanagement/post/postActions"; | ||||
| import { getProjects } from "@/app/lib/actions/entitymanagement/post/projectActions"; | ||||
| import { Bucket, Project, Post, dbSync, Attachment } from "@/models"; | ||||
| @ -15,43 +15,43 @@ import { handleActionResult } from "@/app/lib/actions/clientActionHandler"; | ||||
| import { CPostView } from "@/components/views/admin/post"; | ||||
| 
 | ||||
| type Props = { | ||||
| 	children?: ReactNode; | ||||
|   children?: ReactNode; | ||||
| }; | ||||
| 
 | ||||
| export async function PostView(props: Props) { | ||||
| 	const sync = await dbSync; | ||||
|   const sync = await dbSync; | ||||
| 
 | ||||
| 	const headings: string[] = [ | ||||
| 		"#", | ||||
| 		"Title", | ||||
| 		"Content", | ||||
| 		"Project", | ||||
| 		"CreatedAt", | ||||
| 		"UpdatedAt", | ||||
| 	]; | ||||
|   const headings: string[] = [ | ||||
|     "#", | ||||
|     "Title", | ||||
|     "Content", | ||||
|     "Project", | ||||
|     "CreatedAt", | ||||
|     "UpdatedAt", | ||||
|   ]; | ||||
| 
 | ||||
| 	const posts: GetPostsAttributes[] | undefined = handleActionResult( | ||||
| 		await getPostsWithBucketsAndProject() | ||||
| 	); | ||||
| 	const projects = await Project.findAll().then((projects) => | ||||
| 		projects.map((project) => JSON.parse(JSON.stringify(project))) | ||||
| 	); | ||||
|   const posts: GetPostsAttributes[] | undefined = handleActionResult( | ||||
|     await getPostsWithBucketsAndProject() | ||||
|   ); | ||||
|   const projects = await Project.findAll().then((projects) => | ||||
|     projects.map((project) => JSON.parse(JSON.stringify(project))) | ||||
|   ); | ||||
| 
 | ||||
| 	return ( | ||||
| 		<div className="w-[100%] min-h-fit bg-gray-100 overflow-scroll"> | ||||
| 			<div className="w-[100%] m-auto"> | ||||
| 				<CPostView | ||||
| 					posts={posts ? posts : []} | ||||
| 					projects={projects} | ||||
| 					headings={headings} | ||||
| 					actions={{ | ||||
| 						deletePost: deletePost, | ||||
| 						getPosts: getPostsWithBucketsAndProject, | ||||
| 						getProjects: getProjects, | ||||
| 						savePost: updatePost | ||||
| 					}} | ||||
| 				/> | ||||
| 			</div> | ||||
| 		</div> | ||||
| 	); | ||||
|   return ( | ||||
|     <div className="w-[100%] min-h-fit bg-gray-100 overflow-scroll"> | ||||
|       <div className="w-[100%] m-auto"> | ||||
|         <CPostView | ||||
|           posts={posts ? posts : []} | ||||
|           projects={projects} | ||||
|           headings={headings} | ||||
|           actions={{ | ||||
|             deletePost: deletePost, | ||||
|             getPosts: getPostsWithBucketsAndProject, | ||||
|             getProjects: getProjects, | ||||
|             savePost: updatePost, | ||||
|           }} | ||||
|         /> | ||||
|       </div> | ||||
|     </div> | ||||
|   ); | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user