diff --git a/package.json b/package.json index b7d4c1d1d4..b8bd3d9939 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,10 @@ "scripts": { "dev": "vite", "build": "vite build", + "preview": "vite preview", "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0", - "preview": "vite build; vite preview --host" + "predeploy": "npm run build", + "deploy": "gh-pages -d dist", }, "dependencies": { "@reduxjs/toolkit": "^2.2.3", @@ -23,7 +25,7 @@ "eslint-plugin-react": "^7.34.1", "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-refresh": "^0.4.6", - "gh-pages": "^6.1.1", + "gh-pages": "^6.3.0", "vite": "^5.2.0" } } diff --git a/src/CartItem.jsx b/src/CartItem.jsx index e06317433f..8c039462c7 100644 --- a/src/CartItem.jsx +++ b/src/CartItem.jsx @@ -6,31 +6,51 @@ import './CartItem.css'; const CartItem = ({ onContinueShopping }) => { const cart = useSelector(state => state.cart.items); const dispatch = useDispatch(); + const total = 0; // Calculate total amount for all products in the cart const calculateTotalAmount = () => { - + let total = 0; + + cart.forEach(item => { + const quantity = item.quantity; + const cost = parseFloat(item.cost.substring(1)); + total += quantity * cost; + }); + + return total; }; const handleContinueShopping = (e) => { - + onContinueShopping(e); }; - + const handleCheckoutShopping = (e) => { + alert('Functionality to be added for future reference'); + }; 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) => { + const cost = parseFloat(item.cost.substring(1)); + return (cost * item.quantity).toFixed(2); }; + return (
diff --git a/src/CartSlice.jsx b/src/CartSlice.jsx index 32b8761ed0..c739fa1e16 100644 --- a/src/CartSlice.jsx +++ b/src/CartSlice.jsx @@ -7,12 +7,27 @@ export const CartSlice = createSlice({ }, reducers: { addItem: (state, action) => { - + const { name, image, cost } = action.payload; // Destructure product details from the action payload + // Check if the item already exists in the cart by comparing names + const existingItem = state.items.find(item => item.name === name); + if (existingItem) { + // If item already exists in the cart, increase its quantity + existingItem.quantity++; + } else { + // If item does not exist, add it to the cart with quantity 1 + 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; // Destructure the product name and new quantity from the action payload + // Find the item in the cart that matches the given name + const itemToUpdate = state.items.find(item => item.name === name); + if (itemToUpdate) { + itemToUpdate.quantity = quantity; // If the item is found, update its quantity to the new value + } }, }, diff --git a/src/ProductList.jsx b/src/ProductList.jsx index 7682c04fc4..cf9f79dbca 100644 --- a/src/ProductList.jsx +++ b/src/ProductList.jsx @@ -1,9 +1,16 @@ import React, { useState, useEffect } from 'react'; import './ProductList.css' import CartItem from './CartItem'; +import { addItem } from './CartSlice'; +import { useDispatch, useSelector } from 'react-redux'; + + 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({}); + const dispatch = useDispatch(); + const cart = useSelector((state) => state.cart.items); // accede al carrito const plantsArray = [ { @@ -252,6 +259,13 @@ function ProductList({ onHomeClick }) { e.preventDefault(); setShowCart(false); }; + const handleAddToCart = (product) => { + dispatch(addItem(product)); // Dispatch the action to add the product to the cart (Redux action) + setAddedToCart((prevState) => ({ // Update the local state to reflect that the product has been added + ...prevState, // Spread the previous state to retain existing entries + [product.name]: true, // Set the current product's name as a key with value 'true' to mark it as added + })); + }; return (
@@ -269,7 +283,17 @@ function ProductList({ onHomeClick }) {
handlePlantsClick(e)} style={styleA}>Plants
-
handleCartClick(e)} style={styleA}>

+
+ handleCartClick(e)} style={styleA}> +

+ 🛒 + + {cart.reduce((sum, item) => sum + item.quantity, 0)} + +

+
+
+
{!showCart ? ( @@ -280,6 +304,43 @@ function ProductList({ onHomeClick }) { ) : ( )} + +
+ {plantsArray.map((category, index) => ( // Loop through each category in plantsArray +
{/* Unique key for each category div */} +

+
{category.category}
{/* Display the category name */} +

+
{/* Container for the list of plant cards */} + {category.plants.map((plant, plantIndex) => ( // Loop through each plant in the current category +
{/* Unique key for each plant card */} + {plant.name} +
{plant.name}
{/* Display plant name */} + {/* Display other plant details like description and cost */} +
{plant.description}
{/* Display plant description */} +
${plant.cost}
{/* Display plant cost */} + +
+ ))} +
+
+ ))} + +
); } diff --git a/src/main.jsx b/src/main.jsx index 9ea042ec2b..4803d9e887 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -1,13 +1,15 @@ -import React from 'react' -import ReactDOM from 'react-dom/client' -import App from './App.jsx' -import './index.css' -import { Provider } from 'react-redux' -import store from './store.js' +// src/main.jsx (Vite) +import React from 'react'; +import ReactDOM from 'react-dom/client'; +import App from './App.jsx'; +import './index.css'; +import { Provider } from 'react-redux'; +import store from './store.js'; // or './store' + ReactDOM.createRoot(document.getElementById('root')).render( - + , -) +); \ No newline at end of file diff --git a/src/store.js b/src/store.js index 54d0d6d66e..30e8a48444 100644 --- a/src/store.js +++ b/src/store.js @@ -1,5 +1,8 @@ import { configureStore } from '@reduxjs/toolkit'; import cartReducer from './CartSlice'; +import reducer from './CartSlice'; + + const store = configureStore({ reducer: { cart: cartReducer, diff --git a/vite.config.js b/vite.config.js index 4d190ae430..615f06a9c3 100644 --- a/vite.config.js +++ b/vite.config.js @@ -3,6 +3,6 @@ import react from '@vitejs/plugin-react' // https://vitejs.dev/config/ export default defineConfig({ - base: "/shoppingreact", + base: '/e-plantShopping/', plugins: [react()], -}) +}); \ No newline at end of file