|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { supabase } from '@/integrations/supabase/client'; |
| 3 | + |
| 4 | +interface ReviewFormProps { |
| 5 | + onClose: () => void; |
| 6 | +} |
| 7 | + |
| 8 | +const ReviewForm: React.FC<ReviewFormProps> = ({ onClose }) => { |
| 9 | + const [reviewName, setReviewName] = useState(''); |
| 10 | + const [reviewText, setReviewText] = useState(''); |
| 11 | + const [reviewStars, setReviewStars] = useState(5); |
| 12 | + |
| 13 | + const handleSubmitReview = async () => { |
| 14 | + if (!reviewName || !reviewText) { |
| 15 | + alert('Please fill all fields.'); |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + try { |
| 20 | + const { error } = await supabase |
| 21 | + .from('Review') |
| 22 | + .insert({ |
| 23 | + 'Customer Name': reviewName, |
| 24 | + 'Customer Review': reviewText, |
| 25 | + 'Rating': reviewStars, |
| 26 | + }); |
| 27 | + |
| 28 | + if (error) { |
| 29 | + console.error('Error submitting Review:', error); |
| 30 | + alert('Failed to submit review.'); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + alert('Review submitted successfully!'); |
| 35 | + onClose(); // close popup after success |
| 36 | + } catch (err) { |
| 37 | + console.error('Unexpected error:', err); |
| 38 | + alert('An unexpected error occurred.'); |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + return ( |
| 43 | + <div className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50"> |
| 44 | + <div className="bg-white rounded-xl shadow-xl w-full max-w-md p-6 relative"> |
| 45 | + <h2 className="text-2xl font-bold mb-4">Leave a Review</h2> |
| 46 | + <button |
| 47 | + onClick={onClose} |
| 48 | + className="absolute top-4 right-4 text-gray-600 hover:text-gray-900 text-2xl font-bold leading-none" |
| 49 | + > |
| 50 | + × |
| 51 | + </button> |
| 52 | + <input |
| 53 | + type="text" |
| 54 | + className="border border-gray-300 rounded p-3 w-full mb-4 focus:outline-blue-500" |
| 55 | + placeholder="Your Name" |
| 56 | + value={reviewName} |
| 57 | + onChange={(e) => setReviewName(e.target.value)} |
| 58 | + /> |
| 59 | + <textarea |
| 60 | + className="border border-gray-300 rounded p-3 resize-none w-full h-24 mb-4 focus:outline-blue-500" |
| 61 | + placeholder="Write your review here..." |
| 62 | + value={reviewText} |
| 63 | + onChange={(e) => setReviewText(e.target.value)} |
| 64 | + /> |
| 65 | + <div className="mb-4"> |
| 66 | + <label className="block mb-1 font-semibold">Rating</label> |
| 67 | + <div className="flex space-x-1"> |
| 68 | + {[1, 2, 3, 4, 5].map((star) => ( |
| 69 | + <svg |
| 70 | + key={star} |
| 71 | + onClick={() => setReviewStars(star)} |
| 72 | + xmlns="http://www.w3.org/2000/svg" |
| 73 | + fill={star <= reviewStars ? "yellow" : "none"} |
| 74 | + stroke="black" |
| 75 | + strokeWidth={0.5} |
| 76 | + className="w-6 h-6 cursor-pointer" |
| 77 | + viewBox="0 0 20 20" |
| 78 | + > |
| 79 | + <path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.286 3.904a1 1 0 00.95.69h4.105c.969 0 1.371 1.24.588 1.81l-3.3 2.4a1 1 0 00-.364 1.118l1.286 3.904c.3.92-.755 1.688-1.54 1.118l-3.3-2.4a1 1 0 00-1.176 0l-3.3 2.4c-.784.57-1.838-.197-1.54-1.118l1.286-3.904a1 1 0 00-.363-1.118L2.17 9.43c-.783-.57-.38-1.81.588-1.81h4.106a1 1 0 00.95-.69l1.286-3.904z" /> |
| 80 | + </svg> |
| 81 | + ))} |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + |
| 85 | + <button |
| 86 | + onClick={handleSubmitReview} |
| 87 | + className="w-full bg-green-600 hover:bg-green-700 text-white py-3 rounded-lg font-bold transition" |
| 88 | + > |
| 89 | + Submit Review |
| 90 | + </button> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +}; |
| 95 | + |
| 96 | +export default ReviewForm; |
0 commit comments