|
1 | 1 | 'use client';
|
2 | 2 |
|
3 | 3 | import { XMarkIcon } from '@heroicons/react/24/outline';
|
4 |
| -import clsx from 'clsx'; |
5 | 4 | import { removeItem } from 'components/cart/actions';
|
6 |
| -import LoadingDots from 'components/loading-dots'; |
7 | 5 | import type { CartItem } from 'lib/shopify/types';
|
8 |
| -import { useFormState, useFormStatus } from 'react-dom'; |
| 6 | +import { useFormState } from 'react-dom'; |
9 | 7 |
|
10 |
| -function SubmitButton() { |
11 |
| - const { pending } = useFormStatus(); |
12 |
| - |
13 |
| - return ( |
14 |
| - <button |
15 |
| - type="submit" |
16 |
| - onClick={(e: React.FormEvent<HTMLButtonElement>) => { |
17 |
| - if (pending) e.preventDefault(); |
18 |
| - }} |
19 |
| - aria-label="Remove cart item" |
20 |
| - aria-disabled={pending} |
21 |
| - className={clsx( |
22 |
| - 'ease flex h-[17px] w-[17px] items-center justify-center rounded-full bg-neutral-500 transition-all duration-200', |
23 |
| - { |
24 |
| - 'cursor-not-allowed px-0': pending |
25 |
| - } |
26 |
| - )} |
27 |
| - > |
28 |
| - {pending ? ( |
29 |
| - <LoadingDots className="bg-white" /> |
30 |
| - ) : ( |
31 |
| - <XMarkIcon className="hover:text-accent-3 mx-[1px] h-4 w-4 text-white dark:text-black" /> |
32 |
| - )} |
33 |
| - </button> |
34 |
| - ); |
35 |
| -} |
36 |
| - |
37 |
| -export function DeleteItemButton({ item }: { item: CartItem }) { |
| 8 | +export function DeleteItemButton({ |
| 9 | + item, |
| 10 | + optimisticUpdate |
| 11 | +}: { |
| 12 | + item: CartItem; |
| 13 | + optimisticUpdate: any; |
| 14 | +}) { |
38 | 15 | const [message, formAction] = useFormState(removeItem, null);
|
39 | 16 | const itemId = item.id;
|
40 | 17 | const actionWithVariant = formAction.bind(null, itemId);
|
41 | 18 |
|
42 | 19 | return (
|
43 |
| - <form action={actionWithVariant}> |
44 |
| - <SubmitButton /> |
| 20 | + <form |
| 21 | + action={async () => { |
| 22 | + optimisticUpdate({ itemId, newQuantity: 0, type: 'minus' }); |
| 23 | + await actionWithVariant(); |
| 24 | + }} |
| 25 | + > |
| 26 | + <button |
| 27 | + type="submit" |
| 28 | + aria-label="Remove cart item" |
| 29 | + className="ease flex h-[17px] w-[17px] items-center justify-center rounded-full bg-neutral-500 transition-all duration-200" |
| 30 | + > |
| 31 | + <XMarkIcon className="hover:text-accent-3 mx-[1px] h-4 w-4 text-white dark:text-black" /> |
| 32 | + </button> |
45 | 33 | <p aria-live="polite" className="sr-only" role="status">
|
46 | 34 | {message}
|
47 | 35 | </p>
|
|
0 commit comments