-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improves form submissions and updates dependencies (#1209)
- Loading branch information
Showing
13 changed files
with
851 additions
and
915 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,54 @@ | ||
import { XMarkIcon } from '@heroicons/react/24/outline'; | ||
import LoadingDots from 'components/loading-dots'; | ||
import { useRouter } from 'next/navigation'; | ||
'use client'; | ||
|
||
import { XMarkIcon } from '@heroicons/react/24/outline'; | ||
import clsx from 'clsx'; | ||
import { removeItem } from 'components/cart/actions'; | ||
import LoadingDots from 'components/loading-dots'; | ||
import type { CartItem } from 'lib/shopify/types'; | ||
import { useTransition } from 'react'; | ||
import { | ||
// @ts-ignore | ||
experimental_useFormState as useFormState, | ||
experimental_useFormStatus as useFormStatus | ||
} from 'react-dom'; | ||
|
||
export default function DeleteItemButton({ item }: { item: CartItem }) { | ||
const router = useRouter(); | ||
const [isPending, startTransition] = useTransition(); | ||
function SubmitButton() { | ||
const { pending } = useFormStatus(); | ||
|
||
return ( | ||
<button | ||
aria-label="Remove cart item" | ||
onClick={() => { | ||
startTransition(async () => { | ||
const error = await removeItem(item.id); | ||
|
||
if (error) { | ||
// Trigger the error boundary in the root error.js | ||
throw new Error(error.toString()); | ||
} | ||
|
||
router.refresh(); | ||
}); | ||
type="submit" | ||
onClick={(e: React.FormEvent<HTMLButtonElement>) => { | ||
if (pending) e.preventDefault(); | ||
}} | ||
disabled={isPending} | ||
aria-label="Remove cart item" | ||
aria-disabled={pending} | ||
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 | ||
'cursor-not-allowed px-0': pending | ||
} | ||
)} | ||
> | ||
{isPending ? ( | ||
{pending ? ( | ||
<LoadingDots className="bg-white" /> | ||
) : ( | ||
<XMarkIcon className="hover:text-accent-3 mx-[1px] h-4 w-4 text-white dark:text-black" /> | ||
)} | ||
</button> | ||
); | ||
} | ||
|
||
export function DeleteItemButton({ item }: { item: CartItem }) { | ||
const [message, formAction] = useFormState(removeItem, null); | ||
const itemId = item.id; | ||
const actionWithVariant = formAction.bind(null, itemId); | ||
|
||
return ( | ||
<form action={actionWithVariant}> | ||
<SubmitButton /> | ||
<p aria-live="polite" className="sr-only" role="status"> | ||
{message} | ||
</p> | ||
</form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
1f47796
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
commerce-shopify – ./
www.vercel.store
commerce-2-omega.vercel.app
demo.vercel.store
site-git-commerce.vercel.app
shopify.demo.vercel.shop
shopify.demo.vercel.store
team-a.vercel.store
vercel.store
shopify.vercel.shop
shopify.vercel.store
commerce-shopify-vercel-solutions-vtest314.vercel.app
commerce-shopify-git-main-vercel-solutions-vtest314.vercel.app