diff --git a/src/CartItem.jsx b/src/CartItem.jsx
index e06317433f..e8cf2bd997 100644
--- a/src/CartItem.jsx
+++ b/src/CartItem.jsx
@@ -2,6 +2,7 @@ import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { removeItem, updateQuantity } from './CartSlice';
import './CartItem.css';
+import PropTypes from 'prop-types';
const CartItem = ({ onContinueShopping }) => {
const cart = useSelector(state => state.cart.items);
@@ -9,27 +10,32 @@ const CartItem = ({ onContinueShopping }) => {
// Calculate total amount for all products in the cart
const calculateTotalAmount = () => {
-
+ const totalAmount = cart.reduce((total, item)=> total+ item.quantity * parseFloat(item.cost.substring(1)) , 0)
+ return totalAmount
};
const handleContinueShopping = (e) => {
-
+ e.preventDefault()
+ 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.name))
};
// Calculate total cost based on quantity for an item
const calculateTotalCost = (item) => {
+ return item.quantity * parseFloat(item.cost.substring(1))
};
return (
@@ -57,12 +63,16 @@ const CartItem = ({ onContinueShopping }) => {
-
+
);
};
+CartItem.propTypes = {
+ onContinueShopping: PropTypes.func.isRequired,
+};
export default CartItem;
+
diff --git a/src/CartSlice.jsx b/src/CartSlice.jsx
index 32b8761ed0..f1f3100785 100644
--- a/src/CartSlice.jsx
+++ b/src/CartSlice.jsx
@@ -7,12 +7,24 @@ export const CartSlice = createSlice({
},
reducers: {
addItem: (state, action) => {
-
+ const {name, image, cost} = action.payload;
+ const exisitingItem = state.items.find((item)=> item.name === action.payload.name);
+ if(exisitingItem) {
+ exisitingItem.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 itemToUpdate = state.items.find(item=> item.name === name);
+ if(itemToUpdate) {
+ itemToUpdate.quantity = quantity
+ }
},
},
diff --git a/src/ProductList.css b/src/ProductList.css
index 52f9c7a84f..6585cf839c 100644
--- a/src/ProductList.css
+++ b/src/ProductList.css
@@ -164,6 +164,19 @@ body {
color: white;
display: flex;
}
+
+.cartQuantity {
+ position: absolute;
+ margin-left: 25px;
+ margin-top: 20px;
+ width: 24px;
+ height: 24px;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ font-size: 24px;
+}
+
.cart_quantity_count{
margin-top: 16px;
/* background-color: red; */
@@ -210,6 +223,10 @@ body {
transition-duration: 0.4s;
cursor: pointer;
}
+ .product-button:disabled {
+ cursor: not-allowed;
+ opacity: 0.6;
+ }
.product-button:hover {
background-color: #45a049;
@@ -249,4 +266,4 @@ body {
.ul div {
text-align: center; /* Align text to the center */
}
- }
\ No newline at end of file
+ }
diff --git a/src/ProductList.jsx b/src/ProductList.jsx
index 7682c04fc4..70795f004e 100644
--- a/src/ProductList.jsx
+++ b/src/ProductList.jsx
@@ -1,9 +1,14 @@
import React, { useState, useEffect } from 'react';
import './ProductList.css'
import CartItem from './CartItem';
+import { useDispatch, useSelector } from 'react-redux';
+import { addItem } from './CartSlice';
function ProductList({ onHomeClick }) {
const [showCart, setShowCart] = useState(false);
const [showPlants, setShowPlants] = useState(false); // State to control the visibility of the About Us page
+ const cartItems = useSelector(state=> state.cart.items);
+
+ const dispatch = useDispatch()
const plantsArray = [
{
@@ -252,6 +257,14 @@ function ProductList({ onHomeClick }) {
e.preventDefault();
setShowCart(false);
};
+
+ const handleAddToCart = (product) => {
+ dispatch(addItem(product))
+ };
+
+
+ const totalQuantity = cartItems? cartItems.reduce((total, item)=> total+ item.quantity, 0): 0
+
return (
@@ -269,12 +282,28 @@ function ProductList({ onHomeClick }) {
{!showCart ? (
-
+ {plantsArray.map((category, index)=> (
+
+
{category.category}
+
+ {category.plants.map((plant, idx) => (
+
+

+
{plant.name}
+
{plant.description}
+
{plant.cost}
+
+
+ ))}
+
+
+ ))}
) : (