4
0
forked from crowetic/commerce

Moved and updated cart types

This commit is contained in:
Luis Alvarez 2021-02-02 17:49:05 -05:00
parent e5f0809070
commit 023058dc0c
9 changed files with 59 additions and 50 deletions

View File

@ -1,6 +1,7 @@
import type { BigcommerceCart } from '../../../types'
import { BigcommerceApiError } from '../../utils/errors'
import getCartCookie from '../../utils/get-cart-cookie'
import type { Cart, CartHandlers } from '..'
import type { CartHandlers } from '../'
// Return current cart info
const getCart: CartHandlers['getCart'] = async ({
@ -8,7 +9,7 @@ const getCart: CartHandlers['getCart'] = async ({
body: { cartId },
config,
}) => {
let result: { data?: Cart } = {}
let result: { data?: BigcommerceCart } = {}
if (cartId) {
try {

View File

@ -14,9 +14,6 @@ const updateItem: CartHandlers['updateItem'] = async ({
})
}
console.log('ITEM', item)
console.log('AFTER', parseCartItem(item))
const { data } = await config.storeApiFetch(
`/v3/carts/${cartId}/items/${itemId}`,
{

View File

@ -8,7 +8,11 @@ import getCart from './handlers/get-cart'
import addItem from './handlers/add-item'
import updateItem from './handlers/update-item'
import removeItem from './handlers/remove-item'
import type { Cart, UpdateCartItemHandlerBody } from '../../types'
import type {
BigcommerceCart,
GetCartHandlerBody,
UpdateCartItemHandlerBody,
} from '../../types'
type OptionSelections = {
option_id: Number
@ -27,11 +31,14 @@ export type AddItemBody = { item: ItemBody }
export type RemoveItemBody = { itemId: string }
export type CartHandlers = {
getCart: BigcommerceHandler<Cart, { cartId?: string }>
addItem: BigcommerceHandler<Cart, { cartId?: string } & Partial<AddItemBody>>
updateItem: BigcommerceHandler<Cart, UpdateCartItemHandlerBody>
getCart: BigcommerceHandler<BigcommerceCart, GetCartHandlerBody>
addItem: BigcommerceHandler<
BigcommerceCart,
{ cartId?: string } & Partial<AddItemBody>
>
updateItem: BigcommerceHandler<BigcommerceCart, UpdateCartItemHandlerBody>
removeItem: BigcommerceHandler<
Cart,
BigcommerceCart,
{ cartId?: string } & Partial<RemoveItemBody>
>
}

View File

@ -3,7 +3,7 @@ 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'
import type { Cart, BigcommerceCart } from '../types'
const defaultOpts = {
url: '/api/bigcommerce/cart',

View File

@ -2,11 +2,12 @@ import { useCallback } from 'react'
import debounce from 'lodash.debounce'
import type { HookFetcher } from '@commerce/utils/types'
import { ValidationError } from '@commerce/utils/errors'
import useCartUpdateItem from '@commerce/cart/use-update-item'
import useCartUpdateItem, {
UpdateItemInput as UseUpdateItemInput,
} from '@commerce/cart/use-update-item'
import { normalizeCart } from '../lib/normalize'
import type {
UpdateCartItemBody,
UpdateCartItemInput,
Cart,
BigcommerceCart,
LineItem,
@ -19,6 +20,10 @@ const defaultOpts = {
method: 'PUT',
}
export type UpdateItemInput<T = any> = T extends LineItem
? Partial<UseUpdateItemInput<LineItem>>
: UseUpdateItemInput<LineItem>
export const fetcher: HookFetcher<Cart | null, UpdateCartItemBody> = async (
options,
{ itemId, item },
@ -55,31 +60,24 @@ function extendHook(customFetcher: typeof fetcher, cfg?: { wait?: number }) {
)
return useCallback(
debounce(
async (
input: T extends LineItem
? Partial<UpdateCartItemInput>
: UpdateCartItemInput
) => {
const itemId = input.id ?? item?.id
const productId = input.productId ?? item?.productId
const variantId = input.productId ?? item?.variantId
debounce(async (input: UpdateItemInput<T>) => {
const itemId = input.id ?? item?.id
const productId = input.productId ?? item?.productId
const variantId = input.productId ?? item?.variantId
if (!itemId || !productId || !variantId) {
throw new ValidationError({
message: 'Invalid input used for this operation',
})
}
const data = await fn({
itemId,
item: { productId, variantId, quantity: input.quantity },
if (!itemId || !productId || !variantId) {
throw new ValidationError({
message: 'Invalid input used for this operation',
})
await mutate(data, false)
return data
},
cfg?.wait ?? 500
),
}
const data = await fn({
itemId,
item: { productId, variantId, quantity: input.quantity },
})
await mutate(data, false)
return data
}, cfg?.wait ?? 500),
[fn, mutate]
)
}

View File

@ -43,12 +43,10 @@ export interface CartItemBody extends Core.CartItemBody {
optionSelections?: OptionSelections
}
export interface UpdateCartItemBody extends Core.UpdateCartItemBody {
item: CartItemBody
}
export interface GetCartHandlerBody extends Core.GetCartHandlerBody {}
export interface UpdateCartItemInput
extends Core.UpdateCartItemInput<CartItemBody> {}
export interface UpdateCartItemBody
extends Core.UpdateCartItemBody<CartItemBody> {}
export interface UpdateCartItemHandlerBody
extends Core.UpdateCartItemHandlerBody {}
extends Core.UpdateCartItemHandlerBody<CartItemBody> {}

View File

@ -6,6 +6,7 @@ import { useCommerce } from '..'
export type CartResponse<Data> = ResponseState<Data> & { isEmpty?: boolean }
// Input expected by the `useCart` hook
export type CartInput = {
cartId?: Cart['id']
}

View File

@ -1,4 +1,10 @@
import useAction from '../utils/use-action'
import type { CartItemBody } from '../types'
// Input expected by the action returned by the `useUpdateItem` hook
export type UpdateItemInput<T extends CartItemBody> = T & {
id: string
}
const useUpdateItem = useAction

View File

@ -94,18 +94,19 @@ export interface CartItemBody {
quantity?: number
}
// Body by the update operation
export interface UpdateCartItemBody {
itemId: string
item: CartItemBody
// Body used by the `getCart` operation handler
export interface GetCartHandlerBody {
cartId?: string
}
// Input expected by the `useUpdateItem` hook
export type UpdateCartItemInput<T extends CartItemBody> = T & {
id: string
// Body used by the update operation
export interface UpdateCartItemBody<T extends CartItemBody> {
itemId: string
item: T
}
// Body expected by the update operation handler
export interface UpdateCartItemHandlerBody extends Partial<UpdateCartItemBody> {
export interface UpdateCartItemHandlerBody<T extends CartItemBody>
extends Partial<UpdateCartItemBody<T>> {
cartId?: string
}