// import { title } from "@/components/primitives"; import { SetStateAction, useEffect, useState } from "react"; import { Button, Avatar, Dropdown, DropdownTrigger, DropdownMenu, DropdownItem, Input, Chip, Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, useDisclosure, Spinner, User, } from "@nextui-org/react"; import config from "../config"; import instance from "../security/http"; import { ChatBubbleOvalLeftEllipsisIcon, EllipsisHorizontalIcon, HandThumbsUpIcon, MagnifyingGlassIcon, PlusIcon, XMarkIcon, } from "../icons"; import { useNavigate } from "react-router-dom"; import { retrieveUserInformationById } from "../security/usersbyid"; import { number } from "yup"; // import UserPostImage from "../components/UserPostImage"; interface Post { title: string; postImage: Blob; content: string; tags: string; id: number; userId: number; } type User = { id: number; firstName: string; lastName: string; }; export default function CommunityPage() { const navigate = useNavigate(); const { isOpen, onOpen, onOpenChange } = useDisclosure(); const [selectedPost, setSelectedPost] = useState(null); const [communityList, setCommunityList] = useState([]); const [search, setSearch] = useState(""); // Search Function const [userInformation, setUserInformation] = useState>({}); let accessToken = localStorage.getItem("accessToken"); if (!accessToken) { return ( setTimeout(() => { navigate("/signin"); }, 1000) &&

User is not verified

Redirecting to sign-in page...

); } const getPosts = () => { instance.get(config.serverAddress + "/post").then((res) => { setCommunityList(res.data); }); }; useEffect(() => { getPosts(); }, []); useEffect(() => { const fetchUserInformation = async (userId: number) => { try { const user = await retrieveUserInformationById(userId); setUserInformation((prevMap) => ({ ...prevMap, [userId]: user, })); } catch (error) { console.error(error); } }; communityList.forEach((post) => { if (!userInformation[post.userId]) { fetchUserInformation(post.userId); } }); }, [communityList]); const onSearchChange = (e: { target: { value: SetStateAction } }) => { setSearch(e.target.value); }; const searchPosts = () => { instance .get(config.serverAddress + `/post?search=${search}`) .then((res) => { setCommunityList(res.data); }); }; const onSearchKeyDown = (e: { key: string }) => { if (e.key === "Enter") { searchPosts(); } }; const onClickSearch = () => { searchPosts(); }; const onClickClear = () => { setSearch(""); getPosts(); }; const handleDeleteClick = (post: Post) => { setSelectedPost(post); onOpen(); }; const handleDeleteConfirm = async () => { if (selectedPost) { try { await instance.delete( config.serverAddress + `/post/${selectedPost.id}` ); setCommunityList((prevList) => prevList.filter((post) => post.id !== selectedPost.id) ); onOpenChange(); } catch (error) { console.error("Error deleting post:", error); } } }; const handlePostClick = (id: number) => { navigate(`post/${id}`); }; return (

Community Forums

Socialize, share your experience or ask a question!

{communityList.map((post) => { return (
handlePostClick(post.id)} >
e.stopPropagation()}>

{post.title}

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

e.stopPropagation()} >
{ navigate(`edit/${post.id}`); }} > Edit handleDeleteClick(post)} > Delete

{post.content}

Image

Tag 1 Tag 2
); })}
} />
{(onClose) => ( <> Confirm Delete

Are you sure you want to delete this post?

)}
); }