mirror of
https://github.com/vercel/commerce.git
synced 2025-05-24 02:17:01 +00:00
Works
This commit is contained in:
parent
dd9d5497d9
commit
e24e57ad47
@ -1,64 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useEffect, useRef, useState } from 'react';
|
|
||||||
import { useCookies } from 'react-cookie';
|
|
||||||
|
|
||||||
import CartIcon from 'components/icons/cart';
|
|
||||||
import CartModal from './modal';
|
|
||||||
|
|
||||||
import type { Cart } from 'lib/shopify/types';
|
|
||||||
|
|
||||||
export default function CartButton({
|
|
||||||
cart,
|
|
||||||
cartIdUpdated
|
|
||||||
}: {
|
|
||||||
cart: Cart;
|
|
||||||
cartIdUpdated: boolean;
|
|
||||||
}) {
|
|
||||||
const [, setCookie] = useCookies(['cartId']);
|
|
||||||
const [cartIsOpen, setCartIsOpen] = useState(false);
|
|
||||||
const quantityRef = useRef(cart.totalQuantity);
|
|
||||||
|
|
||||||
// Temporary hack to update the `cartId` cookie when it changes since we cannot update it
|
|
||||||
// on the server-side (yet).
|
|
||||||
useEffect(() => {
|
|
||||||
if (cartIdUpdated) {
|
|
||||||
setCookie('cartId', cart.id, {
|
|
||||||
path: '/',
|
|
||||||
sameSite: 'strict',
|
|
||||||
secure: process.env.NODE_ENV === 'production'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}, [setCookie, cartIdUpdated, cart.id]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
// Open cart modal when 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 (!cartIsOpen) {
|
|
||||||
setCartIsOpen(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Always update the quantity reference
|
|
||||||
quantityRef.current = cart.totalQuantity;
|
|
||||||
}
|
|
||||||
}, [cartIsOpen, cart.totalQuantity, quantityRef]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<CartModal isOpen={cartIsOpen} onClose={() => setCartIsOpen(false)} cart={cart} />
|
|
||||||
|
|
||||||
<button
|
|
||||||
aria-label="Open cart"
|
|
||||||
onClick={() => {
|
|
||||||
setCartIsOpen(true);
|
|
||||||
}}
|
|
||||||
className="relative right-0 top-0"
|
|
||||||
data-testid="open-cart"
|
|
||||||
>
|
|
||||||
<CartIcon quantity={cart.totalQuantity} />
|
|
||||||
</button>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
@ -1,6 +1,6 @@
|
|||||||
import { createCart, getCart } from 'lib/shopify';
|
import { createCart, getCart } from 'lib/shopify';
|
||||||
import { cookies } from 'next/headers';
|
import { cookies } from 'next/headers';
|
||||||
import CartButton from './button';
|
import CartModal from './modal';
|
||||||
|
|
||||||
export default async function Cart() {
|
export default async function Cart() {
|
||||||
const cartId = cookies().get('cartId')?.value;
|
const cartId = cookies().get('cartId')?.value;
|
||||||
@ -19,5 +19,5 @@ export default async function Cart() {
|
|||||||
cartIdUpdated = true;
|
cartIdUpdated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return <CartButton cart={cart} cartIdUpdated={cartIdUpdated} />;
|
return <CartModal cart={cart} cartIdUpdated={cartIdUpdated} />;
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
import { Dialog } from '@headlessui/react';
|
import { Dialog } from '@headlessui/react';
|
||||||
import { AnimatePresence, motion } from 'framer-motion';
|
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
|
|
||||||
|
import CartIcon from 'components/icons/cart';
|
||||||
import CloseIcon from 'components/icons/close';
|
import CloseIcon from 'components/icons/close';
|
||||||
import ShoppingBagIcon from 'components/icons/shopping-bag';
|
import ShoppingBagIcon from 'components/icons/shopping-bag';
|
||||||
import Price from 'components/price';
|
import Price from 'components/price';
|
||||||
import { DEFAULT_OPTION } from 'lib/constants';
|
import { DEFAULT_OPTION } from 'lib/constants';
|
||||||
import type { Cart } from 'lib/shopify/types';
|
import type { Cart } from 'lib/shopify/types';
|
||||||
import { createUrl } from 'lib/utils';
|
import { createUrl } from 'lib/utils';
|
||||||
|
import { useEffect, useRef, useState } from 'react';
|
||||||
|
import { useCookies } from 'react-cookie';
|
||||||
import DeleteItemButton from './delete-item-button';
|
import DeleteItemButton from './delete-item-button';
|
||||||
import EditItemQuantityButton from './edit-item-quantity-button';
|
import EditItemQuantityButton from './edit-item-quantity-button';
|
||||||
|
|
||||||
@ -16,172 +20,164 @@ type MerchandiseSearchParams = {
|
|||||||
[key: string]: string;
|
[key: string]: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function CartModal({
|
export default function CartModal({ cart, cartIdUpdated }: { cart: Cart; cartIdUpdated: boolean }) {
|
||||||
isOpen,
|
const [, setCookie] = useCookies(['cartId']);
|
||||||
onClose,
|
const [cartIsOpen, setCartIsOpen] = useState(false);
|
||||||
cart
|
const quantityRef = useRef(cart.totalQuantity);
|
||||||
}: {
|
const openCart = () => setCartIsOpen(true);
|
||||||
isOpen: boolean;
|
const closeCart = () => setCartIsOpen(false);
|
||||||
onClose: () => void;
|
|
||||||
cart: Cart;
|
useEffect(() => {
|
||||||
}) {
|
if (cartIdUpdated) {
|
||||||
|
setCookie('cartId', cart.id, {
|
||||||
|
path: '/',
|
||||||
|
sameSite: 'strict',
|
||||||
|
secure: process.env.NODE_ENV === 'production'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}, [setCookie, cartIdUpdated, cart.id]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Open cart modal when 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 (!cartIsOpen) {
|
||||||
|
setCartIsOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always update the quantity reference
|
||||||
|
quantityRef.current = cart.totalQuantity;
|
||||||
|
}
|
||||||
|
}, [cartIsOpen, cart.totalQuantity, quantityRef]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AnimatePresence initial={false}>
|
<>
|
||||||
{isOpen && (
|
<button aria-label="Open cart" onClick={openCart} data-testid="open-cart">
|
||||||
<Dialog
|
<CartIcon quantity={cart.totalQuantity} />
|
||||||
as={motion.div}
|
</button>
|
||||||
initial="closed"
|
<Dialog open={cartIsOpen} onClose={closeCart} className="relative z-50" data-testid="cart">
|
||||||
animate="open"
|
<div className="fixed inset-0 bg-black/30" aria-hidden="true" />
|
||||||
exit="closed"
|
<Dialog.Panel className="fixed bottom-0 right-0 top-0 flex h-full w-full flex-col bg-white p-6 text-black dark:bg-black dark:text-white md:w-3/5 lg:w-2/5">
|
||||||
key="dialog"
|
<div className="flex items-center justify-between">
|
||||||
static
|
<p className="text-lg font-bold">My Cart</p>
|
||||||
open={isOpen}
|
<button
|
||||||
onClose={onClose}
|
aria-label="Close cart"
|
||||||
className="relative z-50"
|
onClick={closeCart}
|
||||||
>
|
className="text-black transition-colors hover:text-gray-500 dark:text-gray-100"
|
||||||
<motion.div
|
data-testid="close-cart"
|
||||||
variants={{
|
|
||||||
open: { opacity: 1, backdropFilter: 'blur(0.5px)' },
|
|
||||||
closed: { opacity: 0, backdropFilter: 'blur(0px)' }
|
|
||||||
}}
|
|
||||||
className="fixed inset-0 bg-black/30"
|
|
||||||
aria-hidden="true"
|
|
||||||
/>
|
|
||||||
|
|
||||||
<div className="fixed inset-0 flex justify-end" data-testid="cart">
|
|
||||||
<Dialog.Panel
|
|
||||||
as={motion.div}
|
|
||||||
variants={{
|
|
||||||
open: { translateX: 0 },
|
|
||||||
closed: { translateX: '100%' }
|
|
||||||
}}
|
|
||||||
transition={{ type: 'spring', bounce: 0, duration: 0.3 }}
|
|
||||||
className="flex w-full flex-col bg-white p-8 text-black dark:bg-black dark:text-white md:w-3/5 lg:w-2/5"
|
|
||||||
>
|
>
|
||||||
<div className="flex items-center justify-between">
|
<CloseIcon className="h-7" />
|
||||||
<p className="text-lg font-bold">My Cart</p>
|
</button>
|
||||||
<button
|
|
||||||
aria-label="Close cart"
|
|
||||||
onClick={onClose}
|
|
||||||
className="text-black transition-colors hover:text-gray-500 dark:text-gray-100"
|
|
||||||
data-testid="close-cart"
|
|
||||||
>
|
|
||||||
<CloseIcon className="h-7" />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{cart.lines.length === 0 ? (
|
|
||||||
<div className="mt-20 flex w-full flex-col items-center justify-center overflow-hidden">
|
|
||||||
<ShoppingBagIcon className="h-16" />
|
|
||||||
<p className="mt-6 text-center text-2xl font-bold">Your cart is empty.</p>
|
|
||||||
</div>
|
|
||||||
) : null}
|
|
||||||
{cart.lines.length !== 0 ? (
|
|
||||||
<div className="flex h-full flex-col justify-between overflow-hidden">
|
|
||||||
<ul className="flex-grow overflow-auto p-6">
|
|
||||||
{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 (
|
|
||||||
<li key={i} data-testid="cart-item">
|
|
||||||
<Link
|
|
||||||
className="flex flex-row space-x-4 py-4"
|
|
||||||
href={merchandiseUrl}
|
|
||||||
onClick={onClose}
|
|
||||||
>
|
|
||||||
<div className="relative h-16 w-16 cursor-pointer overflow-hidden bg-white">
|
|
||||||
<Image
|
|
||||||
className="h-full w-full object-cover"
|
|
||||||
width={64}
|
|
||||||
height={64}
|
|
||||||
alt={
|
|
||||||
item.merchandise.product.featuredImage.altText ||
|
|
||||||
item.merchandise.product.title
|
|
||||||
}
|
|
||||||
src={item.merchandise.product.featuredImage.url}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div className="flex flex-1 flex-col text-base">
|
|
||||||
<span className="font-semibold">
|
|
||||||
{item.merchandise.product.title}
|
|
||||||
</span>
|
|
||||||
{item.merchandise.title !== DEFAULT_OPTION ? (
|
|
||||||
<p className="text-sm" data-testid="cart-product-variant">
|
|
||||||
{item.merchandise.title}
|
|
||||||
</p>
|
|
||||||
) : null}
|
|
||||||
</div>
|
|
||||||
<Price
|
|
||||||
className="flex flex-col justify-between space-y-2 text-sm"
|
|
||||||
amount={item.cost.totalAmount.amount}
|
|
||||||
currencyCode={item.cost.totalAmount.currencyCode}
|
|
||||||
/>
|
|
||||||
</Link>
|
|
||||||
<div className="flex h-9 flex-row">
|
|
||||||
<DeleteItemButton item={item} />
|
|
||||||
<p className="ml-2 flex w-full items-center justify-center border dark:border-gray-700">
|
|
||||||
<span className="w-full px-2">{item.quantity}</span>
|
|
||||||
</p>
|
|
||||||
<EditItemQuantityButton item={item} type="minus" />
|
|
||||||
<EditItemQuantityButton item={item} type="plus" />
|
|
||||||
</div>
|
|
||||||
</li>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</ul>
|
|
||||||
<div className="border-t border-gray-200 pt-2 text-sm text-black dark:text-white">
|
|
||||||
<div className="mb-2 flex items-center justify-between">
|
|
||||||
<p>Subtotal</p>
|
|
||||||
<Price
|
|
||||||
className="text-right"
|
|
||||||
amount={cart.cost.subtotalAmount.amount}
|
|
||||||
currencyCode={cart.cost.subtotalAmount.currencyCode}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div className="mb-2 flex items-center justify-between">
|
|
||||||
<p>Taxes</p>
|
|
||||||
<Price
|
|
||||||
className="text-right"
|
|
||||||
amount={cart.cost.totalTaxAmount.amount}
|
|
||||||
currencyCode={cart.cost.totalTaxAmount.currencyCode}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div className="mb-2 flex items-center justify-between border-b border-gray-200 pb-2">
|
|
||||||
<p>Shipping</p>
|
|
||||||
<p className="text-right">Calculated at checkout</p>
|
|
||||||
</div>
|
|
||||||
<div className="mb-2 flex items-center justify-between font-bold">
|
|
||||||
<p>Total</p>
|
|
||||||
<Price
|
|
||||||
className="text-right"
|
|
||||||
amount={cart.cost.totalAmount.amount}
|
|
||||||
currencyCode={cart.cost.totalAmount.currencyCode}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<a
|
|
||||||
href={cart.checkoutUrl}
|
|
||||||
className="flex w-full items-center justify-center bg-black p-3 text-sm font-medium uppercase text-white opacity-90 hover:opacity-100 dark:bg-white dark:text-black"
|
|
||||||
>
|
|
||||||
<span>Proceed to Checkout</span>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
) : null}
|
|
||||||
</Dialog.Panel>
|
|
||||||
</div>
|
</div>
|
||||||
</Dialog>
|
|
||||||
)}
|
{cart.lines.length === 0 ? (
|
||||||
</AnimatePresence>
|
<div className="mt-20 flex w-full flex-col items-center justify-center overflow-hidden">
|
||||||
|
<ShoppingBagIcon className="h-16" />
|
||||||
|
<p className="mt-6 text-center text-2xl font-bold">Your cart is empty.</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="flex h-full flex-col justify-between overflow-hidden">
|
||||||
|
<ul className="flex-grow overflow-auto p-6">
|
||||||
|
{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 (
|
||||||
|
<li key={i} data-testid="cart-item">
|
||||||
|
<Link
|
||||||
|
className="flex flex-row space-x-4 py-4"
|
||||||
|
href={merchandiseUrl}
|
||||||
|
onClick={closeCart}
|
||||||
|
>
|
||||||
|
<div className="relative h-16 w-16 cursor-pointer overflow-hidden bg-white">
|
||||||
|
<Image
|
||||||
|
className="h-full w-full object-cover"
|
||||||
|
width={64}
|
||||||
|
height={64}
|
||||||
|
alt={
|
||||||
|
item.merchandise.product.featuredImage.altText ||
|
||||||
|
item.merchandise.product.title
|
||||||
|
}
|
||||||
|
src={item.merchandise.product.featuredImage.url}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-1 flex-col text-base">
|
||||||
|
<span className="font-semibold">{item.merchandise.product.title}</span>
|
||||||
|
{item.merchandise.title !== DEFAULT_OPTION ? (
|
||||||
|
<p className="text-sm" data-testid="cart-product-variant">
|
||||||
|
{item.merchandise.title}
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
<Price
|
||||||
|
className="flex flex-col justify-between space-y-2 text-sm"
|
||||||
|
amount={item.cost.totalAmount.amount}
|
||||||
|
currencyCode={item.cost.totalAmount.currencyCode}
|
||||||
|
/>
|
||||||
|
</Link>
|
||||||
|
<div className="flex h-9 flex-row">
|
||||||
|
<DeleteItemButton item={item} />
|
||||||
|
<p className="ml-2 flex w-full items-center justify-center border dark:border-gray-700">
|
||||||
|
<span className="w-full px-2">{item.quantity}</span>
|
||||||
|
</p>
|
||||||
|
<EditItemQuantityButton item={item} type="minus" />
|
||||||
|
<EditItemQuantityButton item={item} type="plus" />
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</ul>
|
||||||
|
<div className="border-t border-gray-200 pt-2 text-sm text-black dark:text-white">
|
||||||
|
<div className="mb-2 flex items-center justify-between">
|
||||||
|
<p>Subtotal</p>
|
||||||
|
<Price
|
||||||
|
className="text-right"
|
||||||
|
amount={cart.cost.subtotalAmount.amount}
|
||||||
|
currencyCode={cart.cost.subtotalAmount.currencyCode}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="mb-2 flex items-center justify-between">
|
||||||
|
<p>Taxes</p>
|
||||||
|
<Price
|
||||||
|
className="text-right"
|
||||||
|
amount={cart.cost.totalTaxAmount.amount}
|
||||||
|
currencyCode={cart.cost.totalTaxAmount.currencyCode}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="mb-2 flex items-center justify-between border-b border-gray-200 pb-2">
|
||||||
|
<p>Shipping</p>
|
||||||
|
<p className="text-right">Calculated at checkout</p>
|
||||||
|
</div>
|
||||||
|
<div className="mb-2 flex items-center justify-between font-bold">
|
||||||
|
<p>Total</p>
|
||||||
|
<Price
|
||||||
|
className="text-right"
|
||||||
|
amount={cart.cost.totalAmount.amount}
|
||||||
|
currencyCode={cart.cost.totalAmount.currencyCode}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<a
|
||||||
|
href={cart.checkoutUrl}
|
||||||
|
className="flex w-full items-center justify-center bg-black p-3 text-sm font-medium uppercase text-white opacity-90 hover:opacity-100 dark:bg-white dark:text-black"
|
||||||
|
>
|
||||||
|
<span>Proceed to Checkout</span>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</Dialog.Panel>
|
||||||
|
</Dialog>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user