Skip to content

Commit

Permalink
chore: fetch checkout redirect url with gql
Browse files Browse the repository at this point in the history
  • Loading branch information
deini committed May 13, 2024
1 parent 13038f1 commit e7b4a59
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 51 deletions.
6 changes: 6 additions & 0 deletions .changeset/thin-scissors-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bigcommerce/catalyst-client": minor
"@bigcommerce/catalyst-core": patch
---

removes fetch cart redirect from client and fetch it with gql
23 changes: 21 additions & 2 deletions core/app/[locale]/(default)/cart/_components/checkout-button.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import { getCheckoutUrl } from '~/client/management/get-checkout-url';
import { client } from '~/client';
import { graphql } from '~/client/graphql';
import { Button } from '~/components/ui/button';

export const CheckoutButtonMutation = graphql(`
mutation CheckoutButtonMutation($cartId: String!) {
cart {
createCartRedirectUrls(input: { cartEntityId: $cartId }) {
redirectUrls {
redirectedCheckoutUrl
}
}
}
}
`);

export const CheckoutButton = async ({ cartId, label }: { cartId: string; label: string }) => {
const checkoutUrl = await getCheckoutUrl(cartId);
const { data } = await client.fetch({
document: CheckoutButtonMutation,
variables: { cartId },
fetchOptions: { cache: 'no-store' },
});

const checkoutUrl = data.cart.createCartRedirectUrls.redirectUrls?.redirectedCheckoutUrl;

return (
<Button asChild className="mt-6">
Expand Down
2 changes: 1 addition & 1 deletion core/app/[locale]/(default)/cart/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface Props {

const CartPageQuery = graphql(
`
query getCart($cartId: String) {
query CartPageQuery($cartId: String) {
site {
cart(entityId: $cartId) {
entityId
Expand Down
23 changes: 0 additions & 23 deletions core/client/management/get-checkout-url.ts

This file was deleted.

25 changes: 0 additions & 25 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,6 @@ class Client<FetcherRequestInit extends RequestInit = RequestInit> {
return response.json() as Promise<BigCommerceResponse<TResult>>;
}

async fetchCartRedirectUrls<TResult>(cartId: string) {
const response = await fetch(
`https://${adminApiHostname}/stores/${this.config.storeHash}/v3/carts/${cartId}/redirect_urls`,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Auth-Token': this.config.xAuthToken,
'User-Agent': this.backendUserAgent,
},
cache: 'no-store',
body: JSON.stringify({
cart_id: cartId,
}),
},
);

if (!response.ok) {
throw new Error(`Unable to get checkout URL: ${response.statusText}`);
}

return response.json() as Promise<BigCommerceResponse<TResult>>;
}

async fetchAvailableCountries() {
const response = await fetch(
`https://api.bigcommerce.com/stores/${this.config.storeHash}/v2/countries?limit=250`,
Expand Down

0 comments on commit e7b4a59

Please sign in to comment.