From 53df08538f9708f311922455429eff364ab77cff Mon Sep 17 00:00:00 2001 From: Khushi Kalra Date: Sat, 10 Aug 2024 13:38:26 +0530 Subject: [PATCH] Update ProductDetail.tsx --- src/pages/Shop/ProductDetail.tsx | 155 ++++++++++++++++++++++++++++--- 1 file changed, 142 insertions(+), 13 deletions(-) diff --git a/src/pages/Shop/ProductDetail.tsx b/src/pages/Shop/ProductDetail.tsx index 960af0f..bfc051c 100644 --- a/src/pages/Shop/ProductDetail.tsx +++ b/src/pages/Shop/ProductDetail.tsx @@ -12,6 +12,7 @@ import { useDispatch, useSelector } from "react-redux"; import { supabase } from "../../utils/client"; export interface Data { + id: string; name: string; image: string; price: number; @@ -28,8 +29,7 @@ interface CartItem { name: string; size: string; quantity: number; - } - +} export interface UserState { user: { @@ -60,6 +60,9 @@ function ProductDetail() { const [filledStars, setFilledStars] = useState(0); const availableSizes = ["XS", "S", "M", "L", "XL"]; const [size, setSize] = useState(""); + const [reviewText, setReviewText] = useState(""); + const [reviewImage, setReviewImage] = useState(null); + const [reviews, setReviews] = useState([]); const navigate = useNavigate(); const handleRatingChange = (index: number) => { @@ -75,26 +78,31 @@ function ProductDetail() { useEffect(() => { window.scrollTo(0, 0); + fetchReviews(); }, []); const dispatch = useDispatch(); const userName = useSelector((state: RootState) => state.auth.user.username); + const product = { + id: data.id, name: data.name, image: data.image, price: data.price, desc: data.desc, quantity: data.qauntity || 1, ratings: 5, - size, //Include size + size, }; + const addToCart = async () => { - if (!size) { // added check for size + if (!size) { toast.error('Please select a size'); return; } try { const product = { + id: data.id, name: data.name, image: data.image, price: data.price, @@ -172,9 +180,63 @@ function ProductDetail() { toast.error('Error adding product to cart'); } }; - - + // Function to handle review submission + const handleReviewSubmit = async (event: React.FormEvent) => { + event.preventDefault(); + + try { + const { error } = await supabase + .from('Reviews') + .insert([ + { + username: userName, + product_id: data.id, + review_text: reviewText, + rating: filledStars, + image_url: reviewImage, + }, + ]); + + if (error) { + console.error("Insert error:", error); + throw error; + } + + setReviewText(""); + setReviewImage(null); + fetchReviews(); + toast.success('Review submitted'); + } catch (error) { + console.error("Error submitting review:", error); + toast.error('Error submitting review'); + } + }; + + const handleImageChange = (event: React.ChangeEvent) => { + if (event.target.files && event.target.files[0]) { + setReviewImage(URL.createObjectURL(event.target.files[0])); + } + }; + + const fetchReviews = async () => { + try { + const { data: reviewsData, error } = await supabase + .from('Reviews') + .select('*') + + if (error) { + console.error("Fetch error:", error); + throw error; + } + + setReviews(reviewsData); + } catch (error) { + console.error("Error fetching reviews:", error); + toast.error('Error fetching reviews'); + } + }; + return ( <>
@@ -252,13 +314,80 @@ function ProductDetail() {
+
+
+

Customer Reviews

+
+ + +
@@ -266,7 +395,7 @@ function ProductDetail() { - ) + ); } export default ProductDetail;