import { useState, useEffect } from 'react' import { useAuthStore } from '../store/authStore' import { API_BASE } from '../types' import { Star } from 'lucide-react' import { useTranslation } from 'react-i18next' interface Review { id: number rating: number comment: string | null customerName: string | null createdAt: string customer?: { pictureUrl: string | null } } export default function ProductReviews({ productId }: { productId: number }) { const { t } = useTranslation() const [reviews, setReviews] = useState([]) const [loading, setLoading] = useState(true) const { customer } = useAuthStore() const [rating, setRating] = useState(5) const [comment, setComment] = useState('') const [submitting, setSubmitting] = useState(false) const [filterRating, setFilterRating] = useState(null) const [visibleCount, setVisibleCount] = useState(3) const fetchReviews = () => { fetch(`${API_BASE}/reviews/product/${productId}`) .then(res => res.json()) .then(data => { if (Array.isArray(data)) setReviews(data) }) .catch(console.error) .finally(() => setLoading(false)) } useEffect(() => { fetchReviews() }, [productId]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!customer) return setSubmitting(true) try { const res = await fetch(`${API_BASE}/reviews`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ productId, rating, comment, customerId: customer.id, customerName: customer.displayName }) }) if (res.ok) { setComment('') setRating(5) fetchReviews() } } catch (err) { console.error(err) } finally { setSubmitting(false) } } const avgRating = reviews.length > 0 ? (reviews.reduce((acc, r) => acc + r.rating, 0) / reviews.length).toFixed(1) : '0.0' const filteredReviews = filterRating ? reviews.filter(r => r.rating === filterRating) : reviews const visibleReviews = filteredReviews.slice(0, visibleCount) return (
{/* Summary */}

{t('product.reviews')}

{avgRating}
{[1, 2, 3, 4, 5].map(star => ( ))}

{reviews.length} reviews

{/* List & Form */}
{customer ? (

Write a Review

{[1, 2, 3, 4, 5].map(star => ( ))}