@@ -236,7 +235,7 @@ const Cart: React.FC = () => {
) : (
cartItems.map((item: ITEM) => (
@@ -252,7 +251,7 @@ const Cart: React.FC = () => {
- {item.name}
+ {item.name} ({item.size}) {/* Display size */}
{item.desc}
@@ -262,10 +261,10 @@ const Cart: React.FC = () => {
- ${item.price}
+ ₹{item.price}
-
+
@@ -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)}
@@ -312,7 +308,7 @@ const Cart: React.FC = () => {
navigate("/home/shop/checkout");
}}
/>
-
Total: ${calculateTotal().toFixed(2)}
+ Total: ₹{calculateTotal().toFixed(2)}
)}
diff --git a/src/pages/Shop/ProductDetail.tsx b/src/pages/Shop/ProductDetail.tsx
index 35c069e..960af0f 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;
@@ -82,7 +89,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;
}
@@ -94,38 +101,50 @@ function ProductDetail() {
desc: data.desc,
quantity: data.qauntity || 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: CartItem) => 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,12 +157,12 @@ 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');
@@ -153,6 +172,8 @@ function ProductDetail() {
toast.error('Error adding product to cart');
}
};
+
+
return (
<>
@@ -182,9 +203,9 @@ function ProductDetail() {
{data.desc}
- ${data.price}
+ ₹{data.price}
${data.price + 89}
+ className="text-base font-normal text-gray-500 line-through dark:text-gray-400 ml-2">₹{data.price + 89}
7 in stock