saleor: initial cart integration

This commit is contained in:
Zaiste 2021-04-26 16:24:50 +02:00
parent ef10084e54
commit 19445747f1
No known key found for this signature in database
GPG Key ID: 15DF7EBC7F2FFE35
9 changed files with 41 additions and 76 deletions

View File

@ -9,7 +9,7 @@ import {
checkoutToCart, checkoutToCart,
} from '../utils' } from '../utils'
import { Cart, CartItemBody } from '../types' import { Cart, CartItemBody } from '../types'
import { Mutation, MutationCheckoutLineItemsAddArgs } from '../schema' import { Mutation, MutationCheckoutLinesAddArgs } from '../schema'
export default useAddItem as UseAddItem<typeof handler> export default useAddItem as UseAddItem<typeof handler>
@ -27,13 +27,13 @@ export const handler: MutationHook<Cart, {}, CartItemBody> = {
}) })
} }
const { checkoutLineItemsAdd } = await fetch< const { checkoutLinesAdd } = await fetch<
Mutation, Mutation,
MutationCheckoutLineItemsAddArgs MutationCheckoutLinesAddArgs
>({ >({
...options, ...options,
variables: { variables: {
checkoutId: getCheckoutId(), checkoutId: getCheckoutId().checkoutId,
lineItems: [ lineItems: [
{ {
variantId: item.variantId, variantId: item.variantId,
@ -43,7 +43,7 @@ export const handler: MutationHook<Cart, {}, CartItemBody> = {
}, },
}) })
return checkoutToCart(checkoutLineItemsAdd) return checkoutToCart(checkoutLinesAdd)
}, },
useHook: ({ fetch }) => () => { useHook: ({ fetch }) => () => {
const { mutate } = useCart() const { mutate } = useCart()

View File

@ -6,7 +6,7 @@ import useCommerceCart, {
import { Cart } from '../types' import { Cart } from '../types'
import { SWRHook } from '@commerce/utils/types' import { SWRHook } from '@commerce/utils/types'
import { checkoutCreate, checkoutToCart } from '../utils' import { checkoutCreate, checkoutToCart, getCheckoutId } from '../utils'
import getCheckoutQuery from '../utils/queries/get-checkout-query' import getCheckoutQuery from '../utils/queries/get-checkout-query'
export default useCommerceCart as UseCart<typeof handler> export default useCommerceCart as UseCart<typeof handler>
@ -27,7 +27,7 @@ export const handler: SWRHook<
const data = await fetch({ const data = await fetch({
...options, ...options,
variables: { variables: {
checkoutId: checkoutId, checkoutId: getCheckoutId().checkoutToken,
}, },
}) })
checkout = data.node checkout = data.node

View File

@ -1,2 +1,5 @@
export const API_URL = process.env.NEXT_PUBLIC_SALEOR_API_URL export const API_URL = process.env.NEXT_PUBLIC_SALEOR_API_URL
export const API_CHANNEL = process.env.NEXT_PUBLIC_SALEOR_CHANNEL export const API_CHANNEL = process.env.NEXT_PUBLIC_SALEOR_CHANNEL
export const CHECKOUT_ID_COOKIE = 'saleorCheckoutID'

View File

@ -15,6 +15,7 @@ import fetcher from './fetcher'
export const saleorProvider = { export const saleorProvider = {
locale: 'en-us', locale: 'en-us',
cartCookie: "", cartCookie: "",
cartCookieToken: "",
fetcher, fetcher,
cart: { useCart, useAddItem, useUpdateItem, useRemoveItem }, cart: { useCart, useAddItem, useUpdateItem, useRemoveItem },
customer: { useCustomer }, customer: { useCustomer },

View File

@ -1,23 +1,27 @@
import Cookies from 'js-cookie' import Cookies from 'js-cookie'
import checkoutCreateMutation from './mutations/checkout-create' import checkoutCreateMutation from './mutations/checkout-create'
import { CheckoutCreatePayload } from '../schema' import { CheckoutCreate } from '../schema'
import { CHECKOUT_ID_COOKIE } from '@framework/const'
export const checkoutCreate = async ( export const checkoutCreate = async (
fetch: any fetch: any
): Promise<CheckoutCreatePayload> => { ): Promise<CheckoutCreate> => {
const data = await fetch({ const data = await fetch({
query: checkoutCreateMutation, query: checkoutCreateMutation,
}) })
const checkout = data.checkoutCreate?.checkout const checkout = data.checkoutCreate?.checkout
const checkoutId = checkout?.id const checkoutId = checkout?.id
const checkoutToken = checkout?.token
const value = `${checkoutId}:${checkoutToken}`;
if (checkoutId) { if (checkoutId) {
const options = { const options = {
expires: 60 * 60 * 24 * 30, expires: 60 * 60 * 24 * 30,
} }
Cookies.set('saleorCheckoutID', checkoutId, options) Cookies.set(CHECKOUT_ID_COOKIE, value, options)
} }
return checkout return checkout

View File

@ -1,5 +1,9 @@
import Cookies from 'js-cookie'
import { CHECKOUT_ID_COOKIE } from '../const'
const getCheckoutId = (id?: string) => { const getCheckoutId = (id?: string) => {
return id const r = Cookies.get(CHECKOUT_ID_COOKIE)?.split(":") || [];
return { checkoutId: r[0], checkoutToken: r[1] }
} }
export default getCheckoutId export default getCheckoutId

View File

@ -2,16 +2,19 @@ import { checkoutDetailsFragment } from '../queries/get-checkout-query'
const checkoutCreateMutation = /* GraphQL */ ` const checkoutCreateMutation = /* GraphQL */ `
mutation createCheckout { mutation createCheckout {
checkoutCreate(input: {}) { checkoutCreate(input: {
checkoutUserErrors { email: "customer@example.com",
lines: [{quantity: 1, variantId: "UHJvZHVjdFZhcmlhbnQ6Mjk3"}],
channel: "default-channel"
}) {
errors {
code code
field field
message message
} }
# Breaks GraphQL Codegen checkout {
# checkout { ${checkoutDetailsFragment}
# ${checkoutDetailsFragment} }
# }
} }
} }
` `

View File

@ -1,16 +1,16 @@
import { checkoutDetailsFragment } from '../queries/get-checkout-query' import { checkoutDetailsFragment } from '../queries/get-checkout-query'
const checkoutLineItemAddMutation = /* GraphQL */ ` const checkoutLineItemAddMutation = /* GraphQL */ `
mutation checkoutLineItemAdd($checkoutId: ID!, $lineItems: [CheckoutLineItemInput!]!) { mutation checkoutLineItemAdd($checkoutId: ID!, $lineItems: [CheckoutLineInput!]!) {
checkoutLineItemsAdd(checkoutId: $checkoutId, lineItems: $lineItems) { checkoutLinesAdd(checkoutId: $checkoutId, lines: $lineItems) {
checkoutUserErrors { errors {
code code
field field
message message
} }
# checkout { checkout {
# ${checkoutDetailsFragment} ${checkoutDetailsFragment}
# } }
} }
} }
` `

View File

@ -1,61 +1,11 @@
export const checkoutDetailsFragment = ` export const checkoutDetailsFragment = `
id id
webUrl token
subtotalPriceV2{
amount
currencyCode
}
totalTaxV2 {
amount
currencyCode
}
totalPriceV2 {
amount
currencyCode
}
completedAt
createdAt
taxesIncluded
lineItems(first: 250) {
pageInfo {
hasNextPage
hasPreviousPage
}
edges {
node {
id
title
variant {
id
sku
title
image {
originalSrc
altText
width
height
}
priceV2{
amount
currencyCode
}
compareAtPriceV2{
amount
currencyCode
}
product {
handle
}
}
quantity
}
}
}
` `
const getCheckoutQuery = /* GraphQL */ ` const getCheckoutQuery = /* GraphQL */ `
query($checkoutId: ID!) { query($checkoutId: UUID!) {
node(id: $checkoutId) { checkout(token: $checkoutId) {
... on Checkout { ... on Checkout {
${checkoutDetailsFragment} ${checkoutDetailsFragment}
} }