CartSidebar.jsx
1.83 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
import React from 'react'
import { Link } from 'react-router-dom'
import { useCart } from '../../context/CartContext'
import CartItem from './CartItem'
import './cart.css'
const CartSidebar = ({ isOpen, onClose }) => {
const { cartItems, getCartTotal, getCartItemsCount } = useCart()
if (!isOpen) return null
return (
<div className="cart-sidebar-overlay" onClick={onClose}>
<div className="cart-sidebar" onClick={(e) => e.stopPropagation()}>
<div className="cart-header">
<h2>Shopping Cart ({getCartItemsCount()})</h2>
<button onClick={onClose} className="close-button">×</button>
</div>
<div className="cart-content">
{cartItems.length === 0 ? (
<div className="empty-cart">
<p>Your cart is empty</p>
<Link to="/products" onClick={onClose} className="continue-shopping">
Continue Shopping
</Link>
</div>
) : (
<>
<div className="cart-items">
{cartItems.map(item => (
<CartItem key={item.product._id} item={item} />
))}
</div>
<div className="cart-footer">
<div className="cart-total">
<strong>Total: ${getCartTotal().toFixed(2)}</strong>
</div>
<div className="cart-actions">
<Link to="/cart" onClick={onClose} className="view-cart-button">
View Cart
</Link>
<Link to="/checkout" onClick={onClose} className="checkout-button">
Checkout
</Link>
</div>
</div>
</>
)}
</div>
</div>
</div>
)
}
export default CartSidebar