diff --git a/src/components/CartDrawer.tsx b/src/components/CartDrawer.tsx new file mode 100644 index 0000000..e4150be --- /dev/null +++ b/src/components/CartDrawer.tsx @@ -0,0 +1,149 @@ +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; diff --git a/src/components/Header.tsx b/src/components/Header.tsx index 2caf161..708ecbd 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -1,4 +1,5 @@ import AccountMenu from "./AccountMenu"; +import CartDrawer from "./CartDrawer"; const Header = () => { return ( @@ -11,7 +12,10 @@ const Header = () => { src="/lovable-uploads/bc36fcd7-0c6f-48d2-9921-4323413739c7.png" /> - +
+ + +
);