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

New branch #107

Open
wants to merge 3 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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
"type": "module",
"scripts": {
"dev": "vite",
"predeploy": "npm run build",
"deploy": "gh-pages -d dist",
"build": "vite build",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite build; vite preview --host"
},
"dependencies": {
"@reduxjs/toolkit": "^2.2.3",
"@reduxjs/toolkit": "^2.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^9.1.1"
Expand All @@ -23,7 +25,7 @@
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"gh-pages": "^6.1.1",
"gh-pages": "^6.2.0",
"vite": "^5.2.0"
}
}
48 changes: 18 additions & 30 deletions src/CartItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,42 @@ import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { removeItem, updateQuantity } from './CartSlice';
import './CartItem.css';
import ProductList from './ProductList';

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

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

return cart.reduce((total, item) => {
const itemCost = parseFloat(item.cost.replace('$', '')); // Remove '$' and convert to number
return total + (itemCost * item.quantity);
}, 0).toFixed(2);
};

const handleContinueShopping = (e) => {

const handleContinueShopping = () => {
onContinueShopping();
};



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({ name: item.name })); // Remove item if quantity reaches 0
}
};

const handleRemove = (item) => {
dispatch(removeItem({ name: item.name })); // Dispatch the remove action with item name
};

// Calculate total cost based on quantity for an item
const calculateTotalCost = (item) => {
const itemCost = parseFloat(item.cost.replace('$', ''));
return (itemCost * item.quantity).toFixed(2);
};

return (
Expand All @@ -41,28 +49,8 @@ const CartItem = ({ onContinueShopping }) => {
<img className="cart-item-image" src={item.image} alt={item.name} />
<div className="cart-item-details">
<div className="cart-item-name">{item.name}</div>
<div className="cart-item-cost">{item.cost}</div>
<div className="cart-item-cost">${parseFloat(item.cost.replace('$', '')).toFixed(2)}</div>
<div className="cart-item-quantity">
<button className="cart-item-button cart-item-button-dec" onClick={() => handleDecrement(item)}>-</button>
<span className="cart-item-quantity-value">{item.quantity}</span>
<button className="cart-item-button cart-item-button-inc" onClick={() => handleIncrement(item)}>+</button>
</div>
<div className="cart-item-total">Total: ${calculateTotalCost(item)}</div>
<button className="cart-item-delete" onClick={() => handleRemove(item)}>Delete</button>
</div>
</div>
))}
</div>
<div style={{ marginTop: '20px', color: 'black' }} className='total_cart_amount'></div>
<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>
</div>
</div>
);
};

export default CartItem;


<button className="cart-item-button cart-item-button-inc" onClick={() => handleIncre
34 changes: 15 additions & 19 deletions src/CartSlice.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { configureStore } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';

export const CartSlice = createSlice({
// CartSlice.js
const initialState = {
items: [],
totalQuantity: 0,
};

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

},
removeItem: (state, action) => {
},
updateQuantity: (state, action) => {


},
},
});

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

export default CartSlice.reducer;
addItem(state, action) {
const existingItem = state.items.find(item => item.name === action.payload.name);
if (existingItem) {
existingItem.quantity++;
} else {
stat
49 changes: 47 additions & 2 deletions src/ProductList.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import React, { useState,useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { addItem, removeItem, updateQuantity } from './CartSlice';
import './ProductList.css'
import CartItem from './CartItem';
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 dispatch = useDispatch();
const totalQuantity = useSelector(selectTotalQuantity);


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

const handleAddToCart = (plant) => {
console.log("Adding to cart:", plant);
dispatch(addItem(plant));
setAddedToCart((prevState) => ({
...prevState,
[plant.name]: true // Set the plant name as key and value as true
}));
};

return (
<div>
<div className="navbar" style={styleObj}>
Expand All @@ -263,12 +282,38 @@ 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'>
<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>
<span>{totalQuantity}</span>
</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>
{/*Similarly like the above plant.name show other details like description and cost*/}
<button className="product-button" onClick={() => handleAddToCart(plant)}>Add to Cart</button>
</div>
))}
</div>
</div>
))}

</div>
) : (
Expand Down
2 changes: 1 addition & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
base: "/shoppingreact",
base: "/e-plantShopping",
plugins: [react()],
})