diff --git a/client/src/App.tsx b/client/src/App.tsx index 000d65a..be01cb8 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -29,6 +29,8 @@ import Ranking from "./pages/Ranking"; import ManageSchedulePage from "./pages/ManageSchedulePage"; import EditSchedulePage from "./pages/EditSchedulePage"; import CreateSchedulePage from "./pages/CreateSchedulePage"; +import CommunityPostManagement from "./pages/CommunityPostManagement"; + function App() { return ( @@ -105,6 +107,10 @@ function App() { } /> } /> + + + } /> + diff --git a/client/src/components/AdministratorNavigationPanel.tsx b/client/src/components/AdministratorNavigationPanel.tsx index 106475d..7bfbf2d 100644 --- a/client/src/components/AdministratorNavigationPanel.tsx +++ b/client/src/components/AdministratorNavigationPanel.tsx @@ -148,7 +148,7 @@ export default function AdministratorNavigationPanel() { } - onClickRef="#" + onClickRef="community-posts" /> ([]); + const [communityPostList, setCommunityPostList] = useState([]); + const [mergedList, setMergedList] = useState([]); + const [isModalOpen, setIsModalOpen] = useState(false); + const [postToDelete, setPostToDelete] = useState(null); + + const columns = [ + { key: "profilePicture", label: "PROFILE PICTURE" }, + { key: "firstName", label: "FIRST NAME" }, + { key: "lastName", label: "LAST NAME" }, + { key: "title", label: "TITLE" }, + { key: "content", label: "CONTENT" }, + { key: "postImage", label: "IMAGE" }, + { key: "actions", label: "ACTIONS" } + ]; + + const populateUserInformationList = () => { + instance + .get(`${config.serverAddress}/users/all`) + .then((response) => { + const users = response.data; + const usersWithProfilePictures = users.map((user: { id: string; }) => ({ + ...user, + profilePicture: `${config.serverAddress}/users/profile-image/${user.id}`, + })); + setUserInformationList(usersWithProfilePictures); + console.log("userwithpfp:", usersWithProfilePictures); // Ensure URLs are correct + }) + .catch((error) => { + popErrorToast(error); + }); + }; + + useEffect(() => { + populateUserInformationList(); + }, []); + + const populateCommunityPostList = () => { + instance + .get(`${config.serverAddress}/post`) + .then((response) => { + setCommunityPostList(response.data); + console.log(response.data); + }) + .catch((error) => { + popErrorToast(error); + }); + }; + + useEffect(() => { + populateCommunityPostList(); + }, []); + + useEffect(() => { + if (userInformationList.length > 0 && communityPostList.length > 0) { + const mergedData = communityPostList.map((post) => { + const user = userInformationList.find( + (user) => user.id === post.userId + ); + return { + postId: post.id, + userId: user?.id, + ...post, + ...user, + uniqueKey: `${post.id}-${user?.id}` + }; + }); + + // Remove the duplicate 'id' field from merged data + const removeDuplicateId = mergedData.map(item => { + const { id, ...rest } = item; + return { ...rest }; + }); + + setMergedList(removeDuplicateId); + console.log("merged data: ", removeDuplicateId); + } + }, [userInformationList, communityPostList]); + + const handleCopyID = (postId: string, title: string) => { + navigator.clipboard.writeText(postId); + popToast(title + "'s Post ID has been copied!", 1); + }; + + const handleDeleteClick = (item: any) => { + setPostToDelete(item); + setIsModalOpen(true); + }; + + const handleDeleteConfirm = async () => { + if (postToDelete) { + try { + await instance.delete(`${config.serverAddress}/post/${postToDelete.postId}`); + populateCommunityPostList(); + setIsModalOpen(false); + } catch (error) { + console.error("Error deleting post:", error); + } + } + }; + + return ( + + {mergedList.length > 0 && ( + + Community Post + + + {(column) => ( + {column.label} + )} + + + {(item) => ( + + {(columnKey) => ( + + {columnKey === "profilePicture" ? ( + item.profilePicture ? ( + + ) : ( + "No Image" + ) + ) : columnKey === "actions" ? ( + + + + handleCopyID(item.postId, item.title) + } + aria-label="Copy postId, title" + > + + + + + handleDeleteClick(item)} + > + + + + + ) : ( + + {getKeyValue(item, columnKey)} + + )} + + )} + + )} + + + + )} + + setIsModalOpen(false)} isDismissable={false} isKeyboardDismissDisabled={true}> + + <> + + {postToDelete?.title ? `DELETING POST: ${postToDelete.title}` : "Delete Post"} + + + Are you sure you want to delete this post? + + + setIsModalOpen(false)}> + Cancel + + + Delete + + + > + + + + ); +}
Community Post
+ {getKeyValue(item, columnKey)} +
Are you sure you want to delete this post?