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

Main #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Main #78

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
27 changes: 19 additions & 8 deletions src/CartItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,48 @@ import { useSelector, useDispatch } from 'react-redux';
import { removeItem, updateQuantity } from './CartSlice';
import './CartItem.css';


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) => {
return total + parseFloat(calculateTotalCost(item));
}, 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(item.name));
}

};

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

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



return (
<div className="cart-container">
<h2 style={{ color: 'black' }}>Total Cart Amount: ${calculateTotalAmount()}</h2>
Expand All @@ -55,14 +68,12 @@ const CartItem = ({ onContinueShopping }) => {
</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>
<button className="get-started-button" onClick={handleContinueShopping}>Continue Shopping</button>
<br />
<button className="get-started-button1">Checkout</button>
</div>
</div>
);
};

export default CartItem;


export default CartItem;
25 changes: 23 additions & 2 deletions src/CartSlice.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
import { createSlice } from '@reduxjs/toolkit';

const initialState = {
items: [],
totalQuantity: 0,
};
export const CartSlice = createSlice({
name: 'cart',
initialState: {
items: [], // Initialize items as an empty array
},
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 });
}
state.totalQuantity++;
},
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 the action creators
export const { addItem, removeItem, updateQuantity } = CartSlice.actions;

export default CartSlice.reducer;
// Export the reducer as the default export
export default CartSlice.reducer;
54 changes: 50 additions & 4 deletions src/ProductList.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
import React, { useState,useEffect } from 'react';
import './ProductList.css'
import CartItem from './CartItem';
import { addItem } from './CartSlice';
import { useSelector } from 'react-redux';
import { useDispatch } from 'react-redux'; // Import useDispatch

function ProductList() {
const dispatch = useDispatch();
const cartItems = useSelector((state) => state.cart.items);


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 = useSelector((state) => state.cart.totalQuantity); // Retrieve the total quantity

const handleAddToCart = (product) => {
// Dispatch the plant information to the Redux slice
dispatch(addItem(product));

// Update the addedToCart state to reflect the plant has been added
setAddedToCart((prevState) => ({
...prevState,
[product.name]: true,
}));
}




const plantsArray = [
{
Expand Down Expand Up @@ -242,8 +267,7 @@ const handlePlantsClick = (e) => {
setShowCart(false); // Hide the cart when navigating to About Us
};

const handleContinueShopping = (e) => {
e.preventDefault();
const handleContinueShopping = () => {
setShowCart(false);
};
return (
Expand All @@ -263,12 +287,34 @@ 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></div>

</div>
</div>
{!showCart? (
<div className="product-grid">
{plantsArray.map((category, categoryIndex) => (
<div key={categoryIndex}>
<h1><center>{category.category}</center></h1>
<div className="product-list">
{category.plants.map((plant, plantIndex)=>(
<div key={plantIndex} className ='product-card'>
<img className ="product-image" src={plant.image} alt={plant.name} />
<div className='product-title'> {plant.name} </div>
<p style={{ color: 'red' }}> {plant.cost}</p>
<p>{plant.description}</p>
<button className="product-button" onClick={() => handleAddToCart(plant)}> Add to Cart</button>

</div>
))}

</div>

</div>
))}

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

export default ProductList;
export default ProductList;