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

Redux #119

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Redux #119

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
17 changes: 15 additions & 2 deletions src/CartSlice.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createSlice } from '@reduxjs/toolkit';
import { addItem } from './CartSlice';

export const CartSlice = createSlice({
name: 'cart',
Expand All @@ -7,13 +8,25 @@ export const CartSlice = createSlice({
},
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;
}
},
},
});
Expand Down
25 changes: 24 additions & 1 deletion src/ProductList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CartItem from './CartItem';
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 plantsArray = [
{
Expand Down Expand Up @@ -246,6 +247,14 @@ const handlePlantsClick = (e) => {
e.preventDefault();
setShowCart(false);
};

const handleAddToCart = (product) => {
dispatchEvent(addItem(product));
setAddedToCart((prevState) => ({
...prevState,
[product.name]: true,
}));
}
return (
<div>
<div className="navbar" style={styleObj}>
Expand All @@ -268,7 +277,21 @@ const handlePlantsClick = (e) => {
</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