'use client'; import { Dialog, Transition } from '@headlessui/react'; import { ShoppingCartIcon } from '@heroicons/react/24/outline'; import LoadingDots from 'components/loading-dots'; import Price from 'components/price'; import { useSession } from 'next-auth/react'; import Image from 'next/image'; import Link from 'next/link'; import { Fragment, useEffect, useState } from 'react'; import { useFormStatus } from 'react-dom'; import { useCart } from './cart-context'; import CloseCart from './close-cart'; import { DeleteItemButton } from './delete-item-button'; import { EditItemQuantityButton } from './edit-item-quantity-button'; import OpenCart from './open-cart'; export default function CartModal() { const { cart, setNewCart } = useCart(); const [isOpen, setIsOpen] = useState(false); const [userIsLoggedIn, setUserIsLoggedIn] = useState(false); const openCart = () => setIsOpen(true); const closeCart = () => setIsOpen(false); const { data } = useSession(); useEffect(() => { if (data?.user.token) { const fetchCart = async () => { const cart = await (await fetch('/api/cart')).json(); setNewCart(cart); }; fetchCart(); } }, [data]); return ( <> item.quantity) .reduce((a, b) => a + b, 0) .toString() ?? '0' } /> My Cart {!cart || cart.items?.length === 0 ? ( Your cart is empty. ) : ( {cart.items?.length && cart.items .sort((a, b) => a.name.localeCompare(b.name)) .map((item, i) => { return ( {item.name} {item.variation.map((variation, i) => ( {variation.attribute}: {variation.value} ))} {item.quantity} ); })} Taxes Shipping Calculated at checkout Total )} > ); } function CheckoutButton() { const { pending } = useFormStatus(); return ( {pending ? : 'Proceed to Checkout'} ); }
My Cart
Your cart is empty.
{item.quantity}
Taxes
Shipping
Calculated at checkout
Total