From dca8f281abb60400698e896226fdf4637257f5f6 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Fri, 23 Oct 2020 21:49:33 -0500 Subject: [PATCH] Hook fixes and update the customer after an operation --- .../handlers/get-logged-in-customer.ts | 2 -- lib/bigcommerce/use-customer.tsx | 6 ++-- lib/bigcommerce/use-login.tsx | 3 ++ lib/bigcommerce/use-logout.tsx | 3 ++ lib/bigcommerce/use-signup.tsx | 3 ++ lib/commerce/utils/use-data.tsx | 29 ++++++++++++------- 6 files changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/bigcommerce/api/customers/handlers/get-logged-in-customer.ts b/lib/bigcommerce/api/customers/handlers/get-logged-in-customer.ts index 170457f59..b86a8a01a 100644 --- a/lib/bigcommerce/api/customers/handlers/get-logged-in-customer.ts +++ b/lib/bigcommerce/api/customers/handlers/get-logged-in-customer.ts @@ -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, diff --git a/lib/bigcommerce/use-customer.tsx b/lib/bigcommerce/use-customer.tsx index 85266fbd0..173f1db9c 100644 --- a/lib/bigcommerce/use-customer.tsx +++ b/lib/bigcommerce/use-customer.tsx @@ -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 = async ( _, fetch ) => { - const data = await fetch({ ...defaultOpts, ...options }) - return data.customer + const data = await fetch({ ...defaultOpts, ...options }) + return data?.customer ?? null } export function extendHook( diff --git a/lib/bigcommerce/use-login.tsx b/lib/bigcommerce/use-login.tsx index d0465cc5a..fa91e54b9 100644 --- a/lib/bigcommerce/use-login.tsx +++ b/lib/bigcommerce/use-login.tsx @@ -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 = ( export function extendHook(customFetcher: typeof fetcher) { const useLogin = () => { + const { revalidate } = useCustomer() const fn = useCommerceLogin(defaultOpts, customFetcher) return useCallback( async function login(input: LoginInput) { const data = await fn(input) + await revalidate() return data }, [fn] diff --git a/lib/bigcommerce/use-logout.tsx b/lib/bigcommerce/use-logout.tsx index f4b5f9beb..d12afb4d6 100644 --- a/lib/bigcommerce/use-logout.tsx +++ b/lib/bigcommerce/use-logout.tsx @@ -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 = (options, _, fetch) => { export function extendHook(customFetcher: typeof fetcher) { const useLogout = () => { + const { mutate } = useCustomer() const fn = useCommerceLogout(defaultOpts, customFetcher) return useCallback( async function login() { const data = await fn(null) + await mutate(null, false) return data }, [fn] diff --git a/lib/bigcommerce/use-signup.tsx b/lib/bigcommerce/use-signup.tsx index 709d3f2cf..4e98e7705 100644 --- a/lib/bigcommerce/use-signup.tsx +++ b/lib/bigcommerce/use-signup.tsx @@ -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 = ( export function extendHook(customFetcher: typeof fetcher) { const useSignup = () => { + const { revalidate } = useCustomer() const fn = useCommerceSignup(defaultOpts, customFetcher) return useCallback( async function signup(input: SignupInput) { const data = await fn(input) + await revalidate() return data }, [fn] diff --git a/lib/commerce/utils/use-data.tsx b/lib/commerce/utils/use-data.tsx index 7d94b408b..dd5917ccb 100644 --- a/lib/commerce/utils/use-data.tsx +++ b/lib/commerce/utils/use-data.tsx @@ -18,21 +18,30 @@ export type UseData = ( 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( - { url, query, method }, - // Transform the input array into an object - args.reduce((obj, val, i) => { - obj[input[i][0]!] = val - return obj - }, {}), - fetcherRef.current - ) + try { + return await fetcherFn( + { url, query, method }, + // Transform the input array into an object + args.reduce((obj, val, i) => { + obj[input[i][0]!] = val + return obj + }, {}), + 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( () => {