import { useParams, useNavigate } from "react-router-dom"; import { useCallback, useEffect, useState } from "react"; import instance from "../security/http"; import config from "../config"; import { Button, Avatar, Dropdown, DropdownTrigger, DropdownMenu, DropdownItem, Chip, Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, useDisclosure, Spinner, } from "@nextui-org/react"; import { ChatBubbleOvalLeftEllipsisIcon, EllipsisHorizontalIcon, HandThumbsUpIcon, ArrowUTurnLeftIcon, } from "../icons"; import { retrieveUserInformationById } from "../security/usersbyid"; import CommentInputModule from "../components/CommentInputModule"; import CommentsModule from "../components/CommentsModule"; interface Post { title: string; postImage: Blob; content: string; tags: string; id: string; userId: string; } type User = { id: string; firstName: string; lastName: string; }; const PostPage: React.FC = () => { const navigate = useNavigate(); const { id } = useParams<{ id: string }>(); const [post, setPost] = useState(null); const { isOpen, onOpen, onOpenChange } = useDisclosure(); const [selectedPost, setSelectedPost] = useState(null); const [userInformation, setUserInformation] = useState>({}); useEffect(() => { if (id) { instance.get(`${config.serverAddress}/post/${id}`) .then((res) => setPost(res.data)) .catch((error) => console.error("Error fetching post:", error)); } }, [id]); useEffect(() => { if (post) { const fetchUserInformation = async () => { try { const user = await retrieveUserInformationById(post.userId); setUserInformation((prevMap) => ({ ...prevMap, [post.userId]: user, })); } catch (error) { console.error(error); } }; fetchUserInformation(); } }, [post]); if (!post) { return (
); } const handleDeleteClick = (post: Post) => { setSelectedPost(post); onOpen(); }; const handleDeleteConfirm = async () => { if (selectedPost) { try { await instance.delete( config.serverAddress + `/post/${selectedPost.id}` ); onOpenChange(); } catch (error) { console.error("Error deleting post:", error); } } }; return (

{post.title}

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

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

{post.content}

Image

{/* {userInformation && ( )} */}
Tag 1 Tag 2
{(onClose) => ( <> Confirm Delete

Are you sure you want to delete this post?

)}
); }; export default PostPage;