diff --git a/app/contexts/productContext.js b/app/contexts/productContext.js index 2d133e2..47a8c94 100644 --- a/app/contexts/productContext.js +++ b/app/contexts/productContext.js @@ -27,7 +27,7 @@ export const ProductContextProvider = ({ children }) => { fetchCategory(); }, []); - const cartFn = async (variantId, designId, quantity) => { + const cartFn = async (variantId, designId, quantity, showToast = true) => { try{ const response = await authAxios.post('/orders/cart/manage_item/',{ @@ -35,7 +35,9 @@ export const ProductContextProvider = ({ children }) => { design: designId, quantity: quantity }) - toast.success('Modified Cart Successfully!') + if (showToast) { + toast.success('Modified Cart Successfully!') + } return response } catch(error){ diff --git a/components/shopping-cart/shoppingCart.jsx b/components/shopping-cart/shoppingCart.jsx index 7f46373..d3954c2 100644 --- a/components/shopping-cart/shoppingCart.jsx +++ b/components/shopping-cart/shoppingCart.jsx @@ -1,3 +1,4 @@ + "use client"; import React, { useContext, useEffect, useState } from "react"; import { Trash2, Plus } from "lucide-react"; @@ -43,12 +44,12 @@ const ShoppingCart = () => { }, []); - const handleQuantityChange = async (variantId, designId, quantityChange) => { - const response = await cartFn(variantId, designId, quantityChange); + const handleQuantityChange = async (variantId, designId, quantityChange, showToast = true) => { + const response = await cartFn(variantId, designId, quantityChange, showToast); console.log(response) if (response?.status == 200 || response?.status == 204) { setCartItems((prev) => { - if (!prev) return prev; + const updatedItems = prev[0].items.map((item) => { if (item.variant.id === variantId && item?.design?.id === designId) { @@ -86,6 +87,15 @@ const ShoppingCart = () => { } }; + const clearCart = async () => { + if (!cartItems || !cartItems[0]?.items?.length) return; + + for (const item of cartItems[0].items) { + await handleQuantityChange(item.variant.id, item.design?.id, -item.quantity, false); + } + toast.success('Cart cleared successfully!'); + }; + if (!cartItems) { return null; } @@ -125,7 +135,7 @@ const ShoppingCart = () => { payerEmail: order.payer.email_address, orderId: order.id, cartId: cartItems[0].id, - address: order.address, + address: order.address, }; const apiResponse = await authAxios.post("/orders/payment/", paymentData); @@ -138,14 +148,6 @@ const ShoppingCart = () => { } }; - if (!cartItems) { - return null; - } - - if (!cartItems[0]?.items?.length) { - return ; - } - return (
@@ -232,8 +234,8 @@ const ShoppingCart = () => { size={18} onClick={() => handleQuantityChange( - item.variant.id, - item.design.id, + item?.variant?.id, + item?.design?.id, -item.quantity ) } @@ -250,7 +252,10 @@ const ShoppingCart = () => { -