From a3d45c118e22fa1643d4e66c6fc69938ef2993ab Mon Sep 17 00:00:00 2001 From: JiyaGupta-cs <140608790+JiyaGupta-cs@users.noreply.github.com> Date: Wed, 21 Feb 2024 12:25:58 +0530 Subject: [PATCH] Revert "Fix : Implement Dynamic Price Calculation in CartItems" --- src/Components/CartItems/CartItems.css | 1 - src/Components/CartItems/CartItems.jsx | 27 ++++++++++------------- src/Context/ShopContext.jsx | 30 ++++++-------------------- 3 files changed, 18 insertions(+), 40 deletions(-) diff --git a/src/Components/CartItems/CartItems.css b/src/Components/CartItems/CartItems.css index 535620e..17690a6 100644 --- a/src/Components/CartItems/CartItems.css +++ b/src/Components/CartItems/CartItems.css @@ -49,7 +49,6 @@ border: 2px solid #ebebeb; background: #fff; text-align: center; - color: rgb(5, 5, 5); } .cartitems-down{ diff --git a/src/Components/CartItems/CartItems.jsx b/src/Components/CartItems/CartItems.jsx index a6fa5bf..451fa74 100644 --- a/src/Components/CartItems/CartItems.jsx +++ b/src/Components/CartItems/CartItems.jsx @@ -2,15 +2,10 @@ import React, { useContext } from "react"; import "./CartItems.css"; import { ShopContext } from "../../Context/ShopContext"; import remove_icon from "../Assets/cart_cross_icon.png"; + const CartItems = () => { - const { cartItems, removeFromCart, getTotalCartAmount, theme } = + const { all_product, cartItems, removeFromCart, getTotalCartAmount,theme } = useContext(ShopContext); - - // Function to calculate the total price for each item considering the quantity - const calculateTotalPrice = (item) => { - return item.new_price * item.quantity; - }; - return (
@@ -23,21 +18,21 @@ const CartItems = () => {

Remove


- {cartItems.map((item) => { + {cartItems.map((e) => { return ( -
+
- -

{item.name}

-

${item.new_price}

-

{item.size}

- -

${calculateTotalPrice(item)}

{/* Display total price for each item */} + +

{e.name}

+

${e.new_price}

+

{e?.size}

+ +

${e.new_price}

{ - removeFromCart(item.id); + removeFromCart(e.id); }} alt="" /> diff --git a/src/Context/ShopContext.jsx b/src/Context/ShopContext.jsx index ed5db5a..4be3db9 100644 --- a/src/Context/ShopContext.jsx +++ b/src/Context/ShopContext.jsx @@ -7,36 +7,20 @@ const ShopContextProvider = (props) => { const [cartItems, setcartItems] = useState([]); const [theme,setTheme]=useState("dark"); const addToCart = (itemId, size, quantity) => { - const existingCartItemIndex = cartItems.findIndex(item => item.id === itemId && item.size === size); - - if (existingCartItemIndex !== -1) { - const updatedCartItems = cartItems.map((item, index) => { - if (index === existingCartItemIndex) { - return { - ...item, - quantity: item.quantity + quantity - }; - } - return item; - }); - setcartItems(updatedCartItems); - } else { - const cartProduct = all_product.find((product) => product.id === itemId); - cartProduct.size = size; - cartProduct.quantity = quantity; - setcartItems([...cartItems, cartProduct]); - } + const cartProduct = all_product.find((product) => product.id === itemId); + cartProduct.size = size; + cartProduct.Quantity = quantity; + console.log(cartProduct); + setcartItems([...cartItems, cartProduct]); }; - const removeFromCart = (itemId) => { setcartItems(cartItems.filter((product) => product.id !== itemId)); }; const getTotalCartAmount = () => { - return cartItems.reduce((total, product) => total + (product.new_price * product.quantity), 0); + const total = 0; + return cartItems.map((product) => total + product.new_price)[0] || 0; }; - - const getTotalCartItems = () => { return cartItems.length;