saleor: fix the checkout flow

This commit is contained in:
Zaiste 2021-05-14 14:53:53 +02:00
parent 3e72dfcb6e
commit df3d85f86e
No known key found for this signature in database
GPG Key ID: 15DF7EBC7F2FFE35
7 changed files with 31 additions and 26 deletions

View File

@ -1,20 +1,13 @@
import type { CommerceAPIConfig } from '@commerce/api' import type { CommerceAPIConfig } from '@commerce/api'
import { import * as Const from '../const'
API_URL,
API_CHANNEL,
} from '../const'
if (!API_URL) { if (!Const.API_URL) {
throw new Error( throw new Error(`The environment variable NEXT_SALEOR_API_URL is missing and it's required to access your store`)
`The environment variable NEXT_SALEOR_API_URL is missing and it's required to access your store`
)
} }
if (!API_CHANNEL) { if (!Const.API_CHANNEL) {
throw new Error( throw new Error(`The environment variable NEXT_SALEOR_CHANNEL is missing and it's required to access your store`)
`The environment variable NEXT_SALEOR_CHANNEL is missing and it's required to access your store`
)
} }
import fetchGraphqlApi from './utils/fetch-graphql-api' import fetchGraphqlApi from './utils/fetch-graphql-api'
@ -44,13 +37,13 @@ export class Config {
const config = new Config({ const config = new Config({
locale: 'en-US', locale: 'en-US',
commerceUrl: API_URL, commerceUrl: Const.API_URL,
apiToken: "saleor.Token", apiToken: Const.SALEOR_TOKEN,
cartCookie: "saleor.CheckoutID", cartCookie: Const.CHECKOUT_ID_COOKIE,
cartCookieMaxAge: 60 * 60 * 24 * 30, cartCookieMaxAge: 60 * 60 * 24 * 30,
fetch: fetchGraphqlApi, fetch: fetchGraphqlApi,
customerCookie: "", customerCookie: "",
storeChannel: API_CHANNEL, storeChannel: Const.API_CHANNEL,
}) })
export function getConfig(userConfig?: Partial<SaleorConfig>) { export function getConfig(userConfig?: Partial<SaleorConfig>) {

View File

@ -3,7 +3,7 @@ import type { MutationHook } from '@commerce/utils/types'
import useLogout, { UseLogout } from '@commerce/auth/use-logout' import useLogout, { UseLogout } from '@commerce/auth/use-logout'
import useCustomer from '../customer/use-customer' import useCustomer from '../customer/use-customer'
import * as mutation from '../utils/mutations' import * as mutation from '../utils/mutations'
import { setToken } from '../utils/customer-token' import { setCSRFToken, setToken, setCheckoutToken } from '../utils/customer-token'
export default useLogout as UseLogout<typeof handler> export default useLogout as UseLogout<typeof handler>
@ -16,7 +16,11 @@ export const handler: MutationHook<null> = {
...options, ...options,
variables: {}, variables: {},
}) })
setToken() setToken()
setCSRFToken()
setCheckoutToken()
return null return null
}, },
useHook: ({ fetch }) => () => { useHook: ({ fetch }) => () => {

View File

@ -24,11 +24,10 @@ export const handler: SWRHook<
let checkout let checkout
if (checkoutId) { if (checkoutId) {
const checkoutId = getCheckoutId().checkoutToken;
const data = await fetch({ const data = await fetch({
...options, ...options,
variables: { variables: { checkoutId },
checkoutId: getCheckoutId().checkoutToken,
},
}) })
checkout = data; checkout = data;

View File

@ -1,5 +1,7 @@
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 = 'saleor.CheckoutID' export const CHECKOUT_ID_COOKIE = 'saleor.CheckoutID'
export const SALEOR_TOKEN = 'saleor.Token'
export const SALEOR_CRSF_TOKEN = 'saleor.CSRFToken'

View File

@ -8,13 +8,14 @@ import {
} from '@commerce' } from '@commerce'
import { saleorProvider, SaleorProvider } from './provider' import { saleorProvider, SaleorProvider } from './provider'
import * as Const from './const';
export { saleorProvider } export { saleorProvider }
export type { SaleorProvider } export type { SaleorProvider }
export const saleorConfig: CommerceConfig = { export const saleorConfig: CommerceConfig = {
locale: 'en-us', locale: 'en-us',
cartCookie: "saleorCheckoutID", cartCookie: Const.CHECKOUT_ID_COOKIE
} }
export type SaleorConfig = Partial<CommerceConfig> export type SaleorConfig = Partial<CommerceConfig>

View File

@ -1,13 +1,19 @@
import Cookies, { CookieAttributes } from 'js-cookie' import Cookies, { CookieAttributes } from 'js-cookie'
import * as Const from '../const';
export const getToken = () => Cookies.get('saleor.Token') export const getToken = () => Cookies.get(Const.SALEOR_TOKEN)
export const setToken = (token?: string, options?: CookieAttributes) => { export const setToken = (token?: string, options?: CookieAttributes) => {
setCookie('saleor.Token', token, options) setCookie(Const.SALEOR_TOKEN, token, options)
} }
export const getCSRFToken = () => Cookies.get('saleor.CSRFToken') export const getCSRFToken = () => Cookies.get(Const.SALEOR_CRSF_TOKEN)
export const setCSRFToken = (token?: string, options?: CookieAttributes) => { export const setCSRFToken = (token?: string, options?: CookieAttributes) => {
setCookie('saleor.CSRFToken', token, options) setCookie(Const.SALEOR_CRSF_TOKEN, token, options)
}
export const getCheckoutToken = () => Cookies.get(Const.CHECKOUT_ID_COOKIE)
export const setCheckoutToken = (token?: string, options?: CookieAttributes) => {
setCookie(Const.CHECKOUT_ID_COOKIE, token, options)
} }
const setCookie = (name: string, token?: string, options?: CookieAttributes) => { const setCookie = (name: string, token?: string, options?: CookieAttributes) => {