Skip to content
Open

Final #444

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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
"private": true,
"version": "0.0.0",
"type": "module",
"homepage": "https://santoshkumarmeher.github.io/e-plantShopping",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite build; vite preview --host"
"preview": "vite build; vite preview --host",
"deploy": "npm run build && gh-pages -d dist",
"predeploy": "npm run build"
},
"dependencies": {
"@reduxjs/toolkit": "^2.2.3",
Expand Down
23 changes: 18 additions & 5 deletions src/CartItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,40 @@ const CartItem = ({ onContinueShopping }) => {

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

return cart.reduce((total, item) => {
const itemCost = parseFloat(item.cost.substring(1));
return total + (itemCost * item.quantity);
}, 0).toFixed(2);
};

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 itemCost = parseFloat(item.cost.substring(1));
return (itemCost * item.quantity).toFixed(2);
};

return (
Expand Down Expand Up @@ -57,7 +70,7 @@ const CartItem = ({ onContinueShopping }) => {
<div className="continue_shopping_btn">
<button className="get-started-button" onClick={(e) => handleContinueShopping(e)}>Continue Shopping</button>
<br />
<button className="get-started-button1">Checkout</button>
<button className="get-started-button1" onClick={(e) => handleCheckoutShopping(e)}>Checkout</button>
</div>
</div>
);
Expand Down
18 changes: 15 additions & 3 deletions src/CartSlice.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ export const CartSlice = createSlice({
},
reducers: {
addItem: (state, action) => {

const existingItem = state.items.find(item => item.name === action.payload.name);
if (existingItem) {
existingItem.quantity += 1;
} else {
state.items.push({
...action.payload,
quantity: 1
});
}
},
removeItem: (state, action) => {
state.items = state.items.filter(item => item.name !== action.payload);
},
updateQuantity: (state, action) => {


const { name, quantity } = action.payload;
const item = state.items.find(item => item.name === name);
if (item) {
item.quantity = quantity;
}
},
},
});
Expand Down
43 changes: 43 additions & 0 deletions src/ProductList.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,49 @@ body {
gap: 20px;
}

.product-card img {
max-width: 100%;
height: 200px;
object-fit: cover;
border-radius: 5px;
margin-bottom: 10px;
}

.product-card h3 {
font-weight: bold;
margin-bottom: 10px;
}

.product-card .cost {
color: #e74c3c;
font-size: 1.2rem;
margin-bottom: 10px;
font-weight: bold;
}

.product-card button {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
transition-duration: 0.4s;
cursor: pointer;
border-radius: 4px;
}

.product-card button:hover {
background-color: #45a049;
}

.product-card button.added {
background-color: #888;
}

/* Pseudo-classes - Hover effect on product button */
.product-card:hover {
transform: scale(1.05);
Expand Down
39 changes: 36 additions & 3 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, useSelector } from 'react-redux';
import { addItem } from './CartSlice';
function ProductList({ onHomeClick }) {
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 calculateTotalQuantity = () => {
return cartItems ? cartItems.reduce((total, item) => total + item.quantity, 0) : 0;
};

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

const handleAddToCart = (plant) => {
dispatch(addItem(plant));
setAddedToCart({ ...addedToCart, [plant.name]: true });
};
return (
<div>
<div className="navbar" style={styleObj}>
Expand All @@ -269,13 +283,32 @@ function ProductList({ onHomeClick }) {
</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><span className='cart_quantity_count'>{calculateTotalQuantity()}</span></a></div>
</div>
</div>
{!showCart ? (
<div className="product-grid">


{plantsArray.map((category) => (
<div key={category.category}>
<h2>{category.category}</h2>
<div className="product-list">
{category.plants.map((plant) => (
<div key={plant.name} className="product-card">
<img src={plant.image} alt={plant.name} />
<h3>{plant.name}</h3>
<p>{plant.description}</p>
<p className="cost">{plant.cost}</p>
<button
onClick={() => handleAddToCart(plant)}
className={addedToCart[plant.name] ? 'added' : ''}
>
{addedToCart[plant.name] ? 'Added to Cart' : 'Add to Cart'}
</button>
</div>
))}
</div>
</div>
))}
</div>
) : (
<CartItem onContinueShopping={handleContinueShopping} />
Expand Down
3 changes: 2 additions & 1 deletion src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import App from './App.jsx'
import './index.css'
import { Provider } from 'react-redux'
import store from './store.js'

ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<Provider store={store}>
<App />
<App />
</Provider>
</React.StrictMode>,
)
9 changes: 7 additions & 2 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { configureStore } from '@reduxjs/toolkit';
import cartReducer from './CartSlice';
const store = configureStore({

// Create a Redux store using configureStore from Redux Toolkit
const store = configureStore({
// Define the root reducer object
reducer: {
// 'cart' is the name of the slice in the store, and it's managed by cartReducer
cart: cartReducer,
},
});
export default store

export default store;
2 changes: 1 addition & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
base: "/shoppingreact",
base: "/e-plantShopping/",
plugins: [react()],
})