forked from crowetic/commerce
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 type { HookFetcher } from '@commerce/utils/types'
|
||||||
import { CommerceError } from '@commerce/utils/errors'
|
import { CommerceError } from '@commerce/utils/errors'
|
||||||
import useCartAddItem from '@commerce/cart/use-add-item'
|
import useCartAddItem from '@commerce/cart/use-add-item'
|
||||||
import type { ItemBody, AddItemBody } from '../api/cart'
|
import { normalizeCart } from '../lib/normalize'
|
||||||
import useCart, { Cart } from './use-cart'
|
import type {
|
||||||
|
ItemBody,
|
||||||
|
AddItemBody,
|
||||||
|
Cart as BigcommerceCart,
|
||||||
|
} from '../api/cart'
|
||||||
|
import useCart from './use-cart'
|
||||||
|
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
url: '/api/bigcommerce/cart',
|
url: '/api/bigcommerce/cart',
|
||||||
@ -12,7 +17,7 @@ const defaultOpts = {
|
|||||||
|
|
||||||
export type AddItemInput = ItemBody
|
export type AddItemInput = ItemBody
|
||||||
|
|
||||||
export const fetcher: HookFetcher<Cart, AddItemBody> = (
|
export const fetcher: HookFetcher<Cart, AddItemBody> = async (
|
||||||
options,
|
options,
|
||||||
{ item },
|
{ item },
|
||||||
fetch
|
fetch
|
||||||
@ -26,11 +31,13 @@ export const fetcher: HookFetcher<Cart, AddItemBody> = (
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch({
|
const data = await fetch<BigcommerceCart>({
|
||||||
...defaultOpts,
|
...defaultOpts,
|
||||||
...options,
|
...options,
|
||||||
body: { item },
|
body: { item },
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return normalizeCart(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
export function extendHook(customFetcher: typeof fetcher) {
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { normalizeCart } from '../lib/normalize'
|
|
||||||
import type { HookFetcher } from '@commerce/utils/types'
|
import type { HookFetcher } from '@commerce/utils/types'
|
||||||
import type { SwrOptions } from '@commerce/utils/use-data'
|
import type { SwrOptions } from '@commerce/utils/use-data'
|
||||||
import useResponse from '@commerce/utils/use-response'
|
import useResponse from '@commerce/utils/use-response'
|
||||||
import useCommerceCart, { CartInput } from '@commerce/cart/use-cart'
|
import useCommerceCart, { CartInput } from '@commerce/cart/use-cart'
|
||||||
|
import { normalizeCart } from '../lib/normalize'
|
||||||
import type { Cart as BigcommerceCart } from '../api/cart'
|
import type { Cart as BigcommerceCart } from '../api/cart'
|
||||||
|
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import { HookFetcher } from '@commerce/utils/types'
|
import { HookFetcher } from '@commerce/utils/types'
|
||||||
import useCartRemoveItem from '@commerce/cart/use-remove-item'
|
import useCartRemoveItem from '@commerce/cart/use-remove-item'
|
||||||
import type { RemoveItemBody } from '../api/cart'
|
import { normalizeCart } from '../lib/normalize'
|
||||||
import useCart, { Cart } from './use-cart'
|
import type { RemoveItemBody, Cart as BigcommerceCart } from '../api/cart'
|
||||||
|
import useCart from './use-cart'
|
||||||
|
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
url: '/api/bigcommerce/cart',
|
url: '/api/bigcommerce/cart',
|
||||||
@ -13,16 +14,17 @@ export type RemoveItemInput = {
|
|||||||
id: string
|
id: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const fetcher: HookFetcher<Cart | null, RemoveItemBody> = (
|
export const fetcher: HookFetcher<Cart | null, RemoveItemBody> = async (
|
||||||
options,
|
options,
|
||||||
{ itemId },
|
{ itemId },
|
||||||
fetch
|
fetch
|
||||||
) => {
|
) => {
|
||||||
return fetch({
|
const data = await fetch<BigcommerceCart>({
|
||||||
...defaultOpts,
|
...defaultOpts,
|
||||||
...options,
|
...options,
|
||||||
body: { itemId },
|
body: { itemId },
|
||||||
})
|
})
|
||||||
|
return normalizeCart(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
export function extendHook(customFetcher: typeof fetcher) {
|
||||||
|
@ -3,9 +3,14 @@ import debounce from 'lodash.debounce'
|
|||||||
import type { HookFetcher } from '@commerce/utils/types'
|
import type { HookFetcher } from '@commerce/utils/types'
|
||||||
import { CommerceError } from '@commerce/utils/errors'
|
import { CommerceError } from '@commerce/utils/errors'
|
||||||
import useCartUpdateItem from '@commerce/cart/use-update-item'
|
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 { fetcher as removeFetcher } from './use-remove-item'
|
||||||
import useCart, { Cart } from './use-cart'
|
import useCart from './use-cart'
|
||||||
|
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
url: '/api/bigcommerce/cart',
|
url: '/api/bigcommerce/cart',
|
||||||
@ -14,7 +19,7 @@ const defaultOpts = {
|
|||||||
|
|
||||||
export type UpdateItemInput = Partial<{ id: string } & ItemBody>
|
export type UpdateItemInput = Partial<{ id: string } & ItemBody>
|
||||||
|
|
||||||
export const fetcher: HookFetcher<Cart | null, UpdateItemBody> = (
|
export const fetcher: HookFetcher<Cart | null, UpdateItemBody> = async (
|
||||||
options,
|
options,
|
||||||
{ itemId, item },
|
{ itemId, item },
|
||||||
fetch
|
fetch
|
||||||
@ -30,11 +35,13 @@ export const fetcher: HookFetcher<Cart | null, UpdateItemBody> = (
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch({
|
const data = await fetch<BigcommerceCart>({
|
||||||
...defaultOpts,
|
...defaultOpts,
|
||||||
...options,
|
...options,
|
||||||
body: { itemId, item },
|
body: { itemId, item },
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return normalizeCart(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
function extendHook(customFetcher: typeof fetcher, cfg?: { wait?: number }) {
|
function extendHook(customFetcher: typeof fetcher, cfg?: { wait?: number }) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user