forked from crowetic/commerce
Update use-signup
This commit is contained in:
parent
7cd64d2c2a
commit
683794bb33
@ -1,54 +1,44 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import type { HookFetcher } from '@commerce/utils/types'
|
import type { MutationHook } from '@commerce/utils/types'
|
||||||
import { CommerceError } from '@commerce/utils/errors'
|
import { CommerceError } from '@commerce/utils/errors'
|
||||||
import useCommerceSignup from '@commerce/use-signup'
|
import useSignup, { UseSignup } from '@commerce/use-signup'
|
||||||
import type { SignupBody } from '../api/customers/signup'
|
import type { SignupBody } from '../api/customers/signup'
|
||||||
import useCustomer from '../customer/use-customer'
|
import useCustomer from '../customer/use-customer'
|
||||||
|
|
||||||
const defaultOpts = {
|
export default useSignup as UseSignup<typeof handler>
|
||||||
url: '/api/bigcommerce/customers/signup',
|
|
||||||
method: 'POST',
|
|
||||||
}
|
|
||||||
|
|
||||||
export type SignupInput = SignupBody
|
export const handler: MutationHook<null, {}, SignupBody, SignupBody> = {
|
||||||
|
fetchOptions: {
|
||||||
|
url: '/api/bigcommerce/customers/signup',
|
||||||
|
method: 'POST',
|
||||||
|
},
|
||||||
|
async fetcher({
|
||||||
|
input: { firstName, lastName, email, password },
|
||||||
|
options,
|
||||||
|
fetch,
|
||||||
|
}) {
|
||||||
|
if (!(firstName && lastName && email && password)) {
|
||||||
|
throw new CommerceError({
|
||||||
|
message:
|
||||||
|
'A first name, last name, email and password are required to signup',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export const fetcher: HookFetcher<null, SignupBody> = (
|
return fetch({
|
||||||
options,
|
...options,
|
||||||
{ firstName, lastName, email, password },
|
body: { firstName, lastName, email, password },
|
||||||
fetch
|
|
||||||
) => {
|
|
||||||
if (!(firstName && lastName && email && password)) {
|
|
||||||
throw new CommerceError({
|
|
||||||
message:
|
|
||||||
'A first name, last name, email and password are required to signup',
|
|
||||||
})
|
})
|
||||||
}
|
},
|
||||||
|
useHook: ({ fetch }) => () => {
|
||||||
return fetch({
|
|
||||||
...defaultOpts,
|
|
||||||
...options,
|
|
||||||
body: { firstName, lastName, email, password },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
|
||||||
const useSignup = () => {
|
|
||||||
const { revalidate } = useCustomer()
|
const { revalidate } = useCustomer()
|
||||||
const fn = useCommerceSignup<null, SignupInput>(defaultOpts, customFetcher)
|
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
async function signup(input: SignupInput) {
|
async function signup(input) {
|
||||||
const data = await fn(input)
|
const data = await fetch({ input })
|
||||||
await revalidate()
|
await revalidate()
|
||||||
return data
|
return data
|
||||||
},
|
},
|
||||||
[fn]
|
[fetch, revalidate]
|
||||||
)
|
)
|
||||||
}
|
},
|
||||||
|
|
||||||
useSignup.extend = extendHook
|
|
||||||
|
|
||||||
return useSignup
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default extendHook(fetcher)
|
|
||||||
|
@ -11,6 +11,8 @@ import { handler as useCustomer } from './customer/use-customer'
|
|||||||
import { handler as useSearch } from './product/use-search'
|
import { handler as useSearch } from './product/use-search'
|
||||||
|
|
||||||
import { handler as useLogin } from './auth/use-login'
|
import { handler as useLogin } from './auth/use-login'
|
||||||
|
import { handler as useLogout } from './auth/use-logout'
|
||||||
|
import { handler as useSignup } from './auth/use-signup'
|
||||||
|
|
||||||
import fetcher from './fetcher'
|
import fetcher from './fetcher'
|
||||||
|
|
||||||
@ -26,7 +28,7 @@ export const bigcommerceProvider = {
|
|||||||
},
|
},
|
||||||
customer: { useCustomer },
|
customer: { useCustomer },
|
||||||
products: { useSearch },
|
products: { useSearch },
|
||||||
auth: { useLogin },
|
auth: { useLogin, useLogout, useSignup },
|
||||||
}
|
}
|
||||||
|
|
||||||
export type BigcommerceProvider = typeof bigcommerceProvider
|
export type BigcommerceProvider = typeof bigcommerceProvider
|
||||||
|
@ -1,5 +1,19 @@
|
|||||||
import useAction from './utils/use-action'
|
import { useHook, useMutationHook } from './utils/use-hook'
|
||||||
|
import { mutationFetcher } from './utils/default-fetcher'
|
||||||
|
import type { HookFetcherFn, MutationHook } from './utils/types'
|
||||||
|
import type { Provider } from '.'
|
||||||
|
|
||||||
const useSignup = useAction
|
export type UseSignup<
|
||||||
|
H extends MutationHook<any, any, any> = MutationHook<null>
|
||||||
|
> = ReturnType<H['useHook']>
|
||||||
|
|
||||||
|
export const fetcher: HookFetcherFn<null> = mutationFetcher
|
||||||
|
|
||||||
|
const fn = (provider: Provider) => provider.auth?.useSignup!
|
||||||
|
|
||||||
|
const useSignup: UseSignup = (...args) => {
|
||||||
|
const hook = useHook(fn)
|
||||||
|
return useMutationHook({ fetcher, ...hook })(...args)
|
||||||
|
}
|
||||||
|
|
||||||
export default useSignup
|
export default useSignup
|
||||||
|
Loading…
x
Reference in New Issue
Block a user