Skip to content

aded #251

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

aded #251

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 index.h1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
code goes here
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -12,4 +12,4 @@
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
</html>
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@
"scripts": {
"dev": "vite",
"build": "vite build",
"predeploy": "npm run build",
"deploy": "gh-pages -d dist",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite build; vite preview --host"
},
97 changes: 73 additions & 24 deletions src/CartItem.jsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,117 @@
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 CartItem = ({ onContinueShopping, setAddedToCart }) => {
const cart = useSelector((state) => state.cart.items);
const dispatch = useDispatch();

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

const calculateTotalAmount = (items) => {
let totalPrice = 0;
items.forEach((item) => {
totalPrice += parseFloat(item.cost.replace("$", "")) * item.quantity;
});
return totalPrice;
};

const handleContinueShopping = (e) => {

onContinueShopping(e);
};


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

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

const handleDecrement = (item) => {

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

const handleRemove = (item) => {
const nameToRemove = item.name;
dispatch(removeItem(nameToRemove));
setAddedToCart(({ [nameToRemove]: _, ...rest }) => rest);
};

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

return (
<div className="cart-container">
<h2 style={{ color: 'black' }}>Total Cart Amount: ${calculateTotalAmount()}</h2>
<h2 style={{ color: "black" }}>
Total Cart Amount: ${calculateTotalAmount(cart)}
</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
className="get-started-button1"
onClick={(e) => handleCheckoutShopping(e)}
>
Checkout
</button>
</div>
</div>
);
};

export default CartItem;


export default CartItem;
17 changes: 14 additions & 3 deletions src/CartSlice.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import { createSlice } from '@reduxjs/toolkit';

export const CartSlice = createSlice({
@@ -7,13 +8,23 @@ 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) => {


const { name, quantity } = action.payload;
const itemToUpdate = state.items.find(item => item.name === name);
if (itemToUpdate) {
itemToUpdate.quantity = quantity;
}
},
},
});
Loading
Oops, something went wrong.