'use client' import { useState } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Textarea } from '@/components/ui/textarea' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Star, Upload, X } from 'lucide-react' import { toast } from 'sonner' import { useSession } from 'next-auth/react' import Image from 'next/image' interface ReviewFormProps { productId: string productName: string onSubmit?: (review: any) => void onCancel?: () => void disableImageUpload?: boolean existingReview?: { id: string rating: number title?: string comment?: string images: string[] } } export default function ReviewForm({ productId, productName, onSubmit, onCancel, disableImageUpload = false, existingReview }: ReviewFormProps) { const { data: session } = useSession() const [rating, setRating] = useState(existingReview?.rating || 5) const [title, setTitle] = useState(existingReview?.title || '') const [comment, setComment] = useState(existingReview?.comment || '') const [images, setImages] = useState(existingReview?.images || []) const [hoveredStar, setHoveredStar] = useState(0) const [isSubmitting, setIsSubmitting] = useState(false) const [uploadingImage, setUploadingImage] = useState(false) if (!session) { return (

Please sign in to write a review

) } const handleImageUpload = async (event: React.ChangeEvent) => { const file = event.target.files?.[0] if (!file) return if (images.length >= 5) { toast.error('Maximum 5 images allowed') return } setUploadingImage(true) try { const formData = new FormData() formData.append('file', file) formData.append('folder', 'reviews') const response = await fetch('/api/upload', { method: 'POST', body: formData }) if (response.ok) { const data = await response.json() setImages(prev => [...prev, data.url]) toast.success('Image uploaded successfully') } else { toast.error('Failed to upload image') } } catch (error) { toast.error('Failed to upload image') } finally { setUploadingImage(false) } } const removeImage = (index: number) => { setImages(prev => prev.filter((_, i) => i !== index)) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!rating) { toast.error('Please select a rating') return } setIsSubmitting(true) try { const url = existingReview ? `/api/reviews/${existingReview.id}` : '/api/reviews' const method = existingReview ? 'PUT' : 'POST' const response = await fetch(url, { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ productId, rating, title: title.trim() || undefined, comment: comment.trim() || undefined, images }) }) const data = await response.json() if (response.ok) { toast.success(data.message || 'Review submitted successfully') onSubmit?.(data.review) // Reset form if creating new review if (!existingReview) { setRating(5) setTitle('') setComment('') setImages([]) } } else { toast.error(data.error || 'Failed to submit review') } } catch (error) { toast.error('Failed to submit review') } finally { setIsSubmitting(false) } } const renderStars = () => { return Array.from({ length: 5 }, (_, index) => { const starValue = index + 1 return ( ) }) } return ( {existingReview ? 'Edit Your Review' : 'Write a Review'}

for {productName}

{/* Rating */}
{renderStars()} {rating}/5 stars
{/* Title */}
setTitle(e.target.value)} placeholder="Summarize your experience" maxLength={100} />

{title.length}/100 characters

{/* Comment */}