mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Type changes
This commit is contained in:
@@ -3,7 +3,7 @@ import { default as useCartAddItem } from '@lib/commerce/cart/use-add-item'
|
||||
import type { ItemBody, AddItemBody } from '../api/cart'
|
||||
import { Cart, useCart } from '.'
|
||||
|
||||
export type { ItemBody, AddItemBody }
|
||||
export type UpdateItemInput = ItemBody
|
||||
|
||||
function fetcher(fetch: Fetcher<Cart>, { item }: AddItemBody) {
|
||||
if (
|
||||
@@ -21,8 +21,8 @@ function fetcher(fetch: Fetcher<Cart>, { item }: AddItemBody) {
|
||||
export default function useAddItem() {
|
||||
const { mutate } = useCart()
|
||||
const fn = useCartAddItem<Cart, AddItemBody>(fetcher)
|
||||
const addItem: typeof fn = async (input) => {
|
||||
const data = await fn(input)
|
||||
const addItem = async (input: UpdateItemInput) => {
|
||||
const data = await fn({ item: input })
|
||||
await mutate(data, false)
|
||||
return data
|
||||
}
|
||||
|
@@ -1,9 +1,11 @@
|
||||
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 type { RemoveItemBody } from '../api/cart'
|
||||
import { Cart, useCart } from '.'
|
||||
|
||||
export type { ItemBody, RemoveItemBody }
|
||||
export type RemoveItemInput = {
|
||||
id: string
|
||||
}
|
||||
|
||||
export function fetcher(
|
||||
fetch: Fetcher<Cart | null>,
|
||||
@@ -16,11 +18,11 @@ export function fetcher(
|
||||
})
|
||||
}
|
||||
|
||||
export default function useRemoveItem() {
|
||||
export default function useRemoveItem(item?: any) {
|
||||
const { mutate } = useCart()
|
||||
const fn = useCartRemoveItem<Cart | null, RemoveItemBody>(fetcher)
|
||||
const removeItem: typeof fn = async (input) => {
|
||||
const data = await fn(input)
|
||||
const removeItem = async (input: RemoveItemInput) => {
|
||||
const data = await fn({ itemId: input.id ?? item?.id })
|
||||
await mutate(data, false)
|
||||
return data
|
||||
}
|
||||
|
@@ -26,15 +26,15 @@ function fetcher(
|
||||
})
|
||||
}
|
||||
|
||||
export default function useUpdateItem(item: any = {}) {
|
||||
export default function useUpdateItem(item?: any) {
|
||||
const { mutate } = useCart()
|
||||
const fn = useCartUpdateItem<Cart | null, UpdateItemBody>(fetcher)
|
||||
const updateItem = async (input: UpdateItemInput) => {
|
||||
const data = await fn({
|
||||
itemId: input.id ?? item.id,
|
||||
itemId: input.id ?? item?.id,
|
||||
item: {
|
||||
productId: input.productId ?? item.product_id,
|
||||
variantId: input.productId ?? item.variant_id,
|
||||
productId: input.productId ?? item?.product_id,
|
||||
variantId: input.productId ?? item?.variant_id,
|
||||
quantity: input.quantity,
|
||||
},
|
||||
})
|
||||
|
Reference in New Issue
Block a user