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

initial commit #103

Open
wants to merge 1 commit 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
7 changes: 7 additions & 0 deletions src/CartItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="cart-container">
<h2 style={{ color: 'black' }}>Total Plants : {cart.length}</h2>
<h2 style={{ color: 'black' }}>Total Cart Amount: ${calculateTotalAmount()}</h2>
<div>
{cart.map(item => (
Expand Down
26 changes: 22 additions & 4 deletions src/CartSlice.jsx
Original file line number Diff line number Diff line change
@@ -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;
50 changes: 42 additions & 8 deletions src/ProductList.jsx
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -213,7 +232,7 @@ function ProductList() {
}
];
const styleObj={
backgroundColor: '#4CAF50',
backgroundColor: '#615EFC',
color: '#fff!important',
padding: '15px',
display: 'flex',
Expand Down Expand Up @@ -243,31 +262,46 @@ const handlePlantsClick = (e) => {
};

const handleContinueShopping = (e) => {
console.log("clicked");
e.preventDefault();
setShowCart(false);
};
return (
<div>
<div className="navbar" style={styleObj}>
<div className="tag">
<div className="luxury">
<div style={{cursor:"pointer"}} onClick={props.toLanding} className="luxury">
<img src="https://cdn.pixabay.com/photo/2020/08/05/13/12/eco-5465432_1280.png" alt="" />
<a href="/" style={{textDecoration:'none'}}>
<a style={{textDecoration:'none'}}>
<div>
<h3 style={{color:'white'}}>Paradise Nursery</h3>
<h3 style={{color:'white'}}>Ryders's Plants</h3>
<i style={{color:'white'}}>Where Green Meets Serenity</i>
</div>
</a>
</div>

</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'><label style={{zIndex:1,position:"fixed",fontSize:"1.5rem",cursor:"pointer"}}>{totalItems()}</label><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" id="IconChangeColor" height="68" width="68">0<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">
<br></br>
{plantsArray.map((item)=><div className='mainCategoryDiv'> <h1>{item.category}</h1>
<div className="product-list">
{item.plants.map((plant)=>
<div className='product-card'>
<img className='product-image' src={plant.image} alt={plant.name} />
<h2>{plant.name}</h2>
<p>{plant.description}</p>
<p>{plant.cost}</p>
<button style={{backgroundColor:alreadyInCart(plant.name)?"gray":"#615EFC"}} disabled={alreadyInCart(plant.name)? true:false} onClick={()=>handleAddToCart({name:plant.name,cost:plant.cost,image:plant.image})} className='product-button'>Add to Cart</button>
</div>)}
</div>
</div>)}


</div>
Expand All @@ -278,4 +312,4 @@ const handlePlantsClick = (e) => {
);
}

export default ProductList;
export default ProductList;
1 change: 1 addition & 0 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { configureStore } from '@reduxjs/toolkit';
import cartReducer from './CartSlice';

const store = configureStore({
reducer: {
cart: cartReducer,
Expand Down