import { useState, useEffect, useCallback } from "react"; import { useParams, useNavigate } from "react-router-dom"; import { postApi, commentApi } from "../api/apiService"; import { useAuth } from "../context/AuthContext"; import Layout from "../components/common/Layout"; import CommentItem from "../components/comment/CommentItem"; import CommentInput from "../components/comment/CommentInput"; const HeartIcon = ({ filled }) => ( ); const BackIcon = () => ( ); const PostDetailPage = () => { const { postId } = useParams(); const navigate = useNavigate(); const { user } = useAuth(); const [post, setPost] = useState(null); const [comments, setComments] = useState([]); const [isLoading, setIsLoading] = useState(true); const [isCommentsLoading, setIsCommentsLoading] = useState(true); const [error, setError] = useState(""); const [isLiked, setIsLiked] = useState(false); const [likesCount, setLikesCount] = useState(0); const [isEditing, setIsEditing] = useState(false); const [editContent, setEditContent] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); const fetchPostDetail = useCallback(async () => { try { setIsLoading(true); setError(""); const response = await postApi.getPost(postId); setPost(response.data); setIsLiked(response.data.isLiked || false); setLikesCount(response.data.likesCount || 0); setEditContent(response.data.content); } catch (error) { console.error("Error loading post detail:", error); setError("Error occurred while loading the post."); } finally { setIsLoading(false); } }, [postId]); const fetchComments = useCallback(async () => { try { setIsCommentsLoading(true); const response = await commentApi.getComments(postId); const items = response.data; // 응답이 배열임 setComments(items); } catch (error) { console.error("Error loading comments:", error); } finally { setIsCommentsLoading(false); } }, [postId]); useEffect(() => { fetchPostDetail(); }, [fetchPostDetail]); useEffect(() => { if (postId) { fetchComments(); } }, [fetchComments, postId]); const handleLikeToggle = async () => { if (!user) return; try { if (isLiked) { const response = await postApi.unlikePost(postId, user.username); setLikesCount(response.data.likesCount); setIsLiked(false); } else { const response = await postApi.likePost(postId, user.username); setLikesCount(response.data.likesCount); setIsLiked(true); } } catch (error) { console.error("Error toggling like:", error); } }; const handleGoBack = () => { navigate(-1); }; const handleCommentAdded = (newComment) => { setComments((prevComments) => [newComment, ...prevComments]); if (post) { setPost({ ...post, commentsCount: (post.commentsCount || 0) + 1, }); } }; const handleCommentDelete = (commentId) => { setComments((prevComments) => prevComments.filter((comment) => comment.id !== commentId) ); if (post) { setPost({ ...post, commentsCount: Math.max((post.commentsCount || 0) - 1, 0), }); } }; const handleCommentUpdate = (updatedComment) => { setComments((prevComments) => prevComments.map((comment) => comment.id === updatedComment.id ? updatedComment : comment ) ); }; const isAuthor = user && post && user.username === post.username; const handleEditClick = () => { setIsEditing(true); setEditContent(post.content); }; const handleEditCancel = () => { setIsEditing(false); setEditContent(post.content); }; const handleEditSave = async () => { if (!editContent.trim()) { return; } try { setIsSubmitting(true); const response = await postApi.updatePost( postId, editContent, user.username ); setPost(response.data); setIsEditing(false); } catch (error) { console.error("Error updating post:", error); alert("Error occurred while updating the post."); } finally { setIsSubmitting(false); } }; const handleDeleteClick = async () => { if (!window.confirm("Are you sure you want to delete this post?")) { return; } try { setIsSubmitting(true); await postApi.deletePost(postId, user.username); alert("Post deleted."); navigate("/"); } catch (error) { console.error("Error deleting post:", error); alert("Error occurred while deleting the post."); } finally { setIsSubmitting(false); } }; if (isLoading) { return (

Loading...

); } if (error || !post) { return (

{error || "Post not found."}

); } return (

Post Detail

{post.username}
{isEditing ? (