import React, { useState } from 'react'; import { likePost, unlikePost } from '../services/api'; import Button from './Button'; const PostCard = ({ post, onDeleteClick, refreshPosts, userName = "사용자" }) => { const [isLiked, setIsLiked] = useState(false); const [likeCount, setLikeCount] = useState(post.likeCount); const [isLoading, setIsLoading] = useState(false); // 좋아요 토글 핸들러 const handleLikeToggle = async () => { if (isLoading) return; setIsLoading(true); try { if (isLiked) { await unlikePost(post.id, { userName }); setLikeCount(prev => Math.max(0, prev - 1)); } else { await likePost(post.id, { userName }); setLikeCount(prev => prev + 1); } setIsLiked(!isLiked); } catch (error) { console.error('좋아요 처리 중 오류 발생:', error); } finally { setIsLoading(false); } }; // 날짜 포맷팅 const formatDate = (dateString) => { const options = { year: 'numeric', month: 'short', day: 'numeric' }; return new Date(dateString).toLocaleDateString('ko-KR', options); }; return (
{post.imageUrl && (
{`${post.userName}의
)}
{post.userName} {formatDate(post.createdAt)}

{post.content}

{post.userName === userName && (
)}
); }; export default PostCard;