Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final Project Complete #83

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
{"homepage": "https://saptarsidebnath98.github.io/e-plantShopping",
"name": "shoppingcart",
"private": true,
"private": false,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite build; vite preview --host"
"preview": "vite build; vite preview --host",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
},
"dependencies": {
"@reduxjs/toolkit": "^2.2.3",
Expand Down
34 changes: 28 additions & 6 deletions src/CartItem.jsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,57 @@
import React from 'react';
import React, { useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { removeItem, updateQuantity } from './CartSlice';
import './CartItem.css';

const CartItem = ({ onContinueShopping }) => {
const CartItem = ({ setShowCart }) => {
const cart = useSelector(state => state.cart.items);
const dispatch = useDispatch();

// Calculate total amount for all products in the cart
const calculateTotalAmount = () => {

return cart.map((i)=>i.quantity * Number(i.cost.replace("$",""))).reduce((total, currVal)=> total + currVal, 0);

};
// Calculate total quantity of items in the cart
const calculateTotalQuantity = () => {
return cart.reduce((total, item) => total + item.quantity, 0);
};

// Call setTotalQuantity to pass the total quantity to the parent
// useEffect(() => {
// const totalQuantity = calculateTotalQuantity();
// setTotalQuantity(totalQuantity); // Send to parent
// }, [cart, setTotalQuantity]); // Recalculate whenever the cart changes

const handleContinueShopping = (e) => {

setShowCart(false);
};



const handleIncrement = (item) => {
dispatch(updateQuantity({ name: item.name, quantity: item.quantity + 1 }));
};

const handleDecrement = (item) => {

if (item.quantity > 1) {
dispatch(updateQuantity({ name: item.name, quantity: item.quantity - 1 }));
} else {
dispatch(removeItem(item.name));
}
};

const handleRemove = (item) => {
dispatch(removeItem(item.name));
};

const handleCheckoutShopping = (e) => {
alert('Functionality to be added for future reference');
};

// Calculate total cost based on quantity for an item
const calculateTotalCost = (item) => {
return (item.quantity * Number(item.cost.replace("$", "")));
};

return (
Expand Down Expand Up @@ -57,7 +79,7 @@ const CartItem = ({ onContinueShopping }) => {
<div className="continue_shopping_btn">
<button className="get-started-button" onClick={(e) => handleContinueShopping(e)}>Continue Shopping</button>
<br />
<button className="get-started-button1">Checkout</button>
<button className="get-started-button1" onClick={(e) => handleCheckoutShopping(e)}>Checkout</button>
</div>
</div>
);
Expand Down
38 changes: 26 additions & 12 deletions src/CartSlice.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import { createSlice } from '@reduxjs/toolkit';

export const CartSlice = createSlice({
name: 'cart',
initialState: {
items: [], // Initialize items as an empty array
},
reducers: {
addItem: (state, action) => {

name: 'cart',
initialState: {
items: [], // Initialize items as an empty array
},
removeItem: (state, action) => {
},
updateQuantity: (state, action) => {
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 itemToUpdate = state.items.find(item => item.name === name);
if (itemToUpdate) {
itemToUpdate.quantity = quantity;
}


},
},
},
});
export const selectTotalQuantity = (state) => {
return state.cart.items.reduce((total, item) => total + item.quantity, 0);
};

export const { addItem, removeItem, updateQuantity } = CartSlice.actions;

Expand Down
38 changes: 35 additions & 3 deletions src/ProductList.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import React, { useState,useEffect } from 'react';
import './ProductList.css'
import CartItem from './CartItem';
import { addItem } from './CartSlice';
import { useDispatch, useSelector } from 'react-redux';
import { selectTotalQuantity } 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 [totalQuantity, setTotalQuantity] = useState(0);
const dispatch = useDispatch();

const totalQuantity = useSelector(selectTotalQuantity);

const plantsArray = [
{
category: "Air Purifying Plants",
Expand Down Expand Up @@ -246,6 +254,15 @@ const handlePlantsClick = (e) => {
e.preventDefault();
setShowCart(false);
};

const handleAddToCart = (product) => {
dispatch(addItem(product));
setAddedToCart((prevState) => ({
...prevState,
[product.name]: true,
}));
};

return (
<div>
<div className="navbar" style={styleObj}>
Expand All @@ -263,16 +280,31 @@ const handlePlantsClick = (e) => {
</div>
<div style={styleObjUl}>
<div> <a href="#" onClick={(e)=>handlePlantsClick(e)} style={styleA}>Plants</a></div>
<div> <a href="#" onClick={(e) => handleCartClick(e)} style={styleA}><h1 className='cart'><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" id="IconChangeColor" height="68" width="68"><rect width="156" height="156" fill="none"></rect><circle cx="80" cy="216" r="12"></circle><circle cx="184" cy="216" r="12"></circle><path d="M42.3,72H221.7l-26.4,92.4A15.9,15.9,0,0,1,179.9,176H84.1a15.9,15.9,0,0,1-15.4-11.6L32.5,37.8A8,8,0,0,0,24.8,32H8" fill="none" stroke="#faf9f9" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" id="mainIconPathAttribute"></path></svg></h1></a></div>
<div> <a href="#" onClick={(e) => handleCartClick(e)} style={styleA}><h1 className='cart'>{totalQuantity}<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" id="IconChangeColor" height="68" width="68"><rect width="156" height="156" fill="none"></rect><circle cx="80" cy="216" r="12"></circle><circle cx="184" cy="216" r="12"></circle><path d="M42.3,72H221.7l-26.4,92.4A15.9,15.9,0,0,1,179.9,176H84.1a15.9,15.9,0,0,1-15.4-11.6L32.5,37.8A8,8,0,0,0,24.8,32H8" fill="none" stroke="#faf9f9" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" id="mainIconPathAttribute"></path></svg></h1></a></div>
</div>
</div>
{!showCart? (
<div className="product-grid">

{plantsArray.map((category, index) => (
<div key={index}>
<h1><div>{category.category}</div></h1>
<div className="product-list">
{category.plants.map((plant, plantIndex) => (
<div className="product-card" key={plantIndex}>
<img className="product-image" src={plant.image} alt={plant.name} />
<div className="product-title">{plant.name}</div>
<div className="product-description">{plant.description}</div>
<div className="product-cost">{plant.cost}</div>
<button className="product-button" onClick={() => handleAddToCart(plant)}>Add to Cart</button>
</div>
))}
</div>
</div>
))}

</div>
) : (
<CartItem onContinueShopping={handleContinueShopping}/>
<CartItem setShowCart={setShowCart}/>
)}
</div>
);
Expand Down