mirror of
https://github.com/vercel/commerce.git
synced 2025-07-03 19:51:22 +00:00
Replace checkout reference with cart
This commit is contained in:
parent
c5e3d82202
commit
1d79007171
@ -9,6 +9,7 @@ import {
|
||||
normalizeCart,
|
||||
throwUserErrors,
|
||||
cartLineItemAddMutation,
|
||||
cartCreate,
|
||||
} from '../utils'
|
||||
import { CartLinesAddMutation, CartLinesAddMutationVariables } from '../schema'
|
||||
|
||||
@ -28,13 +29,20 @@ export const handler: MutationHook<AddItemHook> = {
|
||||
})
|
||||
}
|
||||
|
||||
let cartId = getCartId()
|
||||
|
||||
if (!cartId) {
|
||||
const { id } = await cartCreate(fetch)
|
||||
cartId = id
|
||||
}
|
||||
|
||||
const { cartLinesAdd } = await fetch<
|
||||
CartLinesAddMutation,
|
||||
CartLinesAddMutationVariables
|
||||
>({
|
||||
...options,
|
||||
variables: {
|
||||
checkoutId: getCartId(),
|
||||
cartId,
|
||||
lineItems: [
|
||||
{
|
||||
variantId: item.variantId,
|
||||
@ -46,10 +54,6 @@ export const handler: MutationHook<AddItemHook> = {
|
||||
|
||||
throwUserErrors(cartLinesAdd?.userErrors)
|
||||
|
||||
if (!cartLinesAdd?.cart) {
|
||||
throw new CommerceError({ message: 'Missing cart from response' })
|
||||
}
|
||||
|
||||
return normalizeCart(cartLinesAdd?.cart)
|
||||
},
|
||||
useHook:
|
||||
|
@ -54,8 +54,8 @@ export const handler = {
|
||||
>({
|
||||
...options,
|
||||
variables: {
|
||||
checkoutId: getCartId(),
|
||||
lineItems: [
|
||||
cartItems: getCartId(),
|
||||
lines: [
|
||||
{
|
||||
id: itemId,
|
||||
quantity: item.quantity,
|
||||
|
@ -9,6 +9,7 @@ const fetcher: Fetcher = async ({
|
||||
query,
|
||||
}) => {
|
||||
const { locale, ...vars } = variables ?? {}
|
||||
|
||||
return handleFetchResponse(
|
||||
await fetch(url, {
|
||||
method,
|
||||
|
@ -10,11 +10,11 @@ import {
|
||||
CartDetailsFragment,
|
||||
} from '../schema'
|
||||
import { FetcherOptions } from '@commerce/utils/types'
|
||||
import { FetcherError } from '@commerce/utils/errors'
|
||||
import { CommerceError } from '@commerce/utils/errors'
|
||||
|
||||
export const cartCreate = async (
|
||||
fetch: <T = any, B = Body>(options: FetcherOptions<B>) => Promise<T>
|
||||
): Promise<{ node: CartDetailsFragment }> => {
|
||||
): Promise<CartDetailsFragment> => {
|
||||
const { cartCreate } = await fetch<
|
||||
CartCreateMutation,
|
||||
CartCreateMutationVariables
|
||||
@ -24,23 +24,20 @@ export const cartCreate = async (
|
||||
|
||||
const cart = cartCreate?.cart
|
||||
|
||||
if (!cart) {
|
||||
throw new FetcherError({
|
||||
status: 500,
|
||||
if (cart?.id) {
|
||||
const options = {
|
||||
expires: SHOPIFY_COOKIE_EXPIRE,
|
||||
}
|
||||
Cookies.set(SHOPIFY_CART_ID_COOKIE, cart.id, options)
|
||||
} else {
|
||||
throw new CommerceError({
|
||||
errors: cartCreate?.userErrors?.map((e) => ({
|
||||
message: e.message,
|
||||
})) ?? [{ message: 'Could not create cart' }],
|
||||
})
|
||||
}
|
||||
|
||||
if (cart?.id) {
|
||||
const options = {
|
||||
expires: SHOPIFY_COOKIE_EXPIRE,
|
||||
}
|
||||
Cookies.set(SHOPIFY_CART_ID_COOKIE, cart.id, options)
|
||||
}
|
||||
|
||||
return { node: cart }
|
||||
return cart
|
||||
}
|
||||
|
||||
export default cartCreate
|
||||
|
@ -1,33 +0,0 @@
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
import {
|
||||
SHOPIFY_CHECKOUT_ID_COOKIE,
|
||||
SHOPIFY_CHECKOUT_URL_COOKIE,
|
||||
SHOPIFY_COOKIE_EXPIRE,
|
||||
} from '../const'
|
||||
|
||||
import checkoutCreateMutation from './mutations/checkout-create'
|
||||
import { CheckoutCreatePayload } from '../schema'
|
||||
|
||||
export const checkoutCreate = async (
|
||||
fetch: any
|
||||
): Promise<CheckoutCreatePayload> => {
|
||||
const data = await fetch({
|
||||
query: checkoutCreateMutation,
|
||||
})
|
||||
|
||||
const checkout = data.checkoutCreate?.checkout
|
||||
const checkoutId = checkout?.id
|
||||
|
||||
if (checkoutId) {
|
||||
const options = {
|
||||
expires: SHOPIFY_COOKIE_EXPIRE,
|
||||
}
|
||||
Cookies.set(SHOPIFY_CHECKOUT_ID_COOKIE, checkoutId, options)
|
||||
Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout.webUrl, options)
|
||||
}
|
||||
|
||||
return checkout
|
||||
}
|
||||
|
||||
export default checkoutCreate
|
@ -2,7 +2,7 @@ import Cookies from 'js-cookie'
|
||||
import { SHOPIFY_CART_ID_COOKIE } from '../const'
|
||||
|
||||
const getCartId = (id?: string) => {
|
||||
return id ?? Cookies.get(SHOPIFY_CART_ID_COOKIE)
|
||||
return id || Cookies.get(SHOPIFY_CART_ID_COOKIE)
|
||||
}
|
||||
|
||||
export default getCartId
|
||||
|
@ -4,7 +4,7 @@ const cartCreateMutation = /* GraphQL */ `
|
||||
mutation cartCreate {
|
||||
cartCreate {
|
||||
cart {
|
||||
...cartDetails
|
||||
id
|
||||
}
|
||||
userErrors {
|
||||
code
|
||||
|
Loading…
x
Reference in New Issue
Block a user