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 #280

Open
wants to merge 6 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
1 change: 1 addition & 0 deletions .dependencygraph/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"dependencyTree":{"children":[]},"dependencyNodes":{}}
15 changes: 15 additions & 0 deletions .dependencygraph/setting.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"entryFilePath": "src\\extension.ts",
"alias": {
"@": "./src"
},
"resolveExtensions": [
".js",
".jsx",
".ts",
".tsx",
".vue",
".scss",
".less"
]
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 22 additions & 4 deletions src/CartItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,31 @@ const CartItem = ({ onContinueShopping }) => {

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

};
let total = 0;

if (!cart || cart.length === 0) return total;

cart.forEach(item => {
// Extract cost (remove $ and convert to number)
const cost = parseFloat(item.cost.substring(1));
// Get quantity
const quantity = item.quantity || 0;
// Add to running total
total += cost * quantity;
});

// Return formatted total with 2 decimal places
return total.toFixed(2);
};

const handleContinueShopping = (e) => {

};



const handleIncrement = (item) => {
const updatedItem = { ...item, quantity: item.quantity + 1 };
dispatch(updateQuantity(updatedItem));
};

const handleDecrement = (item) => {
Expand All @@ -30,7 +45,10 @@ const CartItem = ({ onContinueShopping }) => {

// Calculate total cost based on quantity for an item
const calculateTotalCost = (item) => {
};
if (!item) return "0.00";
const cost = parseFloat(item.cost.substring(1));
return (cost * item.quantity).toFixed(2);
};

return (
<div className="cart-container">
Expand Down
23 changes: 18 additions & 5 deletions src/CartSlice.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@ 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) => {


},
},
updateQuantity: (state, action) => {
const { name, quantity } = action.payload;
const itemToUpdate = state.items.find(item => item.name === name);
if (itemToUpdate) {
itemToUpdate.quantity = quantity;
}
}
}
});

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

export default CartSlice.reducer;

39 changes: 37 additions & 2 deletions src/ProductList.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import React, { useState, useEffect } from 'react';
import './ProductList.css'
import CartItem from './CartItem';
import { useDispatch } from 'react-redux';
import CartSlice, { addItem, removeItem, updateQuantity } from './CartSlice'; // Import the reducer


function ProductList({ onHomeClick }) {
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 store the items added to the cart
const [cartItems, setCartItems] = useState([]); // State to store the items in the cart
const dispatch = useDispatch();



const plantsArray = [
{
Expand Down Expand Up @@ -252,6 +261,16 @@ function ProductList({ onHomeClick }) {
e.preventDefault();
setShowCart(false);
};

const handleAddToCart = (product) => {
dispatch(addItem(product));
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>
<div className="navbar" style={styleObj}>
Expand All @@ -274,8 +293,24 @@ function ProductList({ onHomeClick }) {
</div>
{!showCart ? (
<div className="product-grid">


<div className="product-grid">
{plantsArray.map((category, index) => (
<div key={index}>
<h1>{category.category}</h1>
<div className="product-list">
{category.plants.map((plant, plantIndex) => (
<div className="product-card" key={plantIndex}>
<div className="product-title">{plant.name}</div>
<img className="product-image" src={plant.image} alt={plant.name}/>
<h5>{plant.description}</h5>
<h3>{plant.cost}</h3>
<button className="product-button" onClick={() => handleAddToCart(plant)}>Add to Cart</button>
</div>
))}
</div>
</div>
))}
</div>
</div>
) : (
<CartItem onContinueShopping={handleContinueShopping} />
Expand Down