diff --git a/src/CartItem.jsx b/src/CartItem.jsx index e06317433..c6b80dc88 100644 --- a/src/CartItem.jsx +++ b/src/CartItem.jsx @@ -9,33 +9,65 @@ const CartItem = ({ onContinueShopping }) => { // Calculate total amount for all products in the cart const calculateTotalAmount = () => { - + let total_cart_amount =0; + cart.forEach(item => { + const itemCost =parseFloat(item.cost.replace('$','')); + total_cart_amount += itemCost * item.updateQuantity; + }); + return total_cart_amount; + }; + + const handleCheckoutShopping = (e) => { + alert('coming!'); + }; const handleContinueShopping = (e) => { - + if (e) e.preventDefault(); + onContinueShopping(e); }; const handleIncrement = (item) => { + const newQuantity = item.quantity + 1; + dispatch(updateQuantity({name: item.name,quantity: + newQuantity})); + console.log("Item quantity incremented by 1: ", newQuantity, +item); }; const handleDecrement = (item) => { - + if (item.quantity > 0) { + const newQuantity = item.quantity - 1; + if (newQuantity === 0) { + dispatch(removeItem({ name: item.name })); + console.log("Item removed quantity went below 1:", item); + } else { + dispatch(updateQuantity({ name: item.name, quantity: newQuantity })); + console.log("Item quantity decremented by 1: ", newQuantity, item); + } + } else { + dispatch(removeItem({ name: item.name })); + console.log("Item removed because item quantity was 0:", item); + } }; const handleRemove = (item) => { + dispatch(removeItem({name: item.name})); + console.log("Item removed:", item); }; // Calculate total cost based on quantity for an item const calculateTotalCost = (item) => { + const itemCost = parseFloat(item.cost.replace('$', '')); + return itemCost * item.quantity; }; return (

Total Cart Amount: ${calculateTotalAmount()}

-
+
{cart.map(item => (
{item.name} @@ -57,7 +89,7 @@ const CartItem = ({ onContinueShopping }) => {

- +
); diff --git a/src/CartSlice.jsx b/src/CartSlice.jsx index 32b8761ed..42f7efb3a 100644 --- a/src/CartSlice.jsx +++ b/src/CartSlice.jsx @@ -4,16 +4,34 @@ export const CartSlice = createSlice({ name: 'cart', initialState: { items: [], // Initialize items as an empty array + totalQuantity: 0, }, reducers: { addItem: (state, action) => { - + const { name, image, cost } = action.payload; + const existingItem = state.items.find(item => item.name === name); + if (existingItem) { + existingItem.quantity++; + } else { + state.items.push({ name, image, cost, quantity: 1 }); + } }, removeItem: (state, action) => { + //state.items = state.items.filter(item => item.name !== action.payload); + const {name} = action.payload; + state.items = state.items.filter(item => item.name !== name); + state.totalQuantity=state.items.reduce((total, item) => + total +item.quantity, 0); }, updateQuantity: (state, action) => { - + const { name, quantity } = action.payload; +const itemToUpdate = state.items.find(item => item.name === name); +if (itemToUpdate) { + itemToUpdate.quantity = quantity; + state.totalQuantity=state.items.reduce((total, item) => + total +item.quantity, 0); +} }, }, }); diff --git a/src/ProductList.jsx b/src/ProductList.jsx index 964b15d49..31a71b62e 100644 --- a/src/ProductList.jsx +++ b/src/ProductList.jsx @@ -1,9 +1,17 @@ import React, { useState,useEffect } from 'react'; import './ProductList.css' import CartItem from './CartItem'; +import addItem from './CartSlice'; +import { useDispatch, useSelector } from 'react-redux'; + function ProductList() { + const dispatch=useDispatch(); + const cart= useSelector((state) => state.cart.items); + const totalQuantity = useSelector((state) => state.cart.totalQuantity); + const productStatus = useSelector(state => state.cart.productStatus); const [showCart, setShowCart] = useState(false); const [showPlants, setShowPlants] = useState(false); // State to control the visibility of the About Us page + const [addedToCart, setAddedToCart] = useState({}); const plantsArray = [ { @@ -243,9 +251,20 @@ const handlePlantsClick = (e) => { }; const handleContinueShopping = (e) => { - e.preventDefault(); + if (e)e.preventDefault(); setShowCart(false); }; + + const handleAddToCart = (product,e) => { + if (e)e.preventDefault(); + console.log(dispatch(addItem(product))); + dispatch(addItem(product)); + setAddedToCart((prevState) => ({ + ...prevState, + [product.name]: true, // Set the product name as key and value as true to indicate it's added to cart + })); + }; + return (
@@ -268,7 +287,24 @@ const handlePlantsClick = (e) => {
{!showCart? (
- + {plantsArray.map((category, index) => ( +
+

{category.category}

+
+ {category.plants.map((plant, plantIndex) => ( +
+ {plant.name} +
{plant.name}
+
{plant.description}
+
{plant.cost}
+ {/*Similarly like the above plant.name show other details like description and cost*/} + {/**/} + +
+ ))} +
+
+ ))}
) : (