Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 1x 1x 34x 35x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 1x 1x 1x 1x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 14x 14x 14x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 33x 35x 35x 35x 35x 35x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | 'use client';
import { useState } from 'react';
import { useSession } from 'next-auth/react';
import { StarRating } from '@/components/ui/StarRating';
import { Button } from '@/components/ui/Button';
import { Icon } from '@/components/ui/icons';
import { cn } from '@/lib/core';
import toast from 'react-hot-toast';
import Link from 'next/link';
import { useFunnelSteps } from '@/hooks/useFunnelTracking';
export interface ReviewFormProps {
productId: number;
onSuccess?: () => void;
className?: string;
}
/**
* ReviewForm - Form for submitting product reviews
*
* Features:
* - Interactive star rating selection
* - Character count for comment
* - Validation
* - Loading state
* - Authentication check
*
* @example
* ```tsx
* <ReviewForm
* productId={123}
* onSuccess={() => refetchReviews()}
* />
* ```
*/
export function ReviewForm({ productId, onSuccess, className }: ReviewFormProps) {
const { data: session, status } = useSession();
const [rating, setRating] = useState(0);
const [comment, setComment] = useState('');
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
const { trackStep } = useFunnelSteps();
const maxCommentLength = 2000;
const minCommentLength = 10;
// Show sign-in prompt if not authenticated
if (status === 'loading') {
return <ReviewFormSkeleton />;
}
if (!session) {
return (
<div className={cn('bg-gray-50 dark:bg-gray-800 rounded-lg p-6 text-center', className)}>
<Icon name="user" size={32} className="text-gray-400 mx-auto mb-3" />
<p className="text-gray-600 dark:text-gray-400 mb-4">Sign in to leave a review</p>
<Link href="/signin">
<Button variant="primary">Sign In</Button>
</Link>
</div>
);
}
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError(null);
// Validate rating
if (rating === 0) {
setError('Please select a rating');
return;
}
// Validate comment if provided
if (comment.trim() && comment.trim().length < minCommentLength) {
setError(`Comment must be at least ${minCommentLength} characters`);
return;
}
setSubmitting(true);
try {
const response = await fetch(`/api/products/${productId}/reviews`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
rating,
comment: comment.trim() || undefined})});
const data = await response.json();
if (data.success) {
toast.success('Review submitted successfully!');
// Track review_submitted for engagement funnel
trackStep('engagement', 'review_submitted', {
product_id: productId,
rating,
has_comment: !!comment.trim()});
setRating(0);
setComment('');
onSuccess?.();
} else {
setError(data.error || 'Failed to submit review');
if (data.error === 'You have already reviewed this product') {
toast.error('You have already reviewed this product');
}
}
} catch {
setError('Failed to submit review. Please try again.');
} finally {
setSubmitting(false);
}
};
const isValid = rating > 0 && (!comment.trim() || comment.trim().length >= minCommentLength);
return (
<form
onSubmit={handleSubmit}
className={cn('bg-gray-50 dark:bg-gray-800 rounded-lg p-6', className)}
>
<h3 className="font-semibold text-lg mb-4 dark:text-gray-100">Write a Review</h3>
{/* Error Message */}
{error && (
<div className="mb-4 p-3 bg-red-50 dark:bg-red-900/30 border border-red-200 dark:border-red-800 rounded-md text-red-700 dark:text-red-300 text-sm flex items-center gap-2">
<Icon name="alert-circle" size={16} />
{error}
</div>
)}
{/* Star Rating */}
<div className="mb-5">
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
Your Rating <span className="text-red-500">*</span>
</label>
<StarRating
rating={rating}
size="lg"
showCount={false}
interactive
onRatingChange={setRating}
/>
{rating > 0 && (
<p className="text-sm text-gray-500 dark:text-gray-400 mt-1">
{rating === 1 && 'Poor'}
{rating === 2 && 'Fair'}
{rating === 3 && 'Good'}
{rating === 4 && 'Very Good'}
{rating === 5 && 'Excellent'}
</p>
)}
</div>
{/* Comment */}
<div className="mb-5">
<label
htmlFor="review-comment"
className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"
>
Your Review (optional)
</label>
<textarea
id="review-comment"
value={comment}
onChange={(e) => setComment(e.target.value)}
placeholder="Share your experience with this product..."
className={cn(
'w-full border border-gray-300 dark:border-gray-600 rounded-md p-3 h-32 resize-none',
'bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100',
'focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent',
'placeholder:text-gray-400 dark:placeholder:text-gray-500'
)}
maxLength={maxCommentLength}
disabled={submitting}
/>
<div className="flex justify-between items-center mt-1">
<p className="text-xs text-gray-500 dark:text-gray-400">
{comment.trim().length > 0 && comment.trim().length < minCommentLength && (
<span className="text-amber-600 dark:text-amber-400">
Minimum {minCommentLength} characters required
</span>
)}
</p>
<p className="text-xs text-gray-500 dark:text-gray-400">
{comment.length}/{maxCommentLength}
</p>
</div>
</div>
{/* Submit Button */}
<Button
type="submit"
variant="primary"
disabled={submitting || !isValid}
className="w-full sm:w-auto"
>
{submitting ? (
<>
<Icon name="reload" size={16} className="animate-spin mr-2" />
Submitting...
</>
) : (
'Submit Review'
)}
</Button>
</form>
);
}
/**
* ReviewForm Loading Skeleton
*/
function ReviewFormSkeleton() {
return (
<div className="bg-gray-50 dark:bg-gray-800 rounded-lg p-6 animate-pulse">
<div className="h-6 w-32 bg-gray-200 dark:bg-gray-700 rounded mb-4" />
<div className="mb-5">
<div className="h-4 w-20 bg-gray-200 dark:bg-gray-700 rounded mb-2" />
<div className="h-8 w-40 bg-gray-200 dark:bg-gray-700 rounded" />
</div>
<div className="mb-5">
<div className="h-4 w-28 bg-gray-200 dark:bg-gray-700 rounded mb-2" />
<div className="h-32 w-full bg-gray-200 dark:bg-gray-700 rounded" />
</div>
<div className="h-10 w-32 bg-gray-200 dark:bg-gray-700 rounded" />
</div>
);
}
|