325 lines
12 KiB
TypeScript
325 lines
12 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import { Star, X, Upload, CheckCircle2, AlertCircle, Loader2 } from 'lucide-react';
|
|
import { useAuthStore } from '@/store/authStore';
|
|
import { reviewService, ProductReviewItem } from '@/services/api/reviewService';
|
|
|
|
interface ReviewSubmissionModalProps {
|
|
productId: string;
|
|
productName: string;
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
onReviewSubmitted: (newReview: ProductReviewItem) => void;
|
|
}
|
|
|
|
const RATING_DESCRIPTIONS: Record<number, string> = {
|
|
1: 'Terrible - Very disappointed',
|
|
2: 'Poor - Not as expected',
|
|
3: 'Average - It was okay',
|
|
4: 'Good - Satisfied with quality',
|
|
5: 'Excellent - Highly recommended!',
|
|
};
|
|
|
|
export function ReviewSubmissionModal({
|
|
productId,
|
|
productName,
|
|
isOpen,
|
|
onClose,
|
|
onReviewSubmitted,
|
|
}: ReviewSubmissionModalProps) {
|
|
const customer = useAuthStore((s) => s.customer);
|
|
|
|
const [rating, setRating] = useState<number>(5);
|
|
const [hoverRating, setHoverRating] = useState<number | null>(null);
|
|
const [title, setTitle] = useState('');
|
|
const [comment, setComment] = useState('');
|
|
const [authorName, setAuthorName] = useState('');
|
|
const [authorEmail, setAuthorEmail] = useState('');
|
|
const [selectedFiles, setSelectedFiles] = useState<File[]>([]);
|
|
const [filePreviews, setFilePreviews] = useState<string[]>([]);
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
const [errorMsg, setErrorMsg] = useState<string | null>(null);
|
|
const [isSuccess, setIsSuccess] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (customer) {
|
|
const fullName = [customer.first_name, customer.last_name].filter(Boolean).join(' ');
|
|
if (fullName) setAuthorName(fullName);
|
|
if (customer.email) setAuthorEmail(customer.email);
|
|
}
|
|
}, [customer]);
|
|
|
|
if (!isOpen) return null;
|
|
|
|
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
const files = Array.from(e.target.files || []);
|
|
if (!files.length) return;
|
|
|
|
const remainingSlots = 3 - selectedFiles.length;
|
|
const toAdd = files.slice(0, remainingSlots);
|
|
|
|
setSelectedFiles((prev) => [...prev, ...toAdd]);
|
|
const newPreviews = toAdd.map((f) => URL.createObjectURL(f));
|
|
setFilePreviews((prev) => [...prev, ...newPreviews]);
|
|
};
|
|
|
|
const removeFile = (index: number) => {
|
|
setSelectedFiles((prev) => prev.filter((_, i) => i !== index));
|
|
setFilePreviews((prev) => {
|
|
URL.revokeObjectURL(prev[index]);
|
|
return prev.filter((_, i) => i !== index);
|
|
});
|
|
};
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setErrorMsg(null);
|
|
|
|
if (!title.trim()) {
|
|
setErrorMsg('Please enter a headline for your review.');
|
|
return;
|
|
}
|
|
if (!comment.trim() || comment.trim().length < 5) {
|
|
setErrorMsg('Please enter a detailed review comment (at least 5 characters).');
|
|
return;
|
|
}
|
|
if (!authorName.trim()) {
|
|
setErrorMsg('Please enter your name.');
|
|
return;
|
|
}
|
|
|
|
setIsSubmitting(true);
|
|
|
|
try {
|
|
// 1. Upload images if any
|
|
const imageUrls: string[] = [];
|
|
for (const file of selectedFiles) {
|
|
try {
|
|
const url = await reviewService.uploadReviewImage(file, productId);
|
|
if (url) imageUrls.push(url);
|
|
} catch (uploadErr) {
|
|
console.warn('Image upload error, continuing without this photo:', uploadErr);
|
|
}
|
|
}
|
|
|
|
// 2. Submit the review
|
|
const newReview = await reviewService.submitReview(productId, {
|
|
rating,
|
|
title: title.trim(),
|
|
comment: comment.trim(),
|
|
author_name: authorName.trim(),
|
|
author_email: authorEmail.trim() || undefined,
|
|
image_urls: imageUrls,
|
|
});
|
|
|
|
setIsSuccess(true);
|
|
setTimeout(() => {
|
|
onReviewSubmitted(newReview);
|
|
onClose();
|
|
setIsSuccess(false);
|
|
}, 1500);
|
|
} catch (err: any) {
|
|
setErrorMsg(err.message || 'Failed to submit review. Please try again.');
|
|
} finally {
|
|
setIsSubmitting(false);
|
|
}
|
|
};
|
|
|
|
const activeRating = hoverRating ?? rating;
|
|
|
|
return (
|
|
<div className="fixed inset-0 z-[120] flex items-center justify-center p-4 bg-black/60 backdrop-blur-xs">
|
|
<div className="relative w-full max-w-lg bg-white rounded-2xl shadow-2xl overflow-hidden border border-gray-100 animate-in fade-in zoom-in-95 duration-200">
|
|
{/* Header */}
|
|
<div className="flex items-center justify-between px-6 py-4 border-b border-gray-100 bg-[#fafbfc]">
|
|
<div>
|
|
<h3 className="text-base font-bold text-gray-900">Write a Review</h3>
|
|
<p className="text-xs text-gray-500 truncate max-w-xs">{productName}</p>
|
|
</div>
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
className="p-1.5 text-gray-400 hover:text-gray-700 rounded-full hover:bg-gray-100 cursor-pointer"
|
|
>
|
|
<X size={18} />
|
|
</button>
|
|
</div>
|
|
|
|
{/* Success Banner */}
|
|
{isSuccess ? (
|
|
<div className="p-8 text-center space-y-3">
|
|
<CheckCircle2 size={48} className="mx-auto text-emerald-500 animate-bounce" />
|
|
<h4 className="text-lg font-bold text-gray-900">Thank You!</h4>
|
|
<p className="text-sm text-gray-600">
|
|
Your review has been successfully submitted and verified.
|
|
</p>
|
|
</div>
|
|
) : (
|
|
/* Form */
|
|
<form onSubmit={handleSubmit} className="p-6 space-y-4 max-h-[80vh] overflow-y-auto">
|
|
{errorMsg && (
|
|
<div className="p-3 bg-red-50 border border-red-200 rounded-lg text-xs text-red-700 flex items-center gap-2">
|
|
<AlertCircle size={15} className="shrink-0 text-red-500" />
|
|
<span>{errorMsg}</span>
|
|
</div>
|
|
)}
|
|
|
|
{/* Overall Rating */}
|
|
<div className="space-y-1.5 text-center sm:text-left">
|
|
<label className="block text-xs font-bold text-gray-700 uppercase tracking-wider">
|
|
Overall Rating *
|
|
</label>
|
|
<div className="flex items-center gap-1.5 justify-center sm:justify-start">
|
|
{[1, 2, 3, 4, 5].map((star) => (
|
|
<button
|
|
key={star}
|
|
type="button"
|
|
onClick={() => setRating(star)}
|
|
onMouseEnter={() => setHoverRating(star)}
|
|
onMouseLeave={() => setHoverRating(null)}
|
|
className="p-1 text-gray-300 hover:scale-110 transition-transform cursor-pointer"
|
|
>
|
|
<Star
|
|
size={24}
|
|
className={
|
|
star <= activeRating
|
|
? 'fill-amber-400 text-amber-400'
|
|
: 'text-gray-200'
|
|
}
|
|
/>
|
|
</button>
|
|
))}
|
|
<span className="text-xs font-semibold text-amber-700 ml-2">
|
|
{RATING_DESCRIPTIONS[activeRating] || ''}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Review Title */}
|
|
<div>
|
|
<label className="block text-xs font-semibold text-gray-700 mb-1">
|
|
Headline / Summary *
|
|
</label>
|
|
<input
|
|
type="text"
|
|
value={title}
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
placeholder="e.g. Excellent display, perfectly fits my Samsung A80"
|
|
className="w-full px-3.5 py-2.5 border border-gray-300 rounded-lg text-sm text-gray-800 placeholder-gray-400 focus:outline-none focus:border-primary"
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
{/* Review Comment */}
|
|
<div>
|
|
<label className="block text-xs font-semibold text-gray-700 mb-1">
|
|
Detailed Review *
|
|
</label>
|
|
<textarea
|
|
value={comment}
|
|
onChange={(e) => setComment(e.target.value)}
|
|
rows={4}
|
|
placeholder="Share your experience: build quality, packaging, installation ease, and performance..."
|
|
className="w-full px-3.5 py-2.5 border border-gray-300 rounded-lg text-sm text-gray-800 placeholder-gray-400 focus:outline-none focus:border-primary"
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
{/* Name and Email */}
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
|
<div>
|
|
<label className="block text-xs font-semibold text-gray-700 mb-1">
|
|
Your Name *
|
|
</label>
|
|
<input
|
|
type="text"
|
|
value={authorName}
|
|
onChange={(e) => setAuthorName(e.target.value)}
|
|
placeholder="e.g. Adithiyan"
|
|
className="w-full px-3.5 py-2 border border-gray-300 rounded-lg text-sm text-gray-800 focus:outline-none focus:border-primary"
|
|
required
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-xs font-semibold text-gray-700 mb-1">
|
|
Email (Used for Verified Buyer check)
|
|
</label>
|
|
<input
|
|
type="email"
|
|
value={authorEmail}
|
|
onChange={(e) => setAuthorEmail(e.target.value)}
|
|
placeholder="name@example.com"
|
|
className="w-full px-3.5 py-2 border border-gray-300 rounded-lg text-sm text-gray-800 focus:outline-none focus:border-primary"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Photos Upload */}
|
|
<div>
|
|
<label className="block text-xs font-semibold text-gray-700 mb-1">
|
|
Attach Photos (Optional, up to 3)
|
|
</label>
|
|
<div className="flex flex-wrap items-center gap-2.5 pt-1">
|
|
{filePreviews.map((previewUrl, idx) => (
|
|
<div
|
|
key={idx}
|
|
className="relative w-16 h-16 rounded-lg overflow-hidden border border-gray-200 shadow-2xs"
|
|
>
|
|
<img src={previewUrl} alt="Preview" className="w-full h-full object-cover" />
|
|
<button
|
|
type="button"
|
|
onClick={() => removeFile(idx)}
|
|
className="absolute top-1 right-1 p-0.5 bg-black/60 text-white rounded-full hover:bg-black"
|
|
>
|
|
<X size={12} />
|
|
</button>
|
|
</div>
|
|
))}
|
|
|
|
{selectedFiles.length < 3 && (
|
|
<label className="w-16 h-16 border-2 border-dashed border-gray-300 hover:border-primary rounded-lg flex flex-col items-center justify-center text-gray-400 hover:text-primary cursor-pointer transition-colors">
|
|
<Upload size={16} />
|
|
<span className="text-[10px] font-semibold mt-0.5">Upload</span>
|
|
<input
|
|
type="file"
|
|
accept="image/jpeg,image/png,image/webp"
|
|
onChange={handleFileChange}
|
|
className="hidden"
|
|
/>
|
|
</label>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Footer Buttons */}
|
|
<div className="pt-3 border-t border-gray-100 flex items-center justify-end gap-3">
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
disabled={isSubmitting}
|
|
className="px-4 py-2 border border-gray-300 rounded-lg text-xs font-semibold text-gray-700 hover:bg-gray-50 cursor-pointer"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
disabled={isSubmitting}
|
|
className="px-6 py-2.5 bg-primary text-white text-xs font-bold rounded-lg hover:brightness-95 shadow-sm inline-flex items-center gap-2 cursor-pointer disabled:opacity-60"
|
|
>
|
|
{isSubmitting ? (
|
|
<>
|
|
<Loader2 size={14} className="animate-spin" />
|
|
<span>Submitting…</span>
|
|
</>
|
|
) : (
|
|
<span>Submit Review</span>
|
|
)}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|