B f3cdbe682b
Bump (#642)
* Packages Bump

* Code Updated

* More API Changes

* Working updates

* Updated Tailwind Config

* SWR API updates

* More changes

* Commercejs Types

* Commercejs Types

* Commercejs Types
2022-01-13 15:30:14 +01:00

44 lines
1.2 KiB
TypeScript

import { MutationHook } from '@commerce/utils/types'
import useLogin, { UseLogin } from '@commerce/auth/use-login'
import { useCallback } from 'react'
import { CommerceError } from '@commerce/utils/errors'
import type { LoginHook } from '../types/login'
import useCustomer from '../customer/use-customer'
import useCart from '../cart/use-cart'
export default useLogin as UseLogin<typeof handler>
export const handler: MutationHook<LoginHook> = {
fetchOptions: {
url: '/api/login',
method: 'POST',
},
async fetcher({ input: { email, password }, options, fetch }) {
if (!(email && password)) {
throw new CommerceError({
message: 'An email and password are required to login',
})
}
return fetch({
...options,
body: { email, password },
})
},
useHook:
({ fetch }) =>
() => {
const { mutate } = useCustomer()
const { mutate: mutateCart } = useCart()
return useCallback(
async function login(input) {
const data = await fetch({ input })
await mutate()
await mutateCart()
return data
},
[fetch, mutate, mutateCart]
)
},
}