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,13 +1,45 @@
import { useCallback } from 'react'
import type { HookFetcher } from '@commerce/utils/types'
import useCommerceLogout from '@commerce/use-logout'
import useCustomer from '../customer/use-customer'
import customerAccessTokenDeleteMutation from '@framework/utils/mutations/customer-access-token-delete'
import {
getCustomerToken,
setCustomerToken,
} from '@framework/utils/customer-token'
export function emptyHook() {
const useEmptyHook = async (options = {}) => {
return useCallback(async function () {
return Promise.resolve()
}, [])
}
return useEmptyHook
const defaultOpts = {
query: customerAccessTokenDeleteMutation,
}
export default emptyHook
export const fetcher: HookFetcher<null> = (options, _, fetch) => {
return fetch({
...defaultOpts,
...options,
variables: {
customerAccessToken: getCustomerToken(),
},
}).then((d) => setCustomerToken(null))
}
export function extendHook(customFetcher: typeof fetcher) {
const useLogout = () => {
const { mutate } = useCustomer()
const fn = useCommerceLogout<null>(defaultOpts, customFetcher)
return useCallback(
async function login() {
const data = await fn(null)
await mutate(null, false)
return data
},
[fn]
)
}
useLogout.extend = extendHook
return useLogout
}
export default extendHook(fetcher)