diff --git a/client/src/actions/CartControl.jsx b/client/src/actions/CartControl.jsx index d53d6d1..9bbb554 100644 --- a/client/src/actions/CartControl.jsx +++ b/client/src/actions/CartControl.jsx @@ -37,10 +37,17 @@ export const CartProvider = ({ children }) => { ).filter(Boolean) ); }; - + + const decreaseCount = (item) => { + if (item.count > 1) { + addToCart({ ...item, count: item.count - 1 }); + } else { + removeFromCart(item); + } + }; return ( - + {children} ); diff --git a/client/src/pages/Cart.jsx b/client/src/pages/Cart.jsx index 7604d1a..5c4fd3f 100644 --- a/client/src/pages/Cart.jsx +++ b/client/src/pages/Cart.jsx @@ -1,60 +1,86 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { useCart } from '../actions/CartControl'; import Payment from '../components/Payment'; import { useNavigate } from 'react-router-dom'; +import {FaMinus} from "react-icons/fa" +import {FaPlus} from "react-icons/fa" +import {FaTrash} from "react-icons/fa" function Cart() { - const { cartItems, addToCart, removeFromCart, isLogin } = useCart(); + const { cartItems, addToCart,decreaseCount ,removeFromCart, isLogin } = useCart(); const navigate = useNavigate(); + + const [totalPrice, setTotalPrice] = useState(0); + + useEffect(() => { + const calculatedTotal = cartItems.reduce( + (acc, item) => acc + item.discountPrice * item.count, + 0 + ); + setTotalPrice(calculatedTotal); + }, [cartItems]); + console.log(cartItems); return ( -
-

Shopping Cart

- +
+

Shopping Cart

{cartItems.length === 0 ? (

Your cart is empty.

) : ( <> -
    - {cartItems.map((item) => ( - console.log(item), -
  • -
    -
    -

    {item.productName}

    -

    ₹ {item.discountPrice}

    -

    Quantity: {item.count}

    +
      + {cartItems.map((item) => ( +
    • + {item.productName} +
      +

      {item.productName}

      +

      + ₹ {item.discountPrice * item.count} +

      +
      +
      + +

      {item.count}

      +
      -
    -
    -
  • - ))} -
- - + + )} -
- ); +); } export default Cart;