import { getKeyValue, Table, TableBody, TableCell, TableColumn, TableHeader, TableRow, Button, Tooltip, Avatar, Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, } from "@nextui-org/react"; import { useEffect, useState } from "react"; import instance from "../security/http"; import config from "../config"; import { popErrorToast, popToast } from "../utilities"; import { ClipboardDocumentIcon, TrashDeleteIcon } from "../icons"; export default function CommunityPostManagement() { const [userInformationList, setUserInformationList] = useState([]); 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); } } }; // Function to safely render item values const renderCellValue = (item: any, columnKey: any) => { const value = getKeyValue(item, columnKey); if (typeof value === "object" && value !== null) { return JSON.stringify(value); // Convert object to string } return value; }; return (
{mergedList.length > 0 && (

Community Post

{(column) => ( {column.label} )} {(item) => ( {(columnKey) => { const value = renderCellValue(item, columnKey); return ( {columnKey === "profilePicture" ? ( item.profilePicture ? ( ) : ( "No Image" ) ) : columnKey === "postImage" ? ( value ? ( ) : ( "No Image" ) ) : columnKey === "actions" ? (
) : (

{value}

)}
); }}
)}
)} setIsModalOpen(false)} isDismissable={false} isKeyboardDismissDisabled={true}> <> {postToDelete?.title ? `DELETING POST: ${postToDelete.title}` : "Delete Post"}

Are you sure you want to delete this post?

); }