mirror of
https://github.com/vercel/commerce.git
synced 2025-03-14 22:42:33 +00:00
Updated cart update hooks
This commit is contained in:
parent
59a4535f0e
commit
f541a545f9
@ -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) {
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { normalizeCart } from '../lib/normalize'
|
||||
import type { HookFetcher } from '@commerce/utils/types'
|
||||
import type { SwrOptions } from '@commerce/utils/use-data'
|
||||
import useResponse from '@commerce/utils/use-response'
|
||||
import useCommerceCart, { CartInput } from '@commerce/cart/use-cart'
|
||||
import { normalizeCart } from '../lib/normalize'
|
||||
import type { Cart as BigcommerceCart } from '../api/cart'
|
||||
|
||||
const defaultOpts = {
|
||||
|
@ -1,8 +1,9 @@
|
||||
import { useCallback } from 'react'
|
||||
import { HookFetcher } from '@commerce/utils/types'
|
||||
import useCartRemoveItem from '@commerce/cart/use-remove-item'
|
||||
import type { RemoveItemBody } from '../api/cart'
|
||||
import useCart, { Cart } from './use-cart'
|
||||
import { normalizeCart } from '../lib/normalize'
|
||||
import type { RemoveItemBody, Cart as BigcommerceCart } from '../api/cart'
|
||||
import useCart from './use-cart'
|
||||
|
||||
const defaultOpts = {
|
||||
url: '/api/bigcommerce/cart',
|
||||
@ -13,16 +14,17 @@ export type RemoveItemInput = {
|
||||
id: string
|
||||
}
|
||||
|
||||
export const fetcher: HookFetcher<Cart | null, RemoveItemBody> = (
|
||||
export const fetcher: HookFetcher<Cart | null, RemoveItemBody> = async (
|
||||
options,
|
||||
{ itemId },
|
||||
fetch
|
||||
) => {
|
||||
return fetch({
|
||||
const data = await fetch<BigcommerceCart>({
|
||||
...defaultOpts,
|
||||
...options,
|
||||
body: { itemId },
|
||||
})
|
||||
return normalizeCart(data)
|
||||
}
|
||||
|
||||
export function extendHook(customFetcher: typeof fetcher) {
|
||||
|
@ -3,9 +3,14 @@ import debounce from 'lodash.debounce'
|
||||
import type { HookFetcher } from '@commerce/utils/types'
|
||||
import { CommerceError } from '@commerce/utils/errors'
|
||||
import useCartUpdateItem from '@commerce/cart/use-update-item'
|
||||
import type { ItemBody, UpdateItemBody } from '../api/cart'
|
||||
import { normalizeCart } from '../lib/normalize'
|
||||
import type {
|
||||
ItemBody,
|
||||
UpdateItemBody,
|
||||
Cart as BigcommerceCart,
|
||||
} from '../api/cart'
|
||||
import { fetcher as removeFetcher } from './use-remove-item'
|
||||
import useCart, { Cart } from './use-cart'
|
||||
import useCart from './use-cart'
|
||||
|
||||
const defaultOpts = {
|
||||
url: '/api/bigcommerce/cart',
|
||||
@ -14,7 +19,7 @@ const defaultOpts = {
|
||||
|
||||
export type UpdateItemInput = Partial<{ id: string } & ItemBody>
|
||||
|
||||
export const fetcher: HookFetcher<Cart | null, UpdateItemBody> = (
|
||||
export const fetcher: HookFetcher<Cart | null, UpdateItemBody> = async (
|
||||
options,
|
||||
{ itemId, item },
|
||||
fetch
|
||||
@ -30,11 +35,13 @@ export const fetcher: HookFetcher<Cart | null, UpdateItemBody> = (
|
||||
})
|
||||
}
|
||||
|
||||
return fetch({
|
||||
const data = await fetch<BigcommerceCart>({
|
||||
...defaultOpts,
|
||||
...options,
|
||||
body: { itemId, item },
|
||||
})
|
||||
|
||||
return normalizeCart(data)
|
||||
}
|
||||
|
||||
function extendHook(customFetcher: typeof fetcher, cfg?: { wait?: number }) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user