-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(core): fetch checkout url until click (#912)
- Loading branch information
Showing
8 changed files
with
84 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@bigcommerce/catalyst-core": patch | ||
--- | ||
|
||
fetch checkout redirect url when user clicks proceed to checkout button |
37 changes: 37 additions & 0 deletions
37
core/app/[locale]/(default)/cart/_actions/redirect-to-checkout.ts
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use server'; | ||
|
||
import { redirect } from 'next/navigation'; | ||
import { z } from 'zod'; | ||
|
||
import { client } from '~/client'; | ||
import { graphql } from '~/client/graphql'; | ||
|
||
const CheckoutRedirectMutation = graphql(` | ||
mutation CheckoutRedirectMutation($cartId: String!) { | ||
cart { | ||
createCartRedirectUrls(input: { cartEntityId: $cartId }) { | ||
redirectUrls { | ||
redirectedCheckoutUrl | ||
} | ||
} | ||
} | ||
} | ||
`); | ||
|
||
export const redirectToCheckout = async (formData: FormData) => { | ||
const cartId = z.string().parse(formData.get('cartId')); | ||
|
||
const { data } = await client.fetch({ | ||
document: CheckoutRedirectMutation, | ||
variables: { cartId }, | ||
fetchOptions: { cache: 'no-store' }, | ||
}); | ||
|
||
const url = data.cart.createCartRedirectUrls.redirectUrls?.redirectedCheckoutUrl; | ||
|
||
if (!url) { | ||
throw new Error('Invalid checkout url.'); | ||
} | ||
|
||
redirect(url); | ||
}; |
51 changes: 28 additions & 23 deletions
51
core/app/[locale]/(default)/cart/_components/checkout-button.tsx
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,31 +1,36 @@ | ||
import { client } from '~/client'; | ||
import { graphql } from '~/client/graphql'; | ||
import { Button } from '~/components/ui/button'; | ||
'use client'; | ||
|
||
import { Loader2 as Spinner } from 'lucide-react'; | ||
import { useTranslations } from 'next-intl'; | ||
import { useFormStatus } from 'react-dom'; | ||
|
||
export const CheckoutButtonMutation = graphql(` | ||
mutation CheckoutButtonMutation($cartId: String!) { | ||
cart { | ||
createCartRedirectUrls(input: { cartEntityId: $cartId }) { | ||
redirectUrls { | ||
redirectedCheckoutUrl | ||
} | ||
} | ||
} | ||
} | ||
`); | ||
import { Button } from '~/components/ui/button'; | ||
|
||
export const CheckoutButton = async ({ cartId, label }: { cartId: string; label: string }) => { | ||
const { data } = await client.fetch({ | ||
document: CheckoutButtonMutation, | ||
variables: { cartId }, | ||
fetchOptions: { cache: 'no-store' }, | ||
}); | ||
import { redirectToCheckout } from '../_actions/redirect-to-checkout'; | ||
|
||
const checkoutUrl = data.cart.createCartRedirectUrls.redirectUrls?.redirectedCheckoutUrl; | ||
const InternalButton = () => { | ||
const t = useTranslations('Cart'); | ||
const { pending } = useFormStatus(); | ||
|
||
return ( | ||
<Button asChild className="mt-6"> | ||
<a href={checkoutUrl}>{label}</a> | ||
<Button className="mt-6" disabled={pending}> | ||
{pending ? ( | ||
<> | ||
<Spinner aria-hidden="true" className="animate-spin text-primary " /> | ||
<span className="sr-only">{t('loading')}</span> | ||
</> | ||
) : ( | ||
t('proceedToCheckout') | ||
)} | ||
</Button> | ||
); | ||
}; | ||
|
||
export const CheckoutButton = ({ cartId }: { cartId: string }) => { | ||
return ( | ||
<form action={redirectToCheckout}> | ||
<input name="cartId" type="hidden" value={cartId} /> | ||
<InternalButton /> | ||
</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
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
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