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';
|
2024-07-28 22:58:59 -05:00
|
|
|
import { useProduct } from 'components/product/product-context';
|
|
|
|
import { Product, ProductVariant } from 'lib/shopify/types';
|
2024-10-15 22:07:55 -05:00
|
|
|
import { useActionState } from 'react';
|
2024-07-28 22:58:59 -05:00
|
|
|
import { useCart } from './cart-context';
|
2023-10-10 21:45:55 -05:00
|
|
|
|
|
|
|
function SubmitButton({
|
|
|
|
availableForSale,
|
|
|
|
selectedVariantId
|
|
|
|
}: {
|
|
|
|
availableForSale: boolean;
|
|
|
|
selectedVariantId: string | undefined;
|
|
|
|
}) {
|
|
|
|
const buttonClasses =
|
|
|
|
'relative flex w-full items-center justify-center rounded-full bg-blue-600 p-4 tracking-wide text-white';
|
|
|
|
const disabledClasses = 'cursor-not-allowed opacity-60 hover:opacity-60';
|
|
|
|
|
|
|
|
if (!availableForSale) {
|
|
|
|
return (
|
2024-07-29 14:22:06 -05:00
|
|
|
<button disabled className={clsx(buttonClasses, disabledClasses)}>
|
2023-10-10 21:45:55 -05:00
|
|
|
Out Of Stock
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-29 14:22:06 -05:00
|
|
|
console.log(selectedVariantId);
|
2023-10-10 21:45:55 -05:00
|
|
|
if (!selectedVariantId) {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
aria-label="Please select an option"
|
2024-07-29 14:22:06 -05:00
|
|
|
disabled
|
2023-10-10 21:45:55 -05:00
|
|
|
className={clsx(buttonClasses, disabledClasses)}
|
|
|
|
>
|
|
|
|
<div className="absolute left-0 ml-4">
|
|
|
|
<PlusIcon className="h-5" />
|
|
|
|
</div>
|
|
|
|
Add To Cart
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
aria-label="Add to cart"
|
|
|
|
className={clsx(buttonClasses, {
|
2024-07-28 22:58:59 -05:00
|
|
|
'hover:opacity-90': true
|
2023-10-10 21:45:55 -05:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
<div className="absolute left-0 ml-4">
|
2024-07-28 22:58:59 -05:00
|
|
|
<PlusIcon className="h-5" />
|
2023-10-10 21:45:55 -05:00
|
|
|
</div>
|
|
|
|
Add To Cart
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
2023-04-17 23:00:47 -04:00
|
|
|
|
2024-07-28 22:58:59 -05:00
|
|
|
export function AddToCart({ product }: { product: Product }) {
|
|
|
|
const { variants, availableForSale } = product;
|
|
|
|
const { addCartItem } = useCart();
|
|
|
|
const { state } = useProduct();
|
2024-10-15 22:07:55 -05:00
|
|
|
const [message, formAction] = useActionState(addItem, null);
|
2024-07-28 22:58:59 -05:00
|
|
|
|
2023-07-30 13:18:31 -05:00
|
|
|
const variant = variants.find((variant: ProductVariant) =>
|
2024-07-28 22:58:59 -05:00
|
|
|
variant.selectedOptions.every((option) => option.value === state[option.name.toLowerCase()])
|
2023-07-30 13:18:31 -05:00
|
|
|
);
|
2024-07-28 22:58:59 -05:00
|
|
|
const defaultVariantId = variants.length === 1 ? variants[0]?.id : undefined;
|
2023-07-30 13:18:31 -05:00
|
|
|
const selectedVariantId = variant?.id || defaultVariantId;
|
2023-10-10 21:45:55 -05:00
|
|
|
const actionWithVariant = formAction.bind(null, selectedVariantId);
|
2024-07-28 22:58:59 -05:00
|
|
|
const finalVariant = variants.find((variant) => variant.id === selectedVariantId)!;
|
2023-07-28 17:00:48 -05:00
|
|
|
|
2023-04-17 23:00:47 -04:00
|
|
|
return (
|
2024-07-28 22:58:59 -05:00
|
|
|
<form
|
|
|
|
action={async () => {
|
|
|
|
addCartItem(finalVariant, product);
|
|
|
|
await actionWithVariant();
|
|
|
|
}}
|
|
|
|
>
|
2023-10-10 21:45:55 -05:00
|
|
|
<SubmitButton availableForSale={availableForSale} selectedVariantId={selectedVariantId} />
|
|
|
|
<p aria-live="polite" className="sr-only" role="status">
|
|
|
|
{message}
|
|
|
|
</p>
|
|
|
|
</form>
|
2023-04-17 23:00:47 -04:00
|
|
|
);
|
|
|
|
}
|