From a53b7527cebd690ac33f5319e89b7cca8214884c Mon Sep 17 00:00:00 2001 From: Wind-Explorer Date: Sat, 3 Aug 2024 01:22:27 +0800 Subject: [PATCH] Posts optimization --- client/src/pages/CommunityPage.tsx | 46 +-- client/src/pages/CommunityPostManagement.tsx | 48 ++- server/routes/post.js | 344 ++++++++++--------- 3 files changed, 242 insertions(+), 196 deletions(-) diff --git a/client/src/pages/CommunityPage.tsx b/client/src/pages/CommunityPage.tsx index eea1572..51660bf 100644 --- a/client/src/pages/CommunityPage.tsx +++ b/client/src/pages/CommunityPage.tsx @@ -46,7 +46,6 @@ type User = { id: string; firstName: string; lastName: string; - }; export default function CommunityPage() { @@ -55,24 +54,30 @@ export default function CommunityPage() { const [selectedPost, setSelectedPost] = useState(null); const [communityList, setCommunityList] = useState([]); const [search, setSearch] = useState(""); // Search Function - const [userInformation, setUserInformation] = useState>({}); + const [userInformation, setUserInformation] = useState>( + {} + ); const [currentUserInfo, setCurrentUserInfo] = useState(null); - const [imageErrorFlags, setImageErrorFlags] = useState>({}); + const [imageErrorFlags, setImageErrorFlags] = useState< + Record + >({}); let accessToken = localStorage.getItem("accessToken"); if (!accessToken) { return ( setTimeout(() => { navigate("/signin"); - }, 1000) - && -
-
-

User is not verified

-

Redirecting to sign-in page...

- + }, 1000) && ( +
+
+

+ User is not verified +

+

Redirecting to sign-in page...

+ +
-
+ ) ); } @@ -165,7 +170,6 @@ export default function CommunityPage() { } }; - const handlePostClick = (id: string) => { navigate(`post/${id}`); }; @@ -202,17 +206,17 @@ export default function CommunityPage() { onClick={() => handlePostClick(post.id)} >
e.stopPropagation()}> - +

{post.title}

-

{userInformation[post.userId]?.firstName} {userInformation[post.userId]?.lastName}

+

+ {userInformation[post.userId]?.firstName}{" "} + {userInformation[post.userId]?.lastName} +

@@ -252,11 +256,13 @@ export default function CommunityPage() {

{post.content}

- {!imageErrorFlags[post.id] && post.postImage && post.postImage !== null && ( + {imageErrorFlags[post.id] ? null : (
- handleImageError(post.id)} /> + onError={() => handleImageError(post.id)} + />
)}
diff --git a/client/src/pages/CommunityPostManagement.tsx b/client/src/pages/CommunityPostManagement.tsx index 73e2f44..a960cd9 100644 --- a/client/src/pages/CommunityPostManagement.tsx +++ b/client/src/pages/CommunityPostManagement.tsx @@ -14,6 +14,7 @@ import { ModalHeader, ModalBody, ModalFooter, + Image, } from "@nextui-org/react"; import { useEffect, useState } from "react"; import instance from "../security/http"; @@ -35,7 +36,7 @@ export default function CommunityPostManagement() { { key: "title", label: "TITLE" }, { key: "content", label: "CONTENT" }, { key: "postImage", label: "IMAGE" }, - { key: "actions", label: "ACTIONS" } + { key: "actions", label: "ACTIONS" }, ]; const populateUserInformationList = () => { @@ -43,7 +44,7 @@ export default function CommunityPostManagement() { .get(`${config.serverAddress}/users/all`) .then((response) => { const users = response.data; - const usersWithProfilePictures = users.map((user: { id: string; }) => ({ + const usersWithProfilePictures = users.map((user: { id: string }) => ({ ...user, profilePicture: `${config.serverAddress}/users/profile-image/${user.id}`, })); @@ -86,12 +87,12 @@ export default function CommunityPostManagement() { userId: user?.id, ...post, ...user, - uniqueKey: `${post.id}-${user?.id}` + uniqueKey: `${post.id}-${user?.id}`, }; }); // Remove the duplicate 'id' field from merged data - const removeDuplicateId = mergedData.map(item => { + const removeDuplicateId = mergedData.map((item) => { const { id, ...rest } = item; return { ...rest }; }); @@ -114,7 +115,9 @@ export default function CommunityPostManagement() { const handleDeleteConfirm = async () => { if (postToDelete) { try { - await instance.delete(`${config.serverAddress}/post/${postToDelete.postId}`); + await instance.delete( + `${config.serverAddress}/post/${postToDelete.postId}` + ); populateCommunityPostList(); setIsModalOpen(false); } catch (error) { @@ -162,12 +165,13 @@ export default function CommunityPostManagement() { "No Image" ) ) : columnKey === "postImage" ? ( - value ? ( - - ) : ( - "No Image" - ) +
+ +

No image

+
) : columnKey === "actions" ? (
@@ -195,11 +199,8 @@ export default function CommunityPostManagement() {
) : ( -

- {value} -

+

{value}

)} - ); }} @@ -210,17 +211,28 @@ export default function CommunityPostManagement() {
)} - setIsModalOpen(false)} isDismissable={false} isKeyboardDismissDisabled={true}> + setIsModalOpen(false)} + isDismissable={false} + isKeyboardDismissDisabled={true} + > <> - {postToDelete?.title ? `DELETING POST: ${postToDelete.title}` : "Delete Post"} + {postToDelete?.title + ? `DELETING POST: ${postToDelete.title}` + : "Delete Post"}

Are you sure you want to delete this post?

-