Fix provider errors

This commit is contained in:
goncy
2021-09-24 18:25:09 -03:00
parent f3bffa85ff
commit eb2aca92ad
11 changed files with 149 additions and 221 deletions

View File

@@ -2,20 +2,22 @@ import type {
MutationHookContext,
HookFetcherContext,
} from '@commerce/utils/types'
import type { Cart, LineItem, RemoveItemHook } from '@commerce/types/customer/card'
import type { Card, RemoveItemHook } from '@commerce/types/customer/card'
import { useCallback } from 'react'
import { ValidationError } from '@commerce/utils/errors'
import useRemoveItem, { UseRemoveItem } from '@commerce/customer/card/use-remove-item'
import useRemoveItem, {
UseRemoveItem,
} from '@commerce/customer/card/use-remove-item'
import useCards from './use-cards'
export type RemoveItemFn<T = any> = T extends LineItem
? (input?: RemoveItemActionInput<T>) => Promise<Cart | null | undefined>
: (input: RemoveItemActionInput<T>) => Promise<Cart | null>
export type RemoveItemFn<T = any> = T extends Card
? (input?: RemoveItemActionInput<T>) => Promise<Card | null | undefined>
: (input: RemoveItemActionInput<T>) => Promise<Card | null>
export type RemoveItemActionInput<T = any> = T extends LineItem
export type RemoveItemActionInput<T = any> = T extends Card
? Partial<RemoveItemHook['actionInput']>
: RemoveItemHook['actionInput']
@@ -34,12 +36,12 @@ export const handler = {
return await fetch({ ...options, body: { itemId } })
},
useHook: ({ fetch }: MutationHookContext<RemoveItemHook>) =>
function useHook<T extends LineItem | undefined = undefined>(
function useHook<T extends Card | undefined = undefined>(
ctx: { item?: T } = {}
) {
const { item } = ctx
const { mutate } = useCards()
const removeItem: RemoveItemFn<LineItem> = async (input) => {
const removeItem: RemoveItemFn<Card> = async (input) => {
const itemId = input?.id ?? item?.id
if (!itemId) {
@@ -50,7 +52,7 @@ export const handler = {
const data = await fetch({ input: { itemId } })
await mutate(data, false)
await mutate([], false)
return data
}