forked from crowetic/commerce
use-signup
This commit is contained in:
parent
ef81a968af
commit
e2b26715b9
52
lib/bigcommerce/use-signup.tsx
Normal file
52
lib/bigcommerce/use-signup.tsx
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import { useCallback } from 'react'
|
||||||
|
import { HookFetcher } from '@lib/commerce/utils/types'
|
||||||
|
import useCommerceSignup from '@lib/commerce/use-signup'
|
||||||
|
import type { CreateCustomerBody } from './api/customers'
|
||||||
|
|
||||||
|
const defaultOpts = {
|
||||||
|
url: '/api/bigcommerce/customers',
|
||||||
|
method: 'POST',
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SignupInput = CreateCustomerBody
|
||||||
|
|
||||||
|
export const fetcher: HookFetcher<undefined, CreateCustomerBody> = (
|
||||||
|
options,
|
||||||
|
{ firstName, lastName, email, password },
|
||||||
|
fetch
|
||||||
|
) => {
|
||||||
|
if (!(firstName && lastName && email && password)) {
|
||||||
|
throw new Error(
|
||||||
|
'A first name, last name, email and password are required to signup'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fetch({
|
||||||
|
url: options?.url ?? defaultOpts.url,
|
||||||
|
method: options?.method ?? defaultOpts.method,
|
||||||
|
body: { firstName, lastName, email, password },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export function extendHook(customFetcher: typeof fetcher) {
|
||||||
|
const useSignup = () => {
|
||||||
|
const fn = useCommerceSignup<undefined, SignupInput>(
|
||||||
|
defaultOpts,
|
||||||
|
customFetcher
|
||||||
|
)
|
||||||
|
|
||||||
|
return useCallback(
|
||||||
|
async function signup(input: SignupInput) {
|
||||||
|
const data = await fn(input)
|
||||||
|
return data
|
||||||
|
},
|
||||||
|
[fn]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
useSignup.extend = extendHook
|
||||||
|
|
||||||
|
return useSignup
|
||||||
|
}
|
||||||
|
|
||||||
|
export default extendHook(fetcher)
|
5
lib/commerce/use-signup.tsx
Normal file
5
lib/commerce/use-signup.tsx
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import useAction from './utils/use-action'
|
||||||
|
|
||||||
|
const useSignup = useAction
|
||||||
|
|
||||||
|
export default useSignup
|
@ -9,9 +9,7 @@ export default function useAction<T, Input>(
|
|||||||
const { fetcherRef } = useCommerce()
|
const { fetcherRef } = useCommerce()
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
function addItem(input: Input) {
|
(input: Input) => fetcher(options, input, fetcherRef.current),
|
||||||
return fetcher(options, input, fetcherRef.current)
|
|
||||||
},
|
|
||||||
[fetcher]
|
[fetcher]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user