Replace checkout reference with cart

This commit is contained in:
cond0r 2021-08-05 22:04:37 +03:00
parent c5e3d82202
commit 1d79007171
7 changed files with 24 additions and 55 deletions

View File

@ -9,6 +9,7 @@ import {
normalizeCart, normalizeCart,
throwUserErrors, throwUserErrors,
cartLineItemAddMutation, cartLineItemAddMutation,
cartCreate,
} from '../utils' } from '../utils'
import { CartLinesAddMutation, CartLinesAddMutationVariables } from '../schema' 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< const { cartLinesAdd } = await fetch<
CartLinesAddMutation, CartLinesAddMutation,
CartLinesAddMutationVariables CartLinesAddMutationVariables
>({ >({
...options, ...options,
variables: { variables: {
checkoutId: getCartId(), cartId,
lineItems: [ lineItems: [
{ {
variantId: item.variantId, variantId: item.variantId,
@ -46,10 +54,6 @@ export const handler: MutationHook<AddItemHook> = {
throwUserErrors(cartLinesAdd?.userErrors) throwUserErrors(cartLinesAdd?.userErrors)
if (!cartLinesAdd?.cart) {
throw new CommerceError({ message: 'Missing cart from response' })
}
return normalizeCart(cartLinesAdd?.cart) return normalizeCart(cartLinesAdd?.cart)
}, },
useHook: useHook:

View File

@ -54,8 +54,8 @@ export const handler = {
>({ >({
...options, ...options,
variables: { variables: {
checkoutId: getCartId(), cartItems: getCartId(),
lineItems: [ lines: [
{ {
id: itemId, id: itemId,
quantity: item.quantity, quantity: item.quantity,

View File

@ -9,6 +9,7 @@ const fetcher: Fetcher = async ({
query, query,
}) => { }) => {
const { locale, ...vars } = variables ?? {} const { locale, ...vars } = variables ?? {}
return handleFetchResponse( return handleFetchResponse(
await fetch(url, { await fetch(url, {
method, method,

View File

@ -10,11 +10,11 @@ import {
CartDetailsFragment, CartDetailsFragment,
} from '../schema' } from '../schema'
import { FetcherOptions } from '@commerce/utils/types' import { FetcherOptions } from '@commerce/utils/types'
import { FetcherError } from '@commerce/utils/errors' import { CommerceError } from '@commerce/utils/errors'
export const cartCreate = async ( export const cartCreate = async (
fetch: <T = any, B = Body>(options: FetcherOptions<B>) => Promise<T> fetch: <T = any, B = Body>(options: FetcherOptions<B>) => Promise<T>
): Promise<{ node: CartDetailsFragment }> => { ): Promise<CartDetailsFragment> => {
const { cartCreate } = await fetch< const { cartCreate } = await fetch<
CartCreateMutation, CartCreateMutation,
CartCreateMutationVariables CartCreateMutationVariables
@ -24,23 +24,20 @@ export const cartCreate = async (
const cart = cartCreate?.cart const cart = cartCreate?.cart
if (!cart) { if (cart?.id) {
throw new FetcherError({ const options = {
status: 500, expires: SHOPIFY_COOKIE_EXPIRE,
}
Cookies.set(SHOPIFY_CART_ID_COOKIE, cart.id, options)
} else {
throw new CommerceError({
errors: cartCreate?.userErrors?.map((e) => ({ errors: cartCreate?.userErrors?.map((e) => ({
message: e.message, message: e.message,
})) ?? [{ message: 'Could not create cart' }], })) ?? [{ message: 'Could not create cart' }],
}) })
} }
if (cart?.id) { return cart
const options = {
expires: SHOPIFY_COOKIE_EXPIRE,
}
Cookies.set(SHOPIFY_CART_ID_COOKIE, cart.id, options)
}
return { node: cart }
} }
export default cartCreate export default cartCreate

View File

@ -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

View File

@ -2,7 +2,7 @@ import Cookies from 'js-cookie'
import { SHOPIFY_CART_ID_COOKIE } from '../const' import { SHOPIFY_CART_ID_COOKIE } from '../const'
const getCartId = (id?: string) => { const getCartId = (id?: string) => {
return id ?? Cookies.get(SHOPIFY_CART_ID_COOKIE) return id || Cookies.get(SHOPIFY_CART_ID_COOKIE)
} }
export default getCartId export default getCartId

View File

@ -4,7 +4,7 @@ const cartCreateMutation = /* GraphQL */ `
mutation cartCreate { mutation cartCreate {
cartCreate { cartCreate {
cart { cart {
...cartDetails id
} }
userErrors { userErrors {
code code