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,
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:

View File

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

View File

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

View File

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

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'
const getCartId = (id?: string) => {
return id ?? Cookies.get(SHOPIFY_CART_ID_COOKIE)
return id || Cookies.get(SHOPIFY_CART_ID_COOKIE)
}
export default getCartId

View File

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