import { useState, useEffect } from "react"; import { ShoppingCart, Trash2, ExternalLink, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle, SheetTrigger, } from "@/components/ui/sheet"; import { useCartStore } from "@/stores/cartStore"; export const CartDrawer = () => { const [isOpen, setIsOpen] = useState(false); const { items, isLoading, isSyncing, removeItem, clearCart, getCheckoutUrl, syncCart } = useCartStore(); const totalItems = items.reduce((sum, item) => sum + item.quantity, 0); const totalPrice = items.reduce((sum, item) => sum + (parseFloat(item.price.amount) * item.quantity), 0); // Sync cart with Shopify when drawer opens useEffect(() => { if (isOpen) syncCart(); }, [isOpen, syncCart]); const handleCheckout = () => { const checkoutUrl = getCheckoutUrl(); if (checkoutUrl) { window.open(checkoutUrl, '_blank'); setIsOpen(false); } }; const handleRemoveItem = async (variantId: string) => { await removeItem(variantId); }; if (totalItems === 0) { return null; } return ( Varukorg {totalItems === 0 ? "Din varukorg är tom" : `${totalItems} produkt${totalItems !== 1 ? 'er' : ''} i varukorgen`}
{items.length === 0 ? (

Din varukorg är tom

) : ( <>
{items.map((item) => (

{item.product.node.title}

Antal: {item.quantity}

{parseFloat(item.price.amount).toLocaleString('sv-SE')} {item.price.currencyCode}

))}
Totalt {totalPrice.toLocaleString('sv-SE')} {items[0]?.price.currencyCode || 'SEK'}
)}
); }; export default CartDrawer;