Move useCustomer to the new hook

This commit is contained in:
Luis Alvarez
2021-02-11 13:26:09 -05:00
parent 8966f0b583
commit d8de84bed1
8 changed files with 102 additions and 49 deletions

View File

@@ -1,38 +1,4 @@
import type { HookFetcher } from '@commerce/utils/types'
import type { SwrOptions } from '@commerce/utils/use-data'
import useCommerceCustomer from '@commerce/use-customer'
import type { Customer, CustomerData } from '../api/customers'
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
import type { BigcommerceProvider } from '..'
const defaultOpts = {
url: '/api/bigcommerce/customers',
method: 'GET',
}
export type { Customer }
export const fetcher: HookFetcher<Customer | null> = async (
options,
_,
fetch
) => {
const data = await fetch<CustomerData | null>({ ...defaultOpts, ...options })
return data?.customer ?? null
}
export function extendHook(
customFetcher: typeof fetcher,
swrOptions?: SwrOptions<Customer | null>
) {
const useCustomer = () => {
return useCommerceCustomer(defaultOpts, [], customFetcher, {
revalidateOnFocus: false,
...swrOptions,
})
}
useCustomer.extend = extendHook
return useCustomer
}
export default extendHook(fetcher)
export default useCustomer as UseCustomer<BigcommerceProvider>