Cart.jsx
2.76 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
import React from 'react'
import { Link } from 'react-router-dom'
import { useCart } from '../context/CartContext'
import { useAuth } from '../context/AuthContext'
import CartItem from '../components/cart/CartItem'
import LoadingSpinner from '../components/common/LoadingSpinner'
const Cart = () => {
const { cartItems, getCartTotal, getCartItemsCount, loading } = useCart()
const { isAuthenticated } = useAuth()
if (loading) {
return <LoadingSpinner />
}
if (cartItems.length === 0) {
return (
<div className="cart-page">
<div className="container">
<div className="empty-cart">
<h1>Your Cart is Empty</h1>
<p>Add some products to your cart to continue shopping</p>
<Link to="/products" className="continue-shopping">
Continue Shopping
</Link>
</div>
</div>
</div>
)
}
return (
<div className="cart-page">
<div className="container">
<div className="cart-header">
<h1>Shopping Cart ({getCartItemsCount()})</h1>
</div>
<div className="cart-content">
<div className="cart-items-section">
{cartItems.map(item => (
<CartItem key={item.product._id} item={item} />
))}
</div>
<div className="cart-summary">
<div className="summary-card">
<h3>Order Summary</h3>
<div className="summary-row">
<span>Subtotal ({getCartItemsCount()} items)</span>
<span>${getCartTotal().toFixed(2)}</span>
</div>
<div className="summary-row">
<span>Shipping</span>
<span>Free</span>
</div>
<div className="summary-row">
<span>Tax</span>
<span>${(getCartTotal() * 0.1).toFixed(2)}</span>
</div>
<div className="summary-row total">
<strong>Total</strong>
<strong>${(getCartTotal() * 1.1).toFixed(2)}</strong>
</div>
{isAuthenticated ? (
<Link to="/checkout" className="checkout-button">
Proceed to Checkout
</Link>
) : (
<div className="login-required">
<p>Please log in to checkout</p>
<Link to="/login" className="login-button">
Login
</Link>
</div>
)}
<Link to="/products" className="continue-shopping">
Continue Shopping
</Link>
</div>
</div>
</div>
</div>
</div>
)
}
export default Cart