4
0
forked from crowetic/commerce
commerce/components/cart/delete-item-button.tsx
Michael Novotny 0dad8ba839
More profiles
2023-08-02 09:43:38 -05:00

46 lines
1.3 KiB
TypeScript

import { XMarkIcon } from '@heroicons/react/24/outline';
import LoadingDots from 'components/loading-dots';
import { useRouter } from 'next/navigation';
import clsx from 'clsx';
import { removeItem } from 'components/cart/actions';
import type { CartItem } from 'lib/shopify/types';
import { useTransition } from 'react';
export default function DeleteItemButton({ item }: { item: CartItem }) {
const router = useRouter();
const [isPending, startTransition] = useTransition();
return (
<button
aria-label="Remove cart item"
onClick={() => {
console.time('profile - remove from cart - button');
startTransition(async () => {
const error = await removeItem(item.id);
if (error) {
alert(error);
return;
}
console.timeEnd('profile - remove from cart - button');
router.refresh();
});
}}
disabled={isPending}
className={clsx(
'ease flex h-[17px] w-[17px] items-center justify-center rounded-full bg-neutral-500 transition-all duration-200',
{
'cursor-not-allowed px-0': isPending
}
)}
>
{isPending ? (
<LoadingDots className="bg-white" />
) : (
<XMarkIcon className="hover:text-accent-3 mx-[1px] h-4 w-4 text-white dark:text-black" />
)}
</button>
);
}