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

Update ProductList.jsx #179

Closed
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
76 changes: 67 additions & 9 deletions src/ProductList.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import React, { useState,useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { addItem } from './CartSlice';
import './ProductList.css'
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([]); // State to track added plants

const plantsArray = [
{
Expand Down Expand Up @@ -212,6 +216,45 @@ function ProductList() {
]
}
];
useEffect(() => {
// Fetch data from an API and update plantsArray
fetch('/api/plants')
.then(response => response.json())
.then(data => setPlantsArray(data));
}, []); // Empty dependency array means this runs once after the initial render

const handleAddToCart = (product) => {
dispatch(addItem(product)); // Use dispatch to add item
setAddedToCart((prevState) => ({
...prevState,
[product.name]: true, // Set the product name as key and value as true to indicate it's added to cart
}));
};

return (
<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
{addedToCart[plant.name] ? "Added to Cart" : "Add to Cart"}
</button>
</div>
))}
</div>
</div>
))}
<CartItem /> {/* This is where you might use CartItem */}
</div>
);
}
const styleObj={
backgroundColor: '#4CAF50',
color: '#fff!important',
Expand Down Expand Up @@ -268,14 +311,29 @@ const handlePlantsClick = (e) => {
</div>
{!showCart? (
<div className="product-grid">


</div>
) : (
<CartItem onContinueShopping={handleContinueShopping}/>
)}
</div>
);
}
{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)}>
{addedToCart[plant.name] ? "Added to Cart" : "Add to Cart"}
</button>
</div>
))}
</div>
</div>
))}
</div>
) : (
<CartItem onContinueShopping={handleContinueShopping}/>
)}
</div>
);

export default ProductList;