forked from crowetic/commerce
Added remove item from cart hook
This commit is contained in:
parent
45c3a5345d
commit
289c2948d6
26
lib/bigcommerce/cart/use-remove-item.tsx
Normal file
26
lib/bigcommerce/cart/use-remove-item.tsx
Normal file
@ -0,0 +1,26 @@
|
||||
import type { Fetcher } from '@lib/commerce'
|
||||
import { default as useCartRemoveItem } from '@lib/commerce/cart/use-remove-item'
|
||||
import type { ItemBody, RemoveItemBody } from '../api/cart'
|
||||
import { Cart, useCart } from '.'
|
||||
|
||||
export type { ItemBody, RemoveItemBody }
|
||||
|
||||
function fetcher(fetch: Fetcher<Cart>, { itemId }: RemoveItemBody) {
|
||||
return fetch({
|
||||
url: '/api/bigcommerce/cart',
|
||||
method: 'POST',
|
||||
body: { itemId },
|
||||
})
|
||||
}
|
||||
|
||||
export default function useRemoveItem() {
|
||||
const { mutate } = useCart()
|
||||
const fn = useCartRemoveItem<Cart, RemoveItemBody>(fetcher)
|
||||
const removeItem: typeof fn = async (input) => {
|
||||
const data = await fn(input)
|
||||
mutate(data)
|
||||
return data
|
||||
}
|
||||
|
||||
return removeItem
|
||||
}
|
11
lib/commerce/cart/use-remove-item.tsx
Normal file
11
lib/commerce/cart/use-remove-item.tsx
Normal file
@ -0,0 +1,11 @@
|
||||
import { Fetcher, useCommerce } from '..'
|
||||
|
||||
export default function useRemoveItem<T, Input>(
|
||||
fetcher: (fetch: Fetcher<T>, input: Input) => T | Promise<T>
|
||||
) {
|
||||
const { fetcher: fetch } = useCommerce()
|
||||
|
||||
return async function removeItem(input: Input) {
|
||||
return fetcher(fetch, input)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user