Hook fixes and update the customer after an operation
This commit is contained in:
		| @@ -43,8 +43,6 @@ const getLoggedInCustomer: CustomersHandlers['getLoggedInCustomer'] = async ({ | ||||
|     ) | ||||
|     const { customer } = data | ||||
|  | ||||
|     console.log('CUSTOMER', customer) | ||||
|  | ||||
|     if (!customer) { | ||||
|       return res.status(400).json({ | ||||
|         data: null, | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| import { HookFetcher } from '@lib/commerce/utils/types' | ||||
| import type { HookFetcher } from '@lib/commerce/utils/types' | ||||
| import type { SwrOptions } from '@lib/commerce/utils/use-data' | ||||
| import useCommerceCustomer from '@lib/commerce/use-customer' | ||||
| import type { Customer, CustomerData } from './api/customers' | ||||
| @@ -15,8 +15,8 @@ export const fetcher: HookFetcher<Customer | null> = async ( | ||||
|   _, | ||||
|   fetch | ||||
| ) => { | ||||
|   const data = await fetch<CustomerData>({ ...defaultOpts, ...options }) | ||||
|   return data.customer | ||||
|   const data = await fetch<CustomerData | null>({ ...defaultOpts, ...options }) | ||||
|   return data?.customer ?? null | ||||
| } | ||||
|  | ||||
| export function extendHook( | ||||
|   | ||||
| @@ -3,6 +3,7 @@ import type { HookFetcher } from '@lib/commerce/utils/types' | ||||
| import { CommerceError } from '@lib/commerce/utils/errors' | ||||
| import useCommerceLogin from '@lib/commerce/use-login' | ||||
| import type { LoginBody } from './api/customers/login' | ||||
| import useCustomer from './use-customer' | ||||
|  | ||||
| const defaultOpts = { | ||||
|   url: '/api/bigcommerce/customers/login', | ||||
| @@ -32,11 +33,13 @@ export const fetcher: HookFetcher<null, LoginBody> = ( | ||||
|  | ||||
| export function extendHook(customFetcher: typeof fetcher) { | ||||
|   const useLogin = () => { | ||||
|     const { revalidate } = useCustomer() | ||||
|     const fn = useCommerceLogin<null, LoginInput>(defaultOpts, customFetcher) | ||||
|  | ||||
|     return useCallback( | ||||
|       async function login(input: LoginInput) { | ||||
|         const data = await fn(input) | ||||
|         await revalidate() | ||||
|         return data | ||||
|       }, | ||||
|       [fn] | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| import { useCallback } from 'react' | ||||
| import type { HookFetcher } from '@lib/commerce/utils/types' | ||||
| import useCommerceLogout from '@lib/commerce/use-logout' | ||||
| import useCustomer from './use-customer' | ||||
|  | ||||
| const defaultOpts = { | ||||
|   url: '/api/bigcommerce/customers/logout', | ||||
| @@ -16,11 +17,13 @@ export const fetcher: HookFetcher<null> = (options, _, fetch) => { | ||||
|  | ||||
| export function extendHook(customFetcher: typeof fetcher) { | ||||
|   const useLogout = () => { | ||||
|     const { mutate } = useCustomer() | ||||
|     const fn = useCommerceLogout<null>(defaultOpts, customFetcher) | ||||
|  | ||||
|     return useCallback( | ||||
|       async function login() { | ||||
|         const data = await fn(null) | ||||
|         await mutate(null, false) | ||||
|         return data | ||||
|       }, | ||||
|       [fn] | ||||
|   | ||||
| @@ -3,6 +3,7 @@ import type { HookFetcher } from '@lib/commerce/utils/types' | ||||
| import { CommerceError } from '@lib/commerce/utils/errors' | ||||
| import useCommerceSignup from '@lib/commerce/use-signup' | ||||
| import type { SignupBody } from './api/customers/signup' | ||||
| import useCustomer from './use-customer' | ||||
|  | ||||
| const defaultOpts = { | ||||
|   url: '/api/bigcommerce/customers/signup', | ||||
| @@ -32,11 +33,13 @@ export const fetcher: HookFetcher<null, SignupBody> = ( | ||||
|  | ||||
| export function extendHook(customFetcher: typeof fetcher) { | ||||
|   const useSignup = () => { | ||||
|     const { revalidate } = useCustomer() | ||||
|     const fn = useCommerceSignup<null, SignupInput>(defaultOpts, customFetcher) | ||||
|  | ||||
|     return useCallback( | ||||
|       async function signup(input: SignupInput) { | ||||
|         const data = await fn(input) | ||||
|         await revalidate() | ||||
|         return data | ||||
|       }, | ||||
|       [fn] | ||||
|   | ||||
| @@ -18,13 +18,14 @@ export type UseData = <Result = any, Input = null>( | ||||
|  | ||||
| const useData: UseData = (options, input, fetcherFn, swrOptions) => { | ||||
|   const { fetcherRef } = useCommerce() | ||||
|   const fetcher = ( | ||||
|   const fetcher = async ( | ||||
|     url?: string, | ||||
|     query?: string, | ||||
|     method?: string, | ||||
|     ...args: any[] | ||||
|   ) => { | ||||
|     return fetcherFn( | ||||
|     try { | ||||
|       return await fetcherFn( | ||||
|         { url, query, method }, | ||||
|         // Transform the input array into an object | ||||
|         args.reduce((obj, val, i) => { | ||||
| @@ -33,6 +34,14 @@ const useData: UseData = (options, input, fetcherFn, swrOptions) => { | ||||
|         }, {}), | ||||
|         fetcherRef.current | ||||
|       ) | ||||
|     } catch (error) { | ||||
|       // SWR will not log errors, but any error that's not an instance | ||||
|       // of CommerceError is not welcomed by this hook | ||||
|       if (!(error instanceof CommerceError)) { | ||||
|         console.error(error) | ||||
|       } | ||||
|       throw error | ||||
|     } | ||||
|   } | ||||
|   const response = useSWR( | ||||
|     () => { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user