From 2e393c2d9cafb3fc72d8785937b438a935540bea Mon Sep 17 00:00:00 2001 From: Moataz Nabil Date: Tue, 3 Dec 2024 22:01:28 +0100 Subject: [PATCH] fix add review --- src/components/product/AddReview.tsx | 13 +++++- src/components/product/ProductReviews.tsx | 35 ++++++++++++--- src/database/db.ts | 40 +++++++++++++---- src/store/useStore.ts | 55 +++++++++++++---------- 4 files changed, 104 insertions(+), 39 deletions(-) diff --git a/src/components/product/AddReview.tsx b/src/components/product/AddReview.tsx index 58c35ce..4127a36 100644 --- a/src/components/product/AddReview.tsx +++ b/src/components/product/AddReview.tsx @@ -12,15 +12,17 @@ export const AddReview: React.FC = ({ productId, onReviewSubmit const [rating, setRating] = useState(5); const [comment, setComment] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); + const [error, setError] = useState(null); const { auth, addReview } = useStore(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); + if (!auth.user) return; + setIsSubmitting(true); + setError(null); try { - if (!auth.user) throw new Error('User not authenticated'); - await addReview({ productId, userId: auth.user.id, @@ -34,6 +36,7 @@ export const AddReview: React.FC = ({ productId, onReviewSubmit setRating(5); onReviewSubmit(); } catch (error) { + setError('Failed to submit review. Please try again.'); console.error('Failed to submit review:', error); } finally { setIsSubmitting(false); @@ -79,6 +82,12 @@ export const AddReview: React.FC = ({ productId, onReviewSubmit /> + {error && ( +

+ {error} +

+ )} +