'use client'; import { Dialog, Transition } from '@headlessui/react'; import { ShoppingCartIcon } 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 Image from 'next/image'; import Link from 'next/link'; import { Fragment, useEffect, useRef, useState } from 'react'; import CloseCart from './close-cart'; import { DeleteItemButton } from './delete-item-button'; import { EditItemQuantityButton } from './edit-item-quantity-button'; import OpenCart from './open-cart'; type MerchandiseSearchParams = { [key: string]: string; }; function TierDiscount({ subTotal }: { subTotal: number }) { const discountGroups = [ { name: 'Tier 1', discount: { amount: 0.05, minimumSpent: 10 } }, { name: 'Tier 2', discount: { amount: 0.1, minimumSpent: 20 } } ]; const highestMinimumSpent = discountGroups.reduce((acc, group) => { if (group.discount.minimumSpent > acc) { return group.discount.minimumSpent; } else { return acc; } }, 0); const eligibleDiscountGroups = discountGroups.filter( (group) => subTotal >= group.discount.minimumSpent ); const finalDiscount = eligibleDiscountGroups.reduce((prev, current) => { // Compare and return the highest discount group based on your criteria return prev.discount.minimumSpent > current.discount.minimumSpent ? prev : current; }); const discountAmount = finalDiscount.discount.amount * 100; return (

Spent more

{discountAmount}% off

); } export default function CartModal({ cart }: { cart: Cart | undefined }) { const [isOpen, setIsOpen] = useState(false); const quantityRef = useRef(cart?.totalQuantity); const openCart = () => setIsOpen(true); const closeCart = () => setIsOpen(false); 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 ( <> ); }