'use client'; import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger } from '@/components/ui/sheet'; import { ShoppingBagIcon } from '@heroicons/react/24/outline'; import Price from 'components/price'; import { DEFAULT_OPTION } from 'lib/constants'; import type { Cart } from 'lib/shopify/types'; import { createUrl } from 'lib/utils'; import { useTranslations } from 'next-intl'; import Link from 'next-intl/link'; import Image from 'next/image'; import { useEffect, useRef, useState } from 'react'; import DeleteItemButton from './delete-item-button'; import EditItemQuantityButton from './edit-item-quantity-button'; import OpenCart from './open-cart'; type MerchandiseSearchParams = { [key: string]: string; }; export default function CartModal({ cart }: { cart: Cart | undefined }) { const [isOpen, setIsOpen] = useState(false); const quantityRef = useRef(cart?.totalQuantity); const t = useTranslations('cart'); useEffect(() => { // Open cart modal when quantity changes. if (cart?.totalQuantity !== quantityRef.current) { // But only if it's not already open (quantity also changes when editing items in cart). if (!isOpen) { setIsOpen(true); } // Always update the quantity reference quantityRef.current = cart?.totalQuantity; } }, [isOpen, cart?.totalQuantity, quantityRef]); return ( <> setIsOpen(!isOpen)}> {t('myCartTitle')} {!cart || cart.lines.length === 0 ? (

{t('empty.title')}

) : (
    {cart.lines.map((item, i) => { const merchandiseSearchParams = {} as MerchandiseSearchParams; item.merchandise.selectedOptions.forEach(({ name, value }) => { if (value !== DEFAULT_OPTION) { merchandiseSearchParams[name.toLowerCase()] = value; } }); const merchandiseUrl = createUrl( `/product/${item.merchandise.product.handle}`, new URLSearchParams(merchandiseSearchParams) ); return (
  • setIsOpen(false)} className="z-30 flex flex-row space-x-4" >
    {
    {item.merchandise.product.title} {item.merchandise.title !== DEFAULT_OPTION ? (

    {item.merchandise.title}

    ) : null}

    {item.quantity}

  • ); })}

Taxes

Shipping

{t('calculatedAtCheckout')}

Total

{t('proceedToCheckout')}
)}

Line item examples

  • setIsOpen(false)} className="z-30 flex flex-row space-x-4" >
    Product title

    Option of product

  • setIsOpen(false)} className="z-30 flex flex-row space-x-4" >
    Product title

    Option of product

); }