Hook fixes and update the customer after an operation

This commit is contained in:
Luis Alvarez 2020-10-23 21:49:33 -05:00
parent 89e7793d10
commit dca8f281ab
6 changed files with 31 additions and 15 deletions

View File

@ -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,

View File

@ -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(

View File

@ -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]

View File

@ -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]

View File

@ -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]

View File

@ -18,21 +18,30 @@ 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(
{ 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(
() => {