2023-10-10 21:45:55 -05:00
|
|
|
'use client';
|
2023-04-17 23:00:47 -04:00
|
|
|
|
2023-10-10 21:45:55 -05:00
|
|
|
import { XMarkIcon } from '@heroicons/react/24/outline';
|
2023-07-24 21:40:29 -05:00
|
|
|
import { removeItem } from 'components/cart/actions';
|
2023-04-17 23:00:47 -04:00
|
|
|
import type { CartItem } from 'lib/shopify/types';
|
2024-10-15 22:07:55 -05:00
|
|
|
import { useActionState } from 'react';
|
2023-04-17 23:00:47 -04:00
|
|
|
|
2024-07-25 17:23:22 -05:00
|
|
|
export function DeleteItemButton({
|
|
|
|
item,
|
|
|
|
optimisticUpdate
|
|
|
|
}: {
|
|
|
|
item: CartItem;
|
|
|
|
optimisticUpdate: any;
|
|
|
|
}) {
|
2024-10-15 22:07:55 -05:00
|
|
|
const [message, formAction] = useActionState(removeItem, null);
|
2024-07-28 22:58:59 -05:00
|
|
|
const merchandiseId = item.merchandise.id;
|
2025-02-09 11:38:22 -06:00
|
|
|
const removeItemAction = formAction.bind(null, merchandiseId);
|
2023-10-10 21:45:55 -05:00
|
|
|
|
|
|
|
return (
|
2024-07-25 17:23:22 -05:00
|
|
|
<form
|
|
|
|
action={async () => {
|
2024-07-28 22:58:59 -05:00
|
|
|
optimisticUpdate(merchandiseId, 'delete');
|
2025-02-09 11:38:22 -06:00
|
|
|
removeItemAction();
|
2024-07-25 17:23:22 -05:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
aria-label="Remove cart item"
|
2024-07-28 22:58:59 -05:00
|
|
|
className="flex h-[24px] w-[24px] items-center justify-center rounded-full bg-neutral-500"
|
2024-07-25 17:23:22 -05:00
|
|
|
>
|
2024-07-28 22:58:59 -05:00
|
|
|
<XMarkIcon className="mx-[1px] h-4 w-4 text-white dark:text-black" />
|
2024-07-25 17:23:22 -05:00
|
|
|
</button>
|
2023-10-10 21:45:55 -05:00
|
|
|
<p aria-live="polite" className="sr-only" role="status">
|
|
|
|
{message}
|
|
|
|
</p>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|