|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; |
| 4 | +import { ReviewListItem } from '@/types/product'; |
| 5 | + |
| 6 | +export interface ProductReviewsProps { |
| 7 | + id: number; |
| 8 | + order?: 'recent' | 'ratingDesc' | 'ratingAsc' | 'likeCount'; |
| 9 | +} |
| 10 | + |
| 11 | +const likeReview = async (reviewId: number): Promise<void> => { |
| 12 | + const res = await fetch(`https://mogazoa-api.vercel.app/13-3/reviews/${reviewId}/like`, { |
| 13 | + method: 'POST', |
| 14 | + }); |
| 15 | + if (!res.ok) throw new Error('Failed to like review'); |
| 16 | +}; |
| 17 | + |
| 18 | +const unlikeReview = async (reviewId: number): Promise<void> => { |
| 19 | + const res = await fetch(`https://mogazoa-api.vercel.app/13-3/reviews/${reviewId}/like`, { |
| 20 | + method: 'DELETE', |
| 21 | + }); |
| 22 | + if (!res.ok) throw new Error('Failed to unlike review'); |
| 23 | +}; |
| 24 | + |
| 25 | +export default function ProductReviews({ id, order = 'recent' }: ProductReviewsProps) { |
| 26 | + const queryClient = useQueryClient(); |
| 27 | + |
| 28 | + const { data: reviews, isLoading } = useQuery<ReviewListItem[]>({ |
| 29 | + queryKey: ['reviews', id, order], |
| 30 | + queryFn: async () => { |
| 31 | + const res = await fetch( |
| 32 | + `https://mogazoa-api.vercel.app/13-3/products/${id}/reviews?order=${order}`, |
| 33 | + ); |
| 34 | + const data = await res.json(); |
| 35 | + return data.list; |
| 36 | + }, |
| 37 | + enabled: Boolean(id), |
| 38 | + }); |
| 39 | + |
| 40 | + const likeReviewMutation = useMutation<void, Error, number>({ |
| 41 | + mutationFn: likeReview, |
| 42 | + onSuccess: () => { |
| 43 | + queryClient.invalidateQueries({ queryKey: ['reviews', id, order] }); |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + const unlikeReviewMutation = useMutation<void, Error, number>({ |
| 48 | + mutationFn: unlikeReview, |
| 49 | + onSuccess: () => { |
| 50 | + queryClient.invalidateQueries({ queryKey: ['reviews', id, order] }); |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + const handleLikeClick = (reviewId: number, isLiked: boolean) => { |
| 55 | + if (isLiked) { |
| 56 | + unlikeReviewMutation.mutate(reviewId); |
| 57 | + } else { |
| 58 | + likeReviewMutation.mutate(reviewId); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + if (isLoading) { |
| 63 | + return ( |
| 64 | + <section className="w-[940px] mx-auto min-h-[120px] flex items-center justify-center text-gray-400"> |
| 65 | + 로딩 중... |
| 66 | + </section> |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + if (!reviews || reviews.length === 0) { |
| 71 | + return ( |
| 72 | + <section className="w-[940px] mx-auto"> |
| 73 | + <div className="bg-black-400 rounded-xl p-8"> |
| 74 | + <div className="text-gray-400 text-center py-8">리뷰가 없습니다</div> |
| 75 | + </div> |
| 76 | + </section> |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + return ( |
| 81 | + <section className="w-[940px] mx-auto"> |
| 82 | + <div className="bg-black-400 rounded-xl p-8"> |
| 83 | + <div className="space-y-6"> |
| 84 | + {reviews.map((review) => ( |
| 85 | + <article key={review.id} className="border-b border-gray-200 pb-6 last:border-0"> |
| 86 | + <div className="flex items-center gap-4 mb-4"> |
| 87 | + <div className="w-10 h-10 rounded-full overflow-hidden"> |
| 88 | + <img |
| 89 | + src={review.user.image || '/placeholder-user.png'} |
| 90 | + alt={review.user.nickname} |
| 91 | + className="w-full h-full object-cover" |
| 92 | + /> |
| 93 | + </div> |
| 94 | + <div> |
| 95 | + <div className="font-medium">{review.user.nickname}</div> |
| 96 | + <div className="text-sm text-gray-500">{review.createdAt}</div> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + <div className="flex items-center gap-2 mb-2"> |
| 100 | + <span className="text-main-indigo">⭐️ {review.rating}</span> |
| 101 | + </div> |
| 102 | + <div className="text-gray-300 mb-4">{review.content}</div> |
| 103 | + {review.reviewImages.length > 0 && ( |
| 104 | + <div className="flex gap-2"> |
| 105 | + {review.reviewImages.map((image) => ( |
| 106 | + <div key={image.id} className="w-20 h-20 rounded-lg overflow-hidden"> |
| 107 | + <img |
| 108 | + src={image.source} |
| 109 | + alt="리뷰 이미지" |
| 110 | + className="w-full h-full object-cover" |
| 111 | + /> |
| 112 | + </div> |
| 113 | + ))} |
| 114 | + </div> |
| 115 | + )} |
| 116 | + <button |
| 117 | + className={`flex items-center gap-1 text-sm ${review.isLiked ? 'text-red-500' : 'text-gray-400'} hover:text-red-500`} |
| 118 | + onClick={() => handleLikeClick(review.id, review.isLiked)} |
| 119 | + disabled={likeReviewMutation.isPending || unlikeReviewMutation.isPending} |
| 120 | + > |
| 121 | + <span>❤️</span> {review.likeCount} |
| 122 | + </button> |
| 123 | + </article> |
| 124 | + ))} |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </section> |
| 128 | + ); |
| 129 | +} |
0 commit comments