Moved use-login and use-logout

This commit is contained in:
Luis Alvarez
2021-02-19 19:43:15 -05:00
parent aa227e3de2
commit 7cd64d2c2a
9 changed files with 95 additions and 81 deletions

View File

@@ -1,54 +1,40 @@
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 useCommerceLogin from '@commerce/use-login'
import useLogin, { UseLogin } from '@commerce/use-login'
import type { LoginBody } from '../api/customers/login'
import useCustomer from '../customer/use-customer'
const defaultOpts = {
url: '/api/bigcommerce/customers/login',
method: 'POST',
}
export default useLogin as UseLogin<typeof handler>
export type LoginInput = LoginBody
export const handler: MutationHook<null, {}, LoginBody> = {
fetchOptions: {
url: '/api/bigcommerce/customers/login',
method: 'POST',
},
async fetcher({ input: { email, password }, options, fetch }) {
if (!(email && password)) {
throw new CommerceError({
message:
'A first name, last name, email and password are required to login',
})
}
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',
return fetch({
...options,
body: { email, password },
})
}
return fetch({
...defaultOpts,
...options,
body: { email, password },
})
}
export function extendHook(customFetcher: typeof fetcher) {
const useLogin = () => {
},
useHook: ({ fetch }) => () => {
const { revalidate } = useCustomer()
const fn = useCommerceLogin<null, LoginInput>(defaultOpts, customFetcher)
return useCallback(
async function login(input: LoginInput) {
const data = await fn(input)
async function login(input) {
const data = await fetch({ input })
await revalidate()
return data
},
[fn]
[fetch, revalidate]
)
}
useLogin.extend = extendHook
return useLogin
},
}
export default extendHook(fetcher)

View File

@@ -1,38 +1,25 @@
import { useCallback } from 'react'
import type { HookFetcher } from '@commerce/utils/types'
import useCommerceLogout from '@commerce/use-logout'
import type { MutationHook } from '@commerce/utils/types'
import useLogout, { UseLogout } from '@commerce/use-logout'
import useCustomer from '../customer/use-customer'
const defaultOpts = {
url: '/api/bigcommerce/customers/logout',
method: 'GET',
}
export default useLogout as UseLogout<typeof handler>
export const fetcher: HookFetcher<null> = (options, _, fetch) => {
return fetch({
...defaultOpts,
...options,
})
}
export function extendHook(customFetcher: typeof fetcher) {
const useLogout = () => {
export const handler: MutationHook<null> = {
fetchOptions: {
url: '/api/bigcommerce/customers/logout',
method: 'GET',
},
useHook: ({ fetch }) => () => {
const { mutate } = useCustomer()
const fn = useCommerceLogout<null>(defaultOpts, customFetcher)
return useCallback(
async function login() {
const data = await fn(null)
async function logout() {
const data = await fetch()
await mutate(null, false)
return data
},
[fn]
[fetch, mutate]
)
}
useLogout.extend = extendHook
return useLogout
},
}
export default extendHook(fetcher)