"use client"; import { Dialog, Transition } from "@headlessui/react"; import { ShoppingCartIcon, XMarkIcon } from "@heroicons/react/24/outline"; import Price from "components/price"; import { DEFAULT_OPTION } from "lib/constants"; import { createUrl } from "lib/utils"; import { getImageUrl } from "lib/utils/image"; import Image from "next/image"; import Link from "next/link"; import { Fragment, useEffect, useRef } from "react"; import { useCart } from "./cart-context"; import { EditItemQuantityButton } from "./edit-item-quantity-button"; type MerchandiseSearchParams = { [key: string]: string; }; export default function CartModal() { const { cart, updateCartItem, removeCartItem } = useCart(); const quantityRef = useRef(cart.totalQuantity); const openCart = () => {}; const closeCart = () => {}; useEffect(() => { // Open cart modal when quantity changes. if (cart.totalQuantity !== quantityRef.current) { // But only if it's not already open. openCart(); } quantityRef.current = cart.totalQuantity; }, [cart.totalQuantity, openCart]); return ( <> {cart.totalQuantity > 0 && ( {cart.totalQuantity} )} Cart Close panel {cart.lines.length === 0 ? ( Your cart is empty. ) : ( {cart.lines .sort((a, b) => a.merchandise.product.title.localeCompare( b.merchandise.product.title ) ) .map((item) => { 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 ( { item.merchandise.product .title } {item.merchandise.title} { updateCartItem( item.merchandise.id, item.quantity - 1 ); }} /> {item.quantity} { updateCartItem( item.merchandise.id, item.quantity + 1 ); }} /> removeCartItem( item.merchandise.id ) } > Remove ); })} )} {cart.lines.length > 0 ? ( Subtotal Taxes Total Checkout or{" "} Continue Shopping → ) : null} > ); }
Your cart is empty.
{item.merchandise.title}
{item.quantity}
Subtotal
Taxes
Total
or{" "} Continue Shopping →