'use client'; import React, { useState, useEffect } from 'react'; import { ChevronDown, MessageSquare, Loader2 } from 'lucide-react'; import { reviewService, ProductReviewItem, ProductReviewSummaryResponse } from '@/services/api/reviewService'; import { ReviewSummaryBreakdown } from './ReviewSummaryBreakdown'; import { ReviewCard } from './ReviewCard'; import { ReviewSubmissionModal } from './ReviewSubmissionModal'; interface ProductReviewsSectionProps { productId: string; productName: string; onReviewsCountChange?: (count: number) => void; } export function ProductReviewsSection({ productId, productName, onReviewsCountChange, }: ProductReviewsSectionProps) { const [data, setData] = useState(null); const [isLoading, setIsLoading] = useState(true); const [sort, setSort] = useState('newest'); const [page, setPage] = useState(1); const [isModalOpen, setIsModalOpen] = useState(false); const [isLoadingMore, setIsLoadingMore] = useState(false); useEffect(() => { setIsLoading(true); reviewService .getProductReviews(productId, 1, 10, sort) .then((res) => { setData(res); if (onReviewsCountChange) onReviewsCountChange(res.total_reviews); }) .finally(() => { setIsLoading(false); }); }, [productId, sort]); const handleLoadMore = () => { if (!data) return; setIsLoadingMore(true); const nextPage = page + 1; reviewService .getProductReviews(productId, nextPage, 10, sort) .then((res) => { setData((prev) => { if (!prev) return res; return { ...res, reviews: [...prev.reviews, ...res.reviews], }; }); setPage(nextPage); }) .finally(() => { setIsLoadingMore(false); }); }; const handleReviewSubmitted = (newReview: ProductReviewItem) => { setData((prev) => { const existingReviews = prev ? prev.reviews : []; const updatedReviews = [newReview, ...existingReviews]; const newTotal = (prev?.total_reviews || 0) + 1; const starKey = String(newReview.rating); const newDist = { ...(prev?.star_distribution || {}) }; newDist[starKey] = (newDist[starKey] || 0) + 1; if (onReviewsCountChange) onReviewsCountChange(newTotal); return { average_rating: prev ? ((prev.average_rating * prev.total_reviews) + newReview.rating) / newTotal : newReview.rating, total_reviews: newTotal, star_distribution: newDist, verified_count: (prev?.verified_count || 0) + (newReview.verified_buyer ? 1 : 0), page: 1, limit: 10, reviews: updatedReviews, }; }); }; if (isLoading) { return (

Loading customer reviews...

); } const reviews = data?.reviews || []; const totalReviews = data?.total_reviews || 0; const averageRating = data?.average_rating || 0; const verifiedCount = data?.verified_count || 0; const starDistribution = data?.star_distribution || { '5': 0, '4': 0, '3': 0, '2': 0, '1': 0 }; const hasMore = reviews.length < totalReviews; return (
{/* 1. Summary Breakdown */} setIsModalOpen(true)} /> {/* 2. Reviews Header with Sorting */}

Customer Feedback ({totalReviews})

{reviews.length > 0 && (
Sort by:
)}
{/* 3. Review Cards List */} {reviews.length === 0 ? (

No reviews yet

Have you purchased this product? Be the first to share your thoughts with other customers.

) : (
{reviews.map((r) => ( ))}
)} {/* 4. Load More Button */} {hasMore && (
)} {/* 5. Submission Modal */} setIsModalOpen(false)} onReviewSubmitted={handleReviewSubmitted} />
); }