From eb3f29fa954f83acf8615bf17bb21e241bff19ad Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Fri, 9 Oct 2020 11:10:58 -0500 Subject: [PATCH] updated the useUpdateItem hook --- lib/bigcommerce/cart/use-add-item.tsx | 4 +- lib/bigcommerce/cart/use-remove-item.tsx | 4 +- lib/bigcommerce/cart/use-update-item.tsx | 69 +++++++++++++++--------- lib/commerce/cart/use-update-item.tsx | 8 +-- lib/commerce/utils/types.ts | 2 +- 5 files changed, 53 insertions(+), 34 deletions(-) diff --git a/lib/bigcommerce/cart/use-add-item.tsx b/lib/bigcommerce/cart/use-add-item.tsx index 7c7c4b272..525bbb1d4 100644 --- a/lib/bigcommerce/cart/use-add-item.tsx +++ b/lib/bigcommerce/cart/use-add-item.tsx @@ -26,8 +26,8 @@ export const fetcher: HookFetcher = ( } return fetch({ - url: options.url!, - method: options.method!, + url: options?.url ?? defaultOpts.url, + method: options?.method ?? defaultOpts.method, body: { item }, }) } diff --git a/lib/bigcommerce/cart/use-remove-item.tsx b/lib/bigcommerce/cart/use-remove-item.tsx index 7d82f51db..39aae1089 100644 --- a/lib/bigcommerce/cart/use-remove-item.tsx +++ b/lib/bigcommerce/cart/use-remove-item.tsx @@ -19,8 +19,8 @@ export const fetcher: HookFetcher = ( fetch ) => { return fetch({ - url: options.url!, - method: options.method!, + url: options?.url ?? defaultOpts.url, + method: options?.method ?? defaultOpts.method, body: { itemId }, }) } diff --git a/lib/bigcommerce/cart/use-update-item.tsx b/lib/bigcommerce/cart/use-update-item.tsx index 792f9f61f..bf739dfda 100644 --- a/lib/bigcommerce/cart/use-update-item.tsx +++ b/lib/bigcommerce/cart/use-update-item.tsx @@ -1,50 +1,67 @@ import { useCallback } from 'react' import debounce from 'lodash.debounce' -import type { Fetcher } from '@lib/commerce' +import { HookFetcher } from '@lib/commerce/utils/types' import { default as useCartUpdateItem } from '@lib/commerce/cart/use-update-item' import type { ItemBody, UpdateItemBody } from '../api/cart' import { fetcher as removeFetcher } from './use-remove-item' import { Cart, useCart } from '.' +const defualtOpts = { + url: '/api/bigcommerce/cart', + method: 'PUT', +} + export type UpdateItemInput = Partial<{ id: string } & ItemBody> -function fetcher( - fetch: Fetcher, - { itemId, item }: UpdateItemBody -) { +export const fetcher: HookFetcher = ( + options, + { itemId, item }, + fetch +) => { if (Number.isInteger(item.quantity)) { // Also allow the update hook to remove an item if the quantity is lower than 1 if (item.quantity! < 1) { - return removeFetcher(fetch, { itemId }) + return removeFetcher(null, { itemId }, fetch) } } else if (item.quantity) { throw new Error('The item quantity has to be a valid integer') } return fetch({ - url: '/api/bigcommerce/cart', - method: 'PUT', + url: options?.url ?? defualtOpts.url, + method: options?.method ?? defualtOpts.method, body: { itemId, item }, }) } -export default function useUpdateItem(item?: any, cfg?: { wait?: number }) { - const { mutate } = useCart() - const fn = useCartUpdateItem(fetcher) +function extend(customFetcher: typeof fetcher, cfg?: { wait?: number }) { + const useUpdateItem = (item?: any) => { + const { mutate } = useCart() + const fn = useCartUpdateItem( + defualtOpts, + customFetcher + ) - return useCallback( - debounce(async (input: UpdateItemInput) => { - const data = await fn({ - itemId: input.id ?? item?.id, - item: { - productId: input.productId ?? item?.product_id, - variantId: input.productId ?? item?.variant_id, - quantity: input.quantity, - }, - }) - await mutate(data, false) - return data - }, cfg?.wait ?? 500), - [fn, mutate] - ) + return useCallback( + debounce(async (input: UpdateItemInput) => { + const data = await fn({ + itemId: input.id ?? item?.id, + item: { + productId: input.productId ?? item?.product_id, + variantId: input.productId ?? item?.variant_id, + quantity: input.quantity, + }, + }) + await mutate(data, false) + return data + }, cfg?.wait ?? 500), + [fn, mutate] + ) + } + + useUpdateItem.extend = extend + + return useUpdateItem } + +export default extend(fetcher) diff --git a/lib/commerce/cart/use-update-item.tsx b/lib/commerce/cart/use-update-item.tsx index d244f5f27..ac9344a74 100644 --- a/lib/commerce/cart/use-update-item.tsx +++ b/lib/commerce/cart/use-update-item.tsx @@ -1,14 +1,16 @@ import { useCallback } from 'react' -import { Fetcher, useCommerce } from '..' +import { HookFetcher, HookFetcherOptions } from '../utils/types' +import { useCommerce } from '..' export default function useUpdateItem( - fetcher: (fetch: Fetcher, input: Input) => T | Promise + options: HookFetcherOptions, + fetcher: HookFetcher ) { const { fetcherRef } = useCommerce() return useCallback( function updateItem(input: Input) { - return fetcher(fetcherRef.current, input) + return fetcher(options, input, fetcherRef.current) }, [fetcher] ) diff --git a/lib/commerce/utils/types.ts b/lib/commerce/utils/types.ts index 980e78e9b..aded044a6 100644 --- a/lib/commerce/utils/types.ts +++ b/lib/commerce/utils/types.ts @@ -10,7 +10,7 @@ export type FetcherOptions = { } export type HookFetcher = ( - options: HookFetcherOptions, + options: HookFetcherOptions | null, input: Input, fetch: Fetcher ) => T | Promise