import React from 'react';
import {
Box,
Typography,
Button,
Paper,
Grid,
Divider,
Card,
CardMedia,
IconButton,
TextField,
CircularProgress,
Alert,
Breadcrumbs,
Link
} from '@mui/material';
import AddIcon from '@mui/icons-material/Add';
import RemoveIcon from '@mui/icons-material/Remove';
import DeleteIcon from '@mui/icons-material/Delete';
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart';
import NavigateNextIcon from '@mui/icons-material/NavigateNext';
import { Link as RouterLink, useNavigate } from 'react-router-dom';
import { useAuth } from '@hooks/reduxHooks';
import { useGetCart, useUpdateCartItem, useClearCart } from '.@hooks/apiHooks';
import imageUtils from '@utils/imageUtils';
const CartPage = () => {
const navigate = useNavigate();
const { user } = useAuth();
// Get cart data
const { data: cart, isLoading, error } = useGetCart(user?.id);
// Cart mutations
const updateCartItem = useUpdateCartItem();
const clearCart = useClearCart();
// Handle quantity change
const handleUpdateQuantity = (productId, newQuantity) => {
updateCartItem.mutate({
userId: user.id,
productId,
quantity: newQuantity
});
};
// Handle remove item
const handleRemoveItem = (productId) => {
updateCartItem.mutate({
userId: user.id,
productId,
quantity: 0
});
};
// Handle clear cart
const handleClearCart = () => {
clearCart.mutate(user.id);
};
// Handle proceed to checkout
const handleCheckout = () => {
navigate('/checkout');
};
// Loading state
if (isLoading) {
return (
);
}
// Error state
if (error) {
return (
Error loading your cart. Please try again.
);
}
// Empty cart state
if (!cart || !cart.items || cart.items.length === 0) {
return (
Your Cart is Empty
Looks like you haven't added any items to your cart yet.
);
}
return (
{/* Breadcrumbs navigation */}
}
aria-label="breadcrumb"
sx={{ mb: 3 }}
>
Home
Your Cart
Your Shopping Cart
{/* Cart items */}
Cart Items ({cart.itemCount})
}
>
Clear Cart
{cart.items.map((item) => (
{/* Product image */}
{/* Product details */}
{item.name}
Category: {item.category_name}
Price: ${parseFloat(item.price).toFixed(2)}
{/* Quantity controls */}
handleUpdateQuantity(item.product_id, item.quantity - 1)}
disabled={item.quantity <= 1 || updateCartItem.isLoading}
>
{
const value = parseInt(e.target.value);
if (!isNaN(value) && value > 0) {
handleUpdateQuantity(item.product_id, value);
}
}}
inputProps={{ min: 1, style: { textAlign: 'center' } }}
size="small"
sx={{ width: 40, mx: 1 }}
/>
handleUpdateQuantity(item.product_id, item.quantity + 1)}
disabled={updateCartItem.isLoading}
>
{/* Subtotal and remove */}
${(parseFloat(item.price) * item.quantity).toFixed(2)}
handleRemoveItem(item.product_id)}
disabled={updateCartItem.isLoading}
sx={{ ml: 1 }}
>
))}
{/* Order summary */}
Order Summary
Subtotal ({cart.itemCount} items)
${cart.total.toFixed(2)}
Shipping
Free
Total
${cart.total.toFixed(2)}
);
};
export default CartPage;