2022-01-21 11:09:34 +01:00

38 lines
916 B
TypeScript

import { SWRHook } from '@commerce/utils/types'
import { CustomerHook } from '@commerce/types/customer'
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
import Cookies from 'js-cookie'
import { ENDPOINT } from '../const'
export default useCustomer as UseCustomer<typeof handler>
const customerId = Cookies.get('CL_CUSTOMER_ID')
export const handler: SWRHook<CustomerHook> = {
fetchOptions: {
url: `${ENDPOINT}/api/customers/${customerId}`,
method: 'GET',
},
async fetcher({ input, options, fetch }) {
const data = await fetch({...options})
return data ? ({
firstName: '',
lastName: '',
email: data.attributes.email ?? '',
} as any)
: null
},
useHook:
({ useData }) =>
(input) => {
return useData({
swrOptions: {
revalidateOnFocus: false,
...input?.swrOptions,
},
})
},
}