2023-04-17 23:00:47 -04:00
|
|
|
'use client';
|
|
|
|
|
2023-07-24 21:40:29 -05:00
|
|
|
import { PlusIcon } from '@heroicons/react/24/outline';
|
2023-04-25 09:38:47 -05:00
|
|
|
import clsx from 'clsx';
|
2023-06-17 13:18:00 -05:00
|
|
|
import { addItem } from 'components/cart/actions';
|
2023-04-17 23:00:47 -04:00
|
|
|
import LoadingDots from 'components/loading-dots';
|
|
|
|
import { ProductVariant } from 'lib/shopify/types';
|
2023-07-24 21:40:29 -05:00
|
|
|
import { useRouter, useSearchParams } from 'next/navigation';
|
2023-07-30 13:18:31 -05:00
|
|
|
import { useTransition } from 'react';
|
2023-04-17 23:00:47 -04:00
|
|
|
|
|
|
|
export function AddToCart({
|
|
|
|
variants,
|
|
|
|
availableForSale
|
|
|
|
}: {
|
|
|
|
variants: ProductVariant[];
|
|
|
|
availableForSale: boolean;
|
|
|
|
}) {
|
|
|
|
const router = useRouter();
|
|
|
|
const searchParams = useSearchParams();
|
|
|
|
const [isPending, startTransition] = useTransition();
|
2023-07-30 13:18:31 -05:00
|
|
|
const defaultVariantId = variants.length === 1 ? variants[0]?.id : undefined;
|
|
|
|
const variant = variants.find((variant: ProductVariant) =>
|
|
|
|
variant.selectedOptions.every(
|
|
|
|
(option) => option.value === searchParams.get(option.name.toLowerCase())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const selectedVariantId = variant?.id || defaultVariantId;
|
2023-07-28 17:00:48 -05:00
|
|
|
const title = !availableForSale
|
|
|
|
? 'Out of stock'
|
|
|
|
: !selectedVariantId
|
|
|
|
? 'Please select options'
|
|
|
|
: undefined;
|
|
|
|
|
2023-04-17 23:00:47 -04:00
|
|
|
return (
|
|
|
|
<button
|
|
|
|
aria-label="Add item to cart"
|
2023-07-28 17:00:48 -05:00
|
|
|
disabled={isPending || !availableForSale || !selectedVariantId}
|
|
|
|
title={title}
|
2023-06-17 13:18:00 -05:00
|
|
|
onClick={() => {
|
2023-08-02 09:43:38 -05:00
|
|
|
console.time('profile - add to cart - button');
|
2023-07-28 17:00:48 -05:00
|
|
|
// Safeguard in case someone messes with `disabled` in devtools.
|
|
|
|
if (!availableForSale || !selectedVariantId) return;
|
|
|
|
|
2023-06-17 13:18:00 -05:00
|
|
|
startTransition(async () => {
|
|
|
|
const error = await addItem(selectedVariantId);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
alert(error);
|
|
|
|
return;
|
|
|
|
}
|
2023-08-02 09:43:38 -05:00
|
|
|
console.timeEnd('profile - add to cart - button');
|
2023-06-17 13:18:00 -05:00
|
|
|
router.refresh();
|
|
|
|
});
|
|
|
|
}}
|
2023-04-25 09:38:47 -05:00
|
|
|
className={clsx(
|
2023-07-24 21:40:29 -05:00
|
|
|
'relative flex w-full items-center justify-center rounded-full bg-blue-600 p-4 tracking-wide text-white hover:opacity-90',
|
2023-04-25 09:38:47 -05:00
|
|
|
{
|
2023-07-28 17:00:48 -05:00
|
|
|
'cursor-not-allowed opacity-60 hover:opacity-60': !availableForSale || !selectedVariantId,
|
2023-06-17 13:18:00 -05:00
|
|
|
'cursor-not-allowed': isPending
|
2023-04-25 09:38:47 -05:00
|
|
|
}
|
|
|
|
)}
|
2023-04-17 23:00:47 -04:00
|
|
|
>
|
2023-07-24 21:40:29 -05:00
|
|
|
<div className="absolute left-0 ml-4">
|
|
|
|
{!isPending ? <PlusIcon className="h-5" /> : <LoadingDots className="mb-3 bg-white" />}
|
|
|
|
</div>
|
2023-04-17 23:00:47 -04:00
|
|
|
<span>{availableForSale ? 'Add To Cart' : 'Out Of Stock'}</span>
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|