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

completed all tasks #80

Open
wants to merge 3 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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"type": "module",
"scripts": {
"dev": "vite",
"predeploy": "npm run build",
"deploy": "gh-pages -d dist",
"build": "vite build",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite build; vite preview --host"
Expand Down
92 changes: 70 additions & 22 deletions src/CartItem.jsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,116 @@
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { removeItem, updateQuantity } from './CartSlice';
import './CartItem.css';
import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { removeItem, updateQuantity } from "./CartSlice";
import "./CartItem.css";

const CartItem = ({ onContinueShopping }) => {
const cart = useSelector(state => state.cart.items);
const cart = useSelector((state) => state.cart.items);
console.log("Cart:", cart);
const dispatch = useDispatch();

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

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

return cart.reduce((total, item) => {
const itemCost = parseFloat(item.cost.replace("$", "")) || 0;
const itemQuantity = item.quantity || 0;
return total + itemCost * itemQuantity;
}, 0);
};

const handleContinueShopping = (e) => {

onContinueShopping();
};



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

const handleDecrement = (item) => {

if (item.quantity > 1) {
dispatch(updateQuantity({ ...item, quantity: item.quantity - 1 }));
}
};

const handleRemove = (item) => {
dispatch(removeItem(item.name));
};

// Calculate total cost based on quantity for an item
const calculateTotalCost = (item) => {
const itemCost = parseFloat(item.cost.replace("$", "")) || 0;
const itemQuantity = item.quantity || 0;
return itemCost * itemQuantity;
};

const handleCheckoutShopping = (e) => {
alert("Functionality to be added for future reference");
};

return (
<div className="cart-container">
<h2 style={{ color: 'black' }}>Total Cart Amount: ${calculateTotalAmount()}</h2>
<h2 style={{ color: "black" }}>
Total Cart Amount: ${calculateTotalAmount()}
</h2>
<div>
{cart.map(item => (
{cart.map((item) => (
<div className="cart-item" key={item.name}>
<img className="cart-item-image" src={item.image} alt={item.name} />
<div className="cart-item-details">
<div className="cart-item-name">{item.name}</div>
<div className="cart-item-cost">{item.cost}</div>
<div className="cart-item-quantity">
<button className="cart-item-button cart-item-button-dec" onClick={() => handleDecrement(item)}>-</button>
<span className="cart-item-quantity-value">{item.quantity}</span>
<button className="cart-item-button cart-item-button-inc" onClick={() => handleIncrement(item)}>+</button>
<button
className="cart-item-button cart-item-button-dec"
onClick={() => handleDecrement(item)}
>
-
</button>
<span className="cart-item-quantity-value">
{item.quantity}
</span>
<button
className="cart-item-button cart-item-button-inc"
onClick={() => handleIncrement(item)}
>
+
</button>
</div>
<div className="cart-item-total">
Total: ${calculateTotalCost(item)}
</div>
<div className="cart-item-total">Total: ${calculateTotalCost(item)}</div>
<button className="cart-item-delete" onClick={() => handleRemove(item)}>Delete</button>
<button
className="cart-item-delete"
onClick={() => handleRemove(item)}
>
Delete
</button>
</div>
</div>
))}
</div>
<div style={{ marginTop: '20px', color: 'black' }} className='total_cart_amount'></div>
<div
style={{ marginTop: "20px", color: "black" }}
className="total_cart_amount"
></div>
<div className="continue_shopping_btn">
<button className="get-started-button" onClick={(e) => handleContinueShopping(e)}>Continue Shopping</button>
<button
className="get-started-button"
onClick={(e) => handleContinueShopping(e)}
>
Continue Shopping
</button>
<br />
<button className="get-started-button1">Checkout</button>
<button
onClick={() => handleCheckoutShopping()}
className="get-started-button1"
>
Checkout
</button>
</div>
</div>
);
};

export default CartItem;


20 changes: 15 additions & 5 deletions src/CartSlice.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { createSlice } from '@reduxjs/toolkit';
import { createSlice } from "@reduxjs/toolkit";

export const CartSlice = createSlice({
name: 'cart',
name: "cart",
initialState: {
items: [], // Initialize items as an empty array
},
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
9 changes: 9 additions & 0 deletions src/ProductList.css
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ body {
transition-duration: 0.4s;
cursor: pointer;
}

.product-button:disabled {
background-color: #cccccc; /* Grey background */
color: #666666; /* Darker grey text */
cursor: not-allowed; /* Show "not allowed" cursor */
}
.product-button:disabled:hover {
background-color: #cccccc; /* Cancel hover effect */
}

.product-button:hover {
background-color: #45a049;
Expand Down
Loading