forked from crowetic/commerce
* Packages Bump * Code Updated * More API Changes * Working updates * Updated Tailwind Config * SWR API updates * More changes * Commercejs Types * Commercejs Types * Commercejs Types
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { useCallback } from 'react'
|
|
import type { MutationHook } from '@commerce/utils/types'
|
|
import { CommerceError } from '@commerce/utils/errors'
|
|
import useSignup, { UseSignup } from '@commerce/auth/use-signup'
|
|
import type { SignupHook } from '../types/signup'
|
|
import useCustomer from '../customer/use-customer'
|
|
|
|
export default useSignup as UseSignup<typeof handler>
|
|
|
|
export const handler: MutationHook<SignupHook> = {
|
|
fetchOptions: {
|
|
url: '/api/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',
|
|
})
|
|
}
|
|
|
|
return fetch({
|
|
...options,
|
|
body: { firstName, lastName, email, password },
|
|
})
|
|
},
|
|
useHook:
|
|
({ fetch }) =>
|
|
() => {
|
|
const { mutate } = useCustomer()
|
|
|
|
return useCallback(
|
|
async function signup(input) {
|
|
const data = await fetch({ input })
|
|
await mutate()
|
|
return data
|
|
},
|
|
[fetch, mutate]
|
|
)
|
|
},
|
|
}
|