|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useTransition } from "react"; |
| 4 | +import { useAppSelector } from "@/hooks/reduxHook"; |
| 5 | +import { togglePostLike } from "@/app/api/posts"; |
| 6 | +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; |
| 7 | +import { faHeart as solidHeart } from "@fortawesome/free-solid-svg-icons"; |
| 8 | +import { faHeart as regularHeart } from "@fortawesome/free-regular-svg-icons"; |
| 9 | +import styles from "./styles.module.scss"; |
| 10 | + |
| 11 | +interface PostLikeButtonProps { |
| 12 | + post: IPost; |
| 13 | + onLikeToggle?: (isLiked: boolean, newLikeCount: number) => void; |
| 14 | +} |
| 15 | + |
| 16 | +export const PostLikeButton = ({ post, onLikeToggle }: PostLikeButtonProps) => { |
| 17 | + const user = useAppSelector((state) => state.user); |
| 18 | + const [isPending, startTransition] = useTransition(); |
| 19 | + |
| 20 | + // likes가 배열인지 확인하고 파싱 |
| 21 | + const parseLikes = (likes: any): string[] => { |
| 22 | + if (Array.isArray(likes)) { |
| 23 | + return likes; |
| 24 | + } |
| 25 | + if (typeof likes === "string") { |
| 26 | + try { |
| 27 | + const parsed = JSON.parse(likes); |
| 28 | + return Array.isArray(parsed) ? parsed : []; |
| 29 | + } catch { |
| 30 | + return []; |
| 31 | + } |
| 32 | + } |
| 33 | + return []; |
| 34 | + }; |
| 35 | + |
| 36 | + const likesArray = parseLikes(post.likes); |
| 37 | + const [isLiked, setIsLiked] = useState( |
| 38 | + user.id ? likesArray.includes(user.id) : false |
| 39 | + ); |
| 40 | + const [likeCount, setLikeCount] = useState(likesArray.length); |
| 41 | + |
| 42 | + const handleLike = async () => { |
| 43 | + if (!user.id || !user.email) { |
| 44 | + alert("로그인이 필요합니다."); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + startTransition(async () => { |
| 49 | + try { |
| 50 | + const newIsLiked = await togglePostLike(post.index); |
| 51 | + setIsLiked(newIsLiked); |
| 52 | + const newCount = newIsLiked ? likeCount + 1 : likeCount - 1; |
| 53 | + setLikeCount(newCount); |
| 54 | + onLikeToggle?.(newIsLiked, newCount); |
| 55 | + } catch (error) { |
| 56 | + console.error("Failed to toggle like:", error); |
| 57 | + alert( |
| 58 | + error instanceof Error |
| 59 | + ? error.message |
| 60 | + : "좋아요 처리 중 오류가 발생했습니다." |
| 61 | + ); |
| 62 | + } |
| 63 | + }); |
| 64 | + }; |
| 65 | + |
| 66 | + if (!user.id || !user.email) { |
| 67 | + return null; // 로그인하지 않은 사용자에게는 버튼을 표시하지 않음 |
| 68 | + } |
| 69 | + |
| 70 | + return ( |
| 71 | + <button |
| 72 | + className={styles.like_button} |
| 73 | + onClick={handleLike} |
| 74 | + disabled={isPending} |
| 75 | + aria-label={isLiked ? "좋아요 취소" : "좋아요"} |
| 76 | + > |
| 77 | + <FontAwesomeIcon |
| 78 | + icon={isLiked ? solidHeart : regularHeart} |
| 79 | + className={`${styles.heart_icon} ${isLiked ? styles.liked : styles.not_liked}`} |
| 80 | + /> |
| 81 | + <span className={styles.like_count}>{likeCount}</span> |
| 82 | + </button> |
| 83 | + ); |
| 84 | +}; |
0 commit comments