This commit is contained in:
cond0r
2021-02-04 16:03:52 +02:00
parent 67ed55d114
commit 32184ecdbd
6 changed files with 42 additions and 105 deletions

View File

@@ -1,53 +1,13 @@
import { useCallback } from 'react'
import type { HookFetcher } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors'
import useCommerceLogin from '@commerce/use-login'
import type { LoginBody } from '../api/customers/login'
import useCustomer from '../customer/use-customer'
const defaultOpts = {
query: '/api/bigcommerce/customers/login',
}
export type LoginInput = LoginBody
export const fetcher: HookFetcher<null, LoginBody> = (
options,
{ email, password },
fetch
) => {
if (!(email && password)) {
throw new CommerceError({
message:
'A first name, last name, email and password are required to login',
})
export function emptyHook() {
const useEmptyHook = async (options = {}) => {
return useCallback(async function () {
return Promise.resolve()
}, [])
}
return fetch({
...defaultOpts,
...options,
body: { email, password },
})
return useEmptyHook
}
export function extendHook(customFetcher: typeof fetcher) {
const useLogin = () => {
const { revalidate } = useCustomer()
const fn = useCommerceLogin<null, LoginInput>(defaultOpts, customFetcher)
return useCallback(
async function login(input: LoginInput) {
const data = await fn(input)
await revalidate()
return data
},
[fn]
)
}
useLogin.extend = extendHook
return useLogin
}
export default extendHook(fetcher)
export default emptyHook