441 lines
18 KiB
TypeScript
441 lines
18 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';
|
|
import GoogleAuthModal from '@/components/auth/GoogleAuthModal';
|
|
|
|
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!',
|
|
};
|
|
|
|
function GoogleIcon({ className = 'w-4 h-4' }: { className?: string }) {
|
|
return (
|
|
<svg className={className} viewBox="0 0 24 24" aria-hidden>
|
|
<path
|
|
fill="#4285F4"
|
|
d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
|
|
/>
|
|
<path
|
|
fill="#34A853"
|
|
d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
|
|
/>
|
|
<path
|
|
fill="#FBBC05"
|
|
d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.06H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.94l2.85-2.22.81-.63z"
|
|
/>
|
|
<path
|
|
fill="#EA4335"
|
|
d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.06l3.66 2.84c.87-2.6 3.3-4.52 6.16-4.52z"
|
|
/>
|
|
</svg>
|
|
);
|
|
}
|
|
|
|
export function ReviewSubmissionModal({
|
|
productId,
|
|
productName,
|
|
isOpen,
|
|
onClose,
|
|
onReviewSubmitted,
|
|
}: ReviewSubmissionModalProps) {
|
|
const customer = useAuthStore((s) => s.customer);
|
|
const bootstrapAuth = useAuthStore((s) => s.bootstrapAuth);
|
|
|
|
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);
|
|
const [showGoogleAuthModal, setShowGoogleAuthModal] = useState(false);
|
|
const [isGoogleAutofilled, setIsGoogleAutofilled] = useState(false);
|
|
|
|
// Restore session when modal opens
|
|
useEffect(() => {
|
|
if (isOpen) {
|
|
void bootstrapAuth();
|
|
}
|
|
}, [isOpen, bootstrapAuth]);
|
|
|
|
// Autofill name & email from logged in customer
|
|
useEffect(() => {
|
|
if (customer) {
|
|
const resolvedName =
|
|
(customer as any).name ||
|
|
(customer as any).full_name ||
|
|
[customer.first_name, customer.last_name].filter(Boolean).join(' ') ||
|
|
customer.first_name ||
|
|
'';
|
|
if (resolvedName) {
|
|
setAuthorName((prev) => (prev ? prev : resolvedName));
|
|
}
|
|
if (customer.email) {
|
|
setAuthorEmail((prev) => (prev ? prev : customer.email));
|
|
}
|
|
setIsGoogleAutofilled(true);
|
|
}
|
|
}, [customer, isOpen]);
|
|
|
|
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 submitted and is currently pending moderation. It will be published once approved by our team.
|
|
</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>
|
|
|
|
{/* Google Autofill Banner / Action */}
|
|
{customer?.email ? (
|
|
<div className="flex items-center justify-between p-2.5 rounded-xl bg-blue-50/80 border border-blue-200/80 text-xs">
|
|
<div className="flex items-center gap-2 min-w-0">
|
|
<div className="w-6 h-6 rounded-full bg-white shadow-xs border border-blue-200 flex items-center justify-center shrink-0">
|
|
<GoogleIcon className="w-3.5 h-3.5" />
|
|
</div>
|
|
<span className="truncate text-blue-950">
|
|
Auto-filled from Google: <strong className="font-semibold text-blue-900">{customer.email}</strong>
|
|
</span>
|
|
</div>
|
|
<span className="inline-flex items-center gap-1 text-[10px] font-bold uppercase tracking-wider text-emerald-700 bg-emerald-50 px-2 py-0.5 rounded-full border border-emerald-200 shrink-0">
|
|
<CheckCircle2 size={11} />
|
|
Verified Sync
|
|
</span>
|
|
</div>
|
|
) : (
|
|
<div className="flex items-center justify-between p-2.5 sm:p-3 rounded-xl bg-gradient-to-r from-gray-50 to-blue-50/40 border border-gray-200 hover:border-blue-300 transition-colors">
|
|
<div className="flex items-center gap-2.5 min-w-0">
|
|
<div className="w-7 h-7 rounded-full bg-white shadow-xs border border-gray-200 flex items-center justify-center shrink-0">
|
|
<GoogleIcon className="w-4 h-4" />
|
|
</div>
|
|
<div className="text-left">
|
|
<p className="text-xs font-semibold text-gray-800">Auto-fill with Google</p>
|
|
<p className="text-[11px] text-gray-500 hidden sm:block">1-click fetch name & verified buyer check</p>
|
|
</div>
|
|
</div>
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowGoogleAuthModal(true)}
|
|
className="inline-flex items-center gap-1.5 px-3 py-1.5 bg-white hover:bg-gray-50 text-gray-700 hover:text-gray-900 border border-gray-300 rounded-lg text-xs font-bold shadow-2xs transition-all cursor-pointer hover:border-gray-400 shrink-0"
|
|
>
|
|
<GoogleIcon className="w-3.5 h-3.5" />
|
|
<span>Continue with Google</span>
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
{/* Name and Email */}
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
|
<div>
|
|
<div className="flex items-center justify-between mb-1">
|
|
<label className="block text-xs font-semibold text-gray-700">
|
|
Your Name *
|
|
</label>
|
|
{isGoogleAutofilled && authorName && (
|
|
<span className="text-[10px] text-blue-600 font-medium inline-flex items-center gap-1">
|
|
<GoogleIcon className="w-2.5 h-2.5" /> Auto-filled
|
|
</span>
|
|
)}
|
|
</div>
|
|
<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>
|
|
<div className="flex items-center justify-between mb-1">
|
|
<label className="block text-xs font-semibold text-gray-700 truncate">
|
|
Email (Used for Verified Buyer check)
|
|
</label>
|
|
{isGoogleAutofilled && authorEmail && (
|
|
<span className="text-[10px] text-emerald-600 font-medium inline-flex items-center gap-1">
|
|
<CheckCircle2 size={10} /> Verified
|
|
</span>
|
|
)}
|
|
</div>
|
|
<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>
|
|
|
|
{/* Google Auth Modal for 1-Click Auto-fill */}
|
|
<GoogleAuthModal
|
|
isOpen={showGoogleAuthModal}
|
|
onClose={() => setShowGoogleAuthModal(false)}
|
|
onSuccess={(data) => {
|
|
const resolvedName =
|
|
[data.first_name, data.last_name].filter(Boolean).join(' ') ||
|
|
data.first_name ||
|
|
data.name ||
|
|
data.email?.split('@')[0] ||
|
|
'';
|
|
if (resolvedName) setAuthorName(resolvedName);
|
|
if (data.email) setAuthorEmail(data.email);
|
|
setIsGoogleAutofilled(true);
|
|
setShowGoogleAuthModal(false);
|
|
}}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
|