Updated cart update hooks

This commit is contained in:
Luis Alvarez
2021-01-28 18:37:07 -05:00
parent 59a4535f0e
commit f541a545f9
4 changed files with 29 additions and 13 deletions

View File

@@ -2,8 +2,13 @@ import { useCallback } from 'react'
import type { HookFetcher } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors'
import useCartAddItem from '@commerce/cart/use-add-item'
import type { ItemBody, AddItemBody } from '../api/cart'
import useCart, { Cart } from './use-cart'
import { normalizeCart } from '../lib/normalize'
import type {
ItemBody,
AddItemBody,
Cart as BigcommerceCart,
} from '../api/cart'
import useCart from './use-cart'
const defaultOpts = {
url: '/api/bigcommerce/cart',
@@ -12,7 +17,7 @@ const defaultOpts = {
export type AddItemInput = ItemBody
export const fetcher: HookFetcher<Cart, AddItemBody> = (
export const fetcher: HookFetcher<Cart, AddItemBody> = async (
options,
{ item },
fetch
@@ -26,11 +31,13 @@ export const fetcher: HookFetcher<Cart, AddItemBody> = (
})
}
return fetch({
const data = await fetch<BigcommerceCart>({
...defaultOpts,
...options,
body: { item },
})
return normalizeCart(data)
}
export function extendHook(customFetcher: typeof fetcher) {