diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx index 33a8e22..fbd6fb9 100644 --- a/src/components/Navbar.tsx +++ b/src/components/Navbar.tsx @@ -194,14 +194,14 @@ function Navbar() { {_.name}

- Price: ${_.price} + Price: ₹{_.price}

))} - subtotal ${total} + subtotal ₹{total} { @@ -34,7 +35,7 @@ const Cart: React.FC = () => { name: "DISCOUNT10", discount: 10, }; - + useEffect(() => { const fetchCartData = async () => { try { @@ -69,7 +70,7 @@ const Cart: React.FC = () => { window.scrollTo(0, 0); }, []); - const handleRemoveItem = async (itemName: string) => { + const handleRemoveItem = async (itemName: string, itemSize: string) => { try { const { data, error } = await supabase .from("Cart") @@ -83,7 +84,7 @@ const Cart: React.FC = () => { if (data && data.products) { const updatedProducts = data.products.filter( - (item: ITEM) => item.name !== itemName + (item: ITEM) => !(item.name === itemName && item.size === itemSize) ); const { error: updateError } = await supabase @@ -94,7 +95,7 @@ const Cart: React.FC = () => { if (updateError) { throw updateError; } - toast.success("item removed success"); + toast.success("Item removed successfully"); setCartItems(updatedProducts); } } catch (error) { @@ -102,8 +103,6 @@ const Cart: React.FC = () => { } }; - // quantity handle here - const handleQuantityChange = async ( itemName: string, newQuantity: number @@ -160,7 +159,7 @@ const Cart: React.FC = () => {
- {cartItems.length == 0 ? ( + {cartItems.length === 0 ? ( "" ) : (
@@ -174,12 +173,12 @@ const Cart: React.FC = () => { Price
-
+

Quantity

-
+

Subtotal

@@ -210,10 +209,10 @@ const Cart: React.FC = () => {
-
+
-
+
@@ -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