diff --git a/src/CartItem.jsx b/src/CartItem.jsx index e06317433..968a5a9d7 100644 --- a/src/CartItem.jsx +++ b/src/CartItem.jsx @@ -9,31 +9,38 @@ const CartItem = ({ onContinueShopping }) => { // Calculate total amount for all products in the cart const calculateTotalAmount = () => { + return cart.reduce((total, item) => total + Number(item.cost.substring(1)) * item.quantity, 0); }; const handleContinueShopping = (e) => { + onContinueShopping(e); }; const handleIncrement = (item) => { + dispatch(updateQuantity({ name: item.name, quantity: item.quantity + 1 })); }; const handleDecrement = (item) => { + dispatch(updateQuantity({ name: item.name, quantity: item.quantity - 1 })); }; const handleRemove = (item) => { + dispatch(removeItem(item)); }; // Calculate total cost based on quantity for an item const calculateTotalCost = (item) => { + return Number(item.cost.substring(1)) * item.quantity; }; return (
+

Total Plants : {cart.length}

Total Cart Amount: ${calculateTotalAmount()}

{cart.map(item => ( diff --git a/src/CartSlice.jsx b/src/CartSlice.jsx index 32b8761ed..dd3911dd0 100644 --- a/src/CartSlice.jsx +++ b/src/CartSlice.jsx @@ -1,23 +1,41 @@ import { createSlice } from '@reduxjs/toolkit'; +import { act } from 'react'; -export const CartSlice = createSlice({ +export const CreatSlice = createSlice({ name: 'cart', initialState: { items: [], // Initialize items as an empty array }, reducers: { addItem: (state, action) => { - + const item=action.payload; + const existingItem=state.items.find((i)=>i.name===item.id); + if(existingItem){ + existingItem.quantity++; + }else{ + state.items.push({...item,quantity:1}); + } }, removeItem: (state, action) => { + const item=action.payload; + const existingItem=state.items.find((i)=>i.name===item.name); + if(existingItem){ + state.items=state.items.filter((i)=>i.name!==item.name); + } }, updateQuantity: (state, action) => { + const { name, quantity } = action.payload; + const item = state.items.find((i) => i.name === name); + item.quantity = quantity; + if (item.quantity === 0) { + state.items = state.items.filter((i) => i.name !== name); + } }, }, }); -export const { addItem, removeItem, updateQuantity } = CartSlice.actions; +export const { addItem, removeItem, updateQuantity } = CreatSlice.actions; -export default CartSlice.reducer; +export default CreatSlice.reducer; \ No newline at end of file diff --git a/src/ProductList.jsx b/src/ProductList.jsx index 964b15d49..67a44bd62 100644 --- a/src/ProductList.jsx +++ b/src/ProductList.jsx @@ -1,10 +1,29 @@ import React, { useState,useEffect } from 'react'; import './ProductList.css' import CartItem from './CartItem'; -function ProductList() { +import { useDispatch, useSelector } from 'react-redux'; +import { addItem } from './CartSlice'; +function ProductList(props) { const [showCart, setShowCart] = useState(false); const [showPlants, setShowPlants] = useState(false); // State to control the visibility of the About Us page - + const [cart, setCart] = useState([]); // State to store the items added to the cart + const dispatch = useDispatch(); + const cartItems=useSelector(state => state.cart.items); + console.log(cartItems); + // setCart(cartItems); + useEffect(() => { + + }, []); + const alreadyInCart = (itemName) => { + return cartItems.some((item) => item.name === itemName); + } + const handleAddToCart = (item) => { + console.log("clicked"); + dispatch(addItem(item)); + } + const totalItems = () => { + return cartItems.reduce((total, item) => total + item.quantity, 0); + } const plantsArray = [ { category: "Air Purifying Plants", @@ -213,7 +232,7 @@ function ProductList() { } ]; const styleObj={ - backgroundColor: '#4CAF50', + backgroundColor: '#615EFC', color: '#fff!important', padding: '15px', display: 'flex', @@ -243,6 +262,7 @@ const handlePlantsClick = (e) => { }; const handleContinueShopping = (e) => { + console.log("clicked"); e.preventDefault(); setShowCart(false); }; @@ -250,11 +270,11 @@ const handlePlantsClick = (e) => {
- {!showCart? (
+

+ {plantsArray.map((item)=>

{item.category}

+
+ {item.plants.map((plant)=> +
+ {plant.name} +

{plant.name}

+

{plant.description}

+

{plant.cost}

+ +
)} +
+
)}
@@ -278,4 +312,4 @@ const handlePlantsClick = (e) => { ); } -export default ProductList; +export default ProductList; \ No newline at end of file diff --git a/src/store.js b/src/store.js index 54d0d6d66..9d762ef35 100644 --- a/src/store.js +++ b/src/store.js @@ -1,5 +1,6 @@ import { configureStore } from '@reduxjs/toolkit'; import cartReducer from './CartSlice'; + const store = configureStore({ reducer: { cart: cartReducer,