+
@@ -275,13 +274,10 @@ const Cart: React.FC = () => {
- ₹
- {item.quantity
- ? item.quantity * item.price
- : item.price}
+ ₹{item.quantity ? item.quantity * item.price : item.price}
- {cartItems.length == 0 ? (
+ {cartItems.length === 0 ? (
""
) : (
Apply Coupon
setCoupon(e.target.value)}
diff --git a/src/pages/Shop/ProductDetail.tsx b/src/pages/Shop/ProductDetail.tsx
index afc6d7a..727546a 100644
--- a/src/pages/Shop/ProductDetail.tsx
+++ b/src/pages/Shop/ProductDetail.tsx
@@ -82,7 +82,7 @@ function ProductDetail() {
size, //Include size
};
const addToCart = async () => {
- if (!size) { //added check for size
+ if (!size) { // added check for size
toast.error('Please select a size');
return;
}
@@ -92,40 +92,52 @@ function ProductDetail() {
image: data.image,
price: data.price,
desc: data.desc,
- quantity: data.qauntity || 1,
+ quantity: data.quantity || 1,
ratings: 5,
- size, //Include size
+ size, // Include size
};
-
+
// Attempt to fetch the user's cart
const { data: userCart, error: fetchError } = await supabase
.from('Cart')
.select('*')
.eq('username', userName)
.single();
-
+
if (fetchError && fetchError.code !== 'PGRST116') { // Ignore "No such record" error
console.error("Fetch error:", fetchError);
throw fetchError;
}
-
- console.log("Product added", product);
-
+
+ console.log("Product to be added", product);
+
if (userCart) {
- // If the cart exists, update it
- const updatedProducts = [...userCart.products, product];
-
+ // Check if the product already exists in the cart
+ const existingProductIndex = userCart.products.findIndex(
+ (item) => item.name === product.name && item.size === product.size
+ );
+
+ let updatedProducts;
+ if (existingProductIndex !== -1) {
+ // If the product exists, increase its quantity
+ updatedProducts = [...userCart.products];
+ updatedProducts[existingProductIndex].quantity += product.quantity;
+ } else {
+ // If the product does not exist, add it to the cart
+ updatedProducts = [...userCart.products, product];
+ }
+
const { error: updateError } = await supabase
.from('Cart')
.update({ products: updatedProducts })
.eq('username', userName);
-
+
if (updateError) {
console.error("Update error:", updateError);
throw updateError;
}
-
- console.log("Product added to cart:", product);
+
+ console.log("Product added/updated in cart:", product);
dispatch(addItem({ item: product }));
toast.success('Product added to cart');
} else {
@@ -138,21 +150,26 @@ function ProductDetail() {
products: [product],
},
]);
-
+
if (insertError) {
console.error("Insert error:", insertError);
throw insertError;
}
-
+
console.log("Product added to cart:", product);
dispatch(addItem({ item: product }));
toast.success('Product added to cart');
}
+
+ // Navigate the user to the cart
+ navigate('/home/shop/cart');
} catch (error) {
console.error("Error adding product to cart:", error);
toast.error('Error adding product to cart');
}
};
+
+
return (
<>
From 0c7d59bf373edb045d8c4efd09a5e4cc7876d686 Mon Sep 17 00:00:00 2001
From: Tanisha Shrivas
Date: Mon, 8 Jul 2024 11:25:33 +0530
Subject: [PATCH 4/5] Update ProductDetail.tsx
---
src/pages/Shop/ProductDetail.tsx | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/src/pages/Shop/ProductDetail.tsx b/src/pages/Shop/ProductDetail.tsx
index 727546a..0d85c0a 100644
--- a/src/pages/Shop/ProductDetail.tsx
+++ b/src/pages/Shop/ProductDetail.tsx
@@ -24,6 +24,13 @@ interface RatingItem {
checked?: boolean;
}
+interface CartItem {
+ name: string;
+ size: string;
+ quantity: number;
+ }
+
+
export interface UserState {
user: {
username: string;
@@ -92,7 +99,7 @@ function ProductDetail() {
image: data.image,
price: data.price,
desc: data.desc,
- quantity: data.quantity || 1,
+ quantity: data.qauntity || 1,
ratings: 5,
size, // Include size
};
@@ -114,8 +121,8 @@ function ProductDetail() {
if (userCart) {
// Check if the product already exists in the cart
const existingProductIndex = userCart.products.findIndex(
- (item) => item.name === product.name && item.size === product.size
- );
+ (item: CartItem) => item.name === product.name && item.size === product.size
+ );
let updatedProducts;
if (existingProductIndex !== -1) {
From 40d85c77c785d816390e2d49915edf1ddc02c20e Mon Sep 17 00:00:00 2001
From: Tanisha Shrivas
Date: Tue, 9 Jul 2024 13:42:09 +0530
Subject: [PATCH 5/5] Update ProductDetail.tsx
---
src/pages/Shop/ProductDetail.tsx | 3 ---
1 file changed, 3 deletions(-)
diff --git a/src/pages/Shop/ProductDetail.tsx b/src/pages/Shop/ProductDetail.tsx
index 0d85c0a..960af0f 100644
--- a/src/pages/Shop/ProductDetail.tsx
+++ b/src/pages/Shop/ProductDetail.tsx
@@ -167,9 +167,6 @@ function ProductDetail() {
dispatch(addItem({ item: product }));
toast.success('Product added to cart');
}
-
- // Navigate the user to the cart
- navigate('/home/shop/cart');
} catch (error) {
console.error("Error adding product to cart:", error);
toast.error('Error adding product to cart');