From d98897a7e353db5e349dffefd0a064f08f62e485 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Wed, 21 Oct 2020 10:23:05 -0500 Subject: [PATCH] Added useCustomer hook --- lib/bigcommerce/cart/use-cart.tsx | 7 +----- lib/bigcommerce/use-customer.tsx | 40 +++++++++++++++++++++++++++++++ lib/commerce/use-customer.tsx | 30 +++++++++++++++++++++++ lib/commerce/utils/use-data.tsx | 1 + 4 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 lib/bigcommerce/use-customer.tsx create mode 100644 lib/commerce/use-customer.tsx diff --git a/lib/bigcommerce/cart/use-cart.tsx b/lib/bigcommerce/cart/use-cart.tsx index 8fe80747a..38d1d8bac 100644 --- a/lib/bigcommerce/cart/use-cart.tsx +++ b/lib/bigcommerce/cart/use-cart.tsx @@ -14,12 +14,7 @@ export const fetcher: HookFetcher = ( { cartId }, fetch ) => { - return cartId - ? fetch({ - url: options?.url, - query: options?.query, - }) - : null + return cartId ? fetch({ ...options }) : null } export function extendHook( diff --git a/lib/bigcommerce/use-customer.tsx b/lib/bigcommerce/use-customer.tsx new file mode 100644 index 000000000..273e4c871 --- /dev/null +++ b/lib/bigcommerce/use-customer.tsx @@ -0,0 +1,40 @@ +import { ConfigInterface } from 'swr' +import { HookFetcher } from '@lib/commerce/utils/types' +import useCommerceCustomer, { CustomerInput } from '@lib/commerce/use-customer' +import type { Customer, CustomerData } from './api/customers' + +const defaultOpts = { + url: '/api/bigcommerce/customer', +} + +export type { Customer } + +export const fetcher: HookFetcher = ( + options, + { cartId }, + fetch +) => { + return cartId ? fetch({ ...options }) : null +} + +export function extendHook( + customFetcher: typeof fetcher, + swrOptions?: ConfigInterface +) { + const useCustomer = () => { + const cart = useCommerceCustomer( + defaultOpts, + [], + customFetcher, + { revalidateOnFocus: false, ...swrOptions } + ) + + return cart + } + + useCustomer.extend = extendHook + + return useCustomer +} + +export default extendHook(fetcher) diff --git a/lib/commerce/use-customer.tsx b/lib/commerce/use-customer.tsx new file mode 100644 index 000000000..e33587dcf --- /dev/null +++ b/lib/commerce/use-customer.tsx @@ -0,0 +1,30 @@ +import { responseInterface, ConfigInterface } from 'swr' +import Cookies from 'js-cookie' +import type { HookInput, HookFetcher, HookFetcherOptions } from './utils/types' +import useData from './utils/use-data' +import { useCommerce } from '.' + +export type CustomerResponse = responseInterface + +export type CustomerInput = { + cartId: string | undefined +} + +export default function useCustomer( + options: HookFetcherOptions, + input: HookInput, + fetcherFn: HookFetcher, + swrOptions?: ConfigInterface +) { + // TODO: Replace this with the login cookie + const { cartCookie } = useCommerce() + + const fetcher: typeof fetcherFn = (options, input, fetch) => { + input.cartId = Cookies.get(cartCookie) + return fetcherFn(options, input, fetch) + } + + const response = useData(options, input, fetcher, swrOptions) + + return response as CustomerResponse +} diff --git a/lib/commerce/utils/use-data.tsx b/lib/commerce/utils/use-data.tsx index fa2b6175c..513bec7bc 100644 --- a/lib/commerce/utils/use-data.tsx +++ b/lib/commerce/utils/use-data.tsx @@ -12,6 +12,7 @@ export default function useData( const fetcher = (url?: string, query?: string, ...args: any[]) => { return fetcherFn( { url, query }, + // Transform the input array into an object args.reduce((obj, val, i) => { obj[input[i][0]!] = val return obj