import { ShoppingCart, Trash } from 'iconsax-react' import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover" import { useGetCart } from '@/app/cart/hooks/useCartData' import { useRemoveFromCart } from '@/app/cart/hooks/useCartData' import { CartItemType } from '@/app/cart/types/Types' import { NumberFormat } from '@/config/func' import Image from 'next/image' import Link from 'next/link' import { Button } from '@/components/ui/button' const Cart = () => { const { data: cartData, isLoading } = useGetCart() const removeFromCartMutation = useRemoveFromCart() const handleRemoveItem = async (productId: number, variantId: string) => { try { await removeFromCartMutation.mutateAsync({ productId, variantId }) } catch (error) { console.error('Error removing item from cart:', error) } } const cart = cartData?.results?.cart const items = cart?.items || [] const itemsCount = cart?.items_count || 0 return (
{itemsCount > 0 && (
{itemsCount}
)}
{/* Header */} { itemsCount > 0 && (
{itemsCount} کالا در سبد خرید شما ثبت شده
) } {/* Cart Items */}
{isLoading ? (
در حال بارگذاری...
) : items.length > 0 ? ( items.slice(0, 3).map((item: CartItemType) => ( handleRemoveItem(item.product._id, item.variant._id)} /> )) ) : (
سبد خرید شما خالی است
)}
{/* View Cart Button */} {itemsCount > 0 && ( )}
) } // Cart Item Card Component const CartItemCard = ({ item, onRemove }: { item: CartItemType, onRemove: () => void }) => { return (
{/* Product Image */}
{item.product.title_fa}
{/* Product Details */}
{item.product.category?._id || 'دسته‌بندی'}
{item.product.title_fa}
{NumberFormat(item.variant.price.selling_price)} تومان
) } export default Cart