-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShoppingCart.js
93 lines (91 loc) · 3.76 KB
/
ShoppingCart.js
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
import React from 'react';
function ShoppingCart({
cartCourses,
deleteCourseFromCartFunction,
totalAmountCalculationFunction,
setCartCourses,
}) {
return (
<div className={`cart ${cartCourses.length > 0 ? 'active' : ''}`}>
<h2>My Cart</h2>
{cartCourses.length === 0 ? (
<p className="empty-cart">Geek, your cart is empty.</p>
) : (
<div>
<ul>
{cartCourses.map((item) => (
<li key={item.product.id} className="cart-item">
<div>
<div className="item-info">
<div className="item-image">
<img src={item.product.image}
alt={item.product.name} />
</div>
<div className="item-details">
<h3>{item.product.name}</h3>
<p>Price: ₹{item.product.price}</p>
</div>
</div>
<div>
<div className="item-actions">
<button
className="remove-button"
onClick={() =>
deleteCourseFromCartFunction(item.product)}>
Remove Product
</button>
<div className="quantity">
<button style={{ margin: "1%" }}
onClick={(e) => {
setCartCourses((prevCartCourses) => {
const updatedCart = prevCartCourses.map(
(prevItem) =>
prevItem.product.id === item.product.id
? { ...prevItem, quantity:
item.quantity + 1 }
: prevItem
);
return updatedCart;
})
}}>+</button>
<p className='quant'>{item.quantity} </p>
<button
onClick={(e) => {
setCartCourses((prevCartCourses) => {
const updatedCart = prevCartCourses.map(
(prevItem) =>
prevItem.product.id === item.product.id
? { ...prevItem, quantity:
Math.max(item.quantity - 1, 0) }
: prevItem
);
return updatedCart;
})
}}>-</button>
</div>
</div>
</div>
</div>
</li>
))}
</ul>
<div className="checkout-section">
<div className="checkout-total">
<p className="total">Total Amount:
₹{totalAmountCalculationFunction()}
</p>
</div>
<button
className="checkout-button"
disabled={cartCourses.length === 0 ||
totalAmountCalculationFunction() === 0}
>
Proceed to Payment
</button>
</div>
</div>
)}
</div>
);
}
export default ShoppingCart;