-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShoppingCart.js
More file actions
95 lines (83 loc) · 3.29 KB
/
Copy pathShoppingCart.js
File metadata and controls
95 lines (83 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// frontend/src/components/ShoppingCart.js
import React from 'react';
function ShoppingCart({ cart, setCart }) {
// Function to remove an item completely
const removeItem = (id) => {
setCart(prevCart => prevCart.filter(item => item.id !== id));
};
// Function to update quantity
const updateQuantity = (id, delta) => {
setCart(prevCart => {
const updatedCart = prevCart.map(item => {
if (item.id === id) {
const newQuantity = item.quantity + delta;
if (newQuantity <= 0) {
return null;
}
return { ...item, quantity: newQuantity };
}
return item;
}).filter(item => item !== null);
return updatedCart;
});
};
// Calculate Totals
const subtotal = cart.reduce((total, item) => total + (item.price * item.quantity), 0);
const taxRate = 0.10;
const tax = subtotal * taxRate;
const total = subtotal + tax;
if (cart.length === 0) {
return <h1 className="status-message">Your Shopping Cart is Empty.</h1>;
}
return (
<div className="shopping-cart-page">
<h1 className="page-title">Shopping Cart</h1>
<div className="cart-layout">
<div className="cart-items">
{cart.map(item => (
<div key={item.id} className="cart-item-card">
<img src={item.image} alt={item.name} className="cart-item-image" />
<div className="cart-item-details">
{/* Currency Change: $ to ₹ */}
<h3>{item.name}</h3>
<p>Price: ₹{item.price.toFixed(2)}</p>
<div className="quantity-controls">
{/* NOTE: The minus button here will remove the item if count hits 0 */}
<button onClick={() => updateQuantity(item.id, -1)} className="qty-btn">-</button>
<span className="qty-display">{item.quantity}</span>
<button onClick={() => updateQuantity(item.id, 1)} className="qty-btn">+</button>
<button onClick={() => removeItem(item.id)} className="remove-btn">Remove</button>
</div>
</div>
{/* Item Subtotal: Rupee (₹) and NO ** */}
<p className="item-subtotal">₹{(item.price * item.quantity).toFixed(2)}</p>
</div>
))}
</div>
{/* Order Summary/Checkout Box (Amazon-style) */}
<div className="order-summary">
<h2>Order Summary</h2>
<div className="summary-line">
<span>Subtotal:</span>
<span>₹{subtotal.toFixed(2)}</span>
</div>
<div className="summary-line">
<span>Shipping:</span>
<span>FREE</span>
</div>
<div className="summary-line">
<span>Tax (10%):</span>
<span>₹{tax.toFixed(2)}</span>
</div>
<div className="summary-line total-line">
<span>Order Total:</span>
{/* Order Total: Rupee (₹) and NO ** */}
<span>₹{total.toFixed(2)}</span>
</div>
<button className="checkout-btn">Proceed to Checkout</button>
</div>
</div>
</div>
);
}
export default ShoppingCart;