'use client';
import React, { useState, useEffect } from 'react';
import Link from 'next/link';
import Image from 'next/image';
import { useCart, CartItem } from '@/app/context/CartContext';
import './CartSidebar.scss';
const CartItemComponent: React.FC<{item: CartItem}> = ({ item }) => {
const { updateQuantity, removeFromCart } = useCart();
const incrementQuantity = () => {
updateQuantity(item.id, item.quantity + 1);
};
const decrementQuantity = () => {
if (item.quantity > 1) {
updateQuantity(item.id, item.quantity - 1);
} else {
if (window.confirm('Remove this item from your cart?')) {
removeFromCart(item.id);
}
}
};
return (
{item.product_name}
${item.price.toFixed(2)}
{item.originalPrice && (
${item.originalPrice.toFixed(2)}
)}
{item.quantity}
);
};
const CartSidebar: React.FC = () => {
const { cartItems, isCartOpen, closeCart, totalItems, totalPrice, clearCart } = useCart();
const [isClosing, setIsClosing] = useState(false);
const handleClose = () => {
setIsClosing(true);
setTimeout(() => {
closeCart();
setIsClosing(false);
}, 300);
};
useEffect(() => {
if (isCartOpen) {
setIsClosing(false);
}
}, [isCartOpen]);
if (!isCartOpen) return null;
return (
<>
Your Cart ({totalItems})
{cartItems.length > 0 ? (
cartItems.map(item =>
)
) : (
Your cart is empty
Continue Shopping
)}
{cartItems.length > 0 && (
Subtotal
${totalPrice.toFixed(2)}
Proceed to Checkout
)}
>
);
};
export default CartSidebar;