Login, Sign Up, Log Out, and checkout & customer association

This commit is contained in:
cond0r
2021-02-05 10:31:04 +02:00
parent 612392aaba
commit dde09c5105
21 changed files with 276 additions and 43 deletions

View File

@@ -1,23 +1,29 @@
import type { HookFetcher } from '@commerce/utils/types'
import type { SwrOptions } from '@commerce/utils/use-data'
import useCommerceCustomer from '@commerce/use-customer'
import getCustomerQuery from '@framework/utils/queries/get-customer-query'
import { getCustomerToken } from '@framework/utils/customer-token'
const defaultOpts = {
query: '/api/bigcommerce/customers',
query: getCustomerQuery,
}
export const fetcher: HookFetcher<Customer | null> = async (
options,
_,
fetch
) => {
const data = await fetch<CustomerData | null>({ ...defaultOpts, ...options })
return data?.customer ?? null
export const fetcher: HookFetcher<any | null> = async (options, _, fetch) => {
const customerAccessToken = getCustomerToken()
if (customerAccessToken) {
const data = await fetch<any | null>({
...defaultOpts,
...options,
variables: { customerAccessToken },
})
return data?.customer ?? null
}
return null
}
export function extendHook(
customFetcher: typeof fetcher,
swrOptions?: SwrOptions<Customer | null>
swrOptions?: SwrOptions<any | null>
) {
const useCustomer = () => {
return useCommerceCustomer(defaultOpts, [], customFetcher, {