diff --git a/src/CartItem.jsx b/src/CartItem.jsx index e06317433..fb96816d6 100644 --- a/src/CartItem.jsx +++ b/src/CartItem.jsx @@ -1,6 +1,6 @@ import React from 'react'; import { useSelector, useDispatch } from 'react-redux'; -import { removeItem, updateQuantity } from './CartSlice'; +import { addItem, removeItem, updateQuantity } from './CartSlice'; import './CartItem.css'; const CartItem = ({ onContinueShopping }) => { @@ -9,27 +9,39 @@ const CartItem = ({ onContinueShopping }) => { // Calculate total amount for all products in the cart const calculateTotalAmount = () => { - + let totalAmount=0; + cart.map((plant)=>{ + totalAmount+=parseInt(plant.cost.slice(1))*plant.quantity; + }) + return totalAmount; }; const handleContinueShopping = (e) => { - + onContinueShopping(e); }; - - + const handleCheckoutShopping = (e) => { + alert('Functionality to be added for future reference'); + }; const handleIncrement = (item) => { + dispatch(addItem(item)); }; const handleDecrement = (item) => { - + if(item.quantity===1){ + dispatch(removeItem(item.name)); + } else{ + dispatch(updateQuantity({name:item.name,quantity:item.quantity-1})); + } }; const handleRemove = (item) => { + dispatch(removeItem(item.name)); }; // Calculate total cost based on quantity for an item const calculateTotalCost = (item) => { + return item.quantity*parseInt(item.cost.slice(1)); }; return ( @@ -57,12 +69,10 @@ const CartItem = ({ onContinueShopping }) => {

- +
); }; -export default CartItem; - - +export default CartItem; \ No newline at end of file diff --git a/src/CartSlice.jsx b/src/CartSlice.jsx index 32b8761ed..2f4d94c7c 100644 --- a/src/CartSlice.jsx +++ b/src/CartSlice.jsx @@ -7,11 +7,26 @@ export const CartSlice = createSlice({ }, 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); }, updateQuantity: (state, action) => { + const {name,quantity}=action.payload; + const existing=state.items.find((plant)=>plant.name===name); + if(existing){ + let updateCost=quantity-existing.quantity; + existing.quantity=quantity; + state.totalQuantity+=updateCost; + +} }, diff --git a/src/ProductList.jsx b/src/ProductList.jsx index 964b15d49..9d0110f3f 100644 --- a/src/ProductList.jsx +++ b/src/ProductList.jsx @@ -1,10 +1,16 @@ import React, { useState,useEffect } from 'react'; -import './ProductList.css' +import './ProductList.css'; +import { useDispatch,useSelector } from 'react-redux'; import CartItem from './CartItem'; +import {addItem} from './CartSlice'; function ProductList() { 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 dispatch=useDispatch(); + const totalQuantity=useSelector((state)=>state.cart.totalQuantity); + const items=useSelector((state)=>state.cart.items); + const plantsArray = [ { category: "Air Purifying Plants", @@ -246,6 +252,12 @@ const handlePlantsClick = (e) => { e.preventDefault(); setShowCart(false); }; + const handleAddtoCart=(product)=>{ + dispatch(addItem(product)); + setAddedtoCart((prevState)=>({ + ...prevState,[product.name]:true, + })); +} return (
@@ -263,19 +275,42 @@ const handlePlantsClick = (e) => {
handlePlantsClick(e)} style={styleA}>Plants
-
handleCartClick(e)} style={styleA}>

+
handleCartClick(e)} style={styleA}>

{totalQuantity}

{!showCart? (
+ { + plantsArray.map((item,index)=>( +
+

{item.category}

+
+ {item.plants.map((plant,pindex)=>( +
+ +
{plant.name}
+
{plant.cost}
+
{plant.description}
+ { !items.find((sample)=>sample.name===plant.name)?( + + ):(

Added to Cart

) + } +
+ )) + } +
+
+ + )) + }
) : ( - + )} ); } -export default ProductList; +export default ProductList; \ No newline at end of file