forked from crowetic/commerce
Moved and updated cart types
This commit is contained in:
parent
e5f0809070
commit
023058dc0c
@ -1,6 +1,7 @@
|
|||||||
|
import type { BigcommerceCart } from '../../../types'
|
||||||
import { BigcommerceApiError } from '../../utils/errors'
|
import { BigcommerceApiError } from '../../utils/errors'
|
||||||
import getCartCookie from '../../utils/get-cart-cookie'
|
import getCartCookie from '../../utils/get-cart-cookie'
|
||||||
import type { Cart, CartHandlers } from '..'
|
import type { CartHandlers } from '../'
|
||||||
|
|
||||||
// Return current cart info
|
// Return current cart info
|
||||||
const getCart: CartHandlers['getCart'] = async ({
|
const getCart: CartHandlers['getCart'] = async ({
|
||||||
@ -8,7 +9,7 @@ const getCart: CartHandlers['getCart'] = async ({
|
|||||||
body: { cartId },
|
body: { cartId },
|
||||||
config,
|
config,
|
||||||
}) => {
|
}) => {
|
||||||
let result: { data?: Cart } = {}
|
let result: { data?: BigcommerceCart } = {}
|
||||||
|
|
||||||
if (cartId) {
|
if (cartId) {
|
||||||
try {
|
try {
|
||||||
|
@ -14,9 +14,6 @@ const updateItem: CartHandlers['updateItem'] = async ({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('ITEM', item)
|
|
||||||
console.log('AFTER', parseCartItem(item))
|
|
||||||
|
|
||||||
const { data } = await config.storeApiFetch(
|
const { data } = await config.storeApiFetch(
|
||||||
`/v3/carts/${cartId}/items/${itemId}`,
|
`/v3/carts/${cartId}/items/${itemId}`,
|
||||||
{
|
{
|
||||||
|
@ -8,7 +8,11 @@ import getCart from './handlers/get-cart'
|
|||||||
import addItem from './handlers/add-item'
|
import addItem from './handlers/add-item'
|
||||||
import updateItem from './handlers/update-item'
|
import updateItem from './handlers/update-item'
|
||||||
import removeItem from './handlers/remove-item'
|
import removeItem from './handlers/remove-item'
|
||||||
import type { Cart, UpdateCartItemHandlerBody } from '../../types'
|
import type {
|
||||||
|
BigcommerceCart,
|
||||||
|
GetCartHandlerBody,
|
||||||
|
UpdateCartItemHandlerBody,
|
||||||
|
} from '../../types'
|
||||||
|
|
||||||
type OptionSelections = {
|
type OptionSelections = {
|
||||||
option_id: Number
|
option_id: Number
|
||||||
@ -27,11 +31,14 @@ export type AddItemBody = { item: ItemBody }
|
|||||||
export type RemoveItemBody = { itemId: string }
|
export type RemoveItemBody = { itemId: string }
|
||||||
|
|
||||||
export type CartHandlers = {
|
export type CartHandlers = {
|
||||||
getCart: BigcommerceHandler<Cart, { cartId?: string }>
|
getCart: BigcommerceHandler<BigcommerceCart, GetCartHandlerBody>
|
||||||
addItem: BigcommerceHandler<Cart, { cartId?: string } & Partial<AddItemBody>>
|
addItem: BigcommerceHandler<
|
||||||
updateItem: BigcommerceHandler<Cart, UpdateCartItemHandlerBody>
|
BigcommerceCart,
|
||||||
|
{ cartId?: string } & Partial<AddItemBody>
|
||||||
|
>
|
||||||
|
updateItem: BigcommerceHandler<BigcommerceCart, UpdateCartItemHandlerBody>
|
||||||
removeItem: BigcommerceHandler<
|
removeItem: BigcommerceHandler<
|
||||||
Cart,
|
BigcommerceCart,
|
||||||
{ cartId?: string } & Partial<RemoveItemBody>
|
{ cartId?: string } & Partial<RemoveItemBody>
|
||||||
>
|
>
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ 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 { normalizeCart } from '../lib/normalize'
|
||||||
import type { Cart as BigcommerceCart } from '../api/cart'
|
import type { Cart, BigcommerceCart } from '../types'
|
||||||
|
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
url: '/api/bigcommerce/cart',
|
url: '/api/bigcommerce/cart',
|
||||||
|
@ -2,11 +2,12 @@ import { useCallback } from 'react'
|
|||||||
import debounce from 'lodash.debounce'
|
import debounce from 'lodash.debounce'
|
||||||
import type { HookFetcher } from '@commerce/utils/types'
|
import type { HookFetcher } from '@commerce/utils/types'
|
||||||
import { ValidationError } from '@commerce/utils/errors'
|
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 { normalizeCart } from '../lib/normalize'
|
||||||
import type {
|
import type {
|
||||||
UpdateCartItemBody,
|
UpdateCartItemBody,
|
||||||
UpdateCartItemInput,
|
|
||||||
Cart,
|
Cart,
|
||||||
BigcommerceCart,
|
BigcommerceCart,
|
||||||
LineItem,
|
LineItem,
|
||||||
@ -19,6 +20,10 @@ const defaultOpts = {
|
|||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type UpdateItemInput<T = any> = T extends LineItem
|
||||||
|
? Partial<UseUpdateItemInput<LineItem>>
|
||||||
|
: UseUpdateItemInput<LineItem>
|
||||||
|
|
||||||
export const fetcher: HookFetcher<Cart | null, UpdateCartItemBody> = async (
|
export const fetcher: HookFetcher<Cart | null, UpdateCartItemBody> = async (
|
||||||
options,
|
options,
|
||||||
{ itemId, item },
|
{ itemId, item },
|
||||||
@ -55,31 +60,24 @@ function extendHook(customFetcher: typeof fetcher, cfg?: { wait?: number }) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
debounce(
|
debounce(async (input: UpdateItemInput<T>) => {
|
||||||
async (
|
const itemId = input.id ?? item?.id
|
||||||
input: T extends LineItem
|
const productId = input.productId ?? item?.productId
|
||||||
? Partial<UpdateCartItemInput>
|
const variantId = input.productId ?? item?.variantId
|
||||||
: UpdateCartItemInput
|
|
||||||
) => {
|
|
||||||
const itemId = input.id ?? item?.id
|
|
||||||
const productId = input.productId ?? item?.productId
|
|
||||||
const variantId = input.productId ?? item?.variantId
|
|
||||||
|
|
||||||
if (!itemId || !productId || !variantId) {
|
if (!itemId || !productId || !variantId) {
|
||||||
throw new ValidationError({
|
throw new ValidationError({
|
||||||
message: 'Invalid input used for this operation',
|
message: 'Invalid input used for this operation',
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await fn({
|
|
||||||
itemId,
|
|
||||||
item: { productId, variantId, quantity: input.quantity },
|
|
||||||
})
|
})
|
||||||
await mutate(data, false)
|
}
|
||||||
return data
|
|
||||||
},
|
const data = await fn({
|
||||||
cfg?.wait ?? 500
|
itemId,
|
||||||
),
|
item: { productId, variantId, quantity: input.quantity },
|
||||||
|
})
|
||||||
|
await mutate(data, false)
|
||||||
|
return data
|
||||||
|
}, cfg?.wait ?? 500),
|
||||||
[fn, mutate]
|
[fn, mutate]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -43,12 +43,10 @@ export interface CartItemBody extends Core.CartItemBody {
|
|||||||
optionSelections?: OptionSelections
|
optionSelections?: OptionSelections
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UpdateCartItemBody extends Core.UpdateCartItemBody {
|
export interface GetCartHandlerBody extends Core.GetCartHandlerBody {}
|
||||||
item: CartItemBody
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UpdateCartItemInput
|
export interface UpdateCartItemBody
|
||||||
extends Core.UpdateCartItemInput<CartItemBody> {}
|
extends Core.UpdateCartItemBody<CartItemBody> {}
|
||||||
|
|
||||||
export interface UpdateCartItemHandlerBody
|
export interface UpdateCartItemHandlerBody
|
||||||
extends Core.UpdateCartItemHandlerBody {}
|
extends Core.UpdateCartItemHandlerBody<CartItemBody> {}
|
||||||
|
@ -6,6 +6,7 @@ import { useCommerce } from '..'
|
|||||||
|
|
||||||
export type CartResponse<Data> = ResponseState<Data> & { isEmpty?: boolean }
|
export type CartResponse<Data> = ResponseState<Data> & { isEmpty?: boolean }
|
||||||
|
|
||||||
|
// Input expected by the `useCart` hook
|
||||||
export type CartInput = {
|
export type CartInput = {
|
||||||
cartId?: Cart['id']
|
cartId?: Cart['id']
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,10 @@
|
|||||||
import useAction from '../utils/use-action'
|
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
|
const useUpdateItem = useAction
|
||||||
|
|
||||||
|
@ -94,18 +94,19 @@ export interface CartItemBody {
|
|||||||
quantity?: number
|
quantity?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
// Body by the update operation
|
// Body used by the `getCart` operation handler
|
||||||
export interface UpdateCartItemBody {
|
export interface GetCartHandlerBody {
|
||||||
itemId: string
|
cartId?: string
|
||||||
item: CartItemBody
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Input expected by the `useUpdateItem` hook
|
// Body used by the update operation
|
||||||
export type UpdateCartItemInput<T extends CartItemBody> = T & {
|
export interface UpdateCartItemBody<T extends CartItemBody> {
|
||||||
id: string
|
itemId: string
|
||||||
|
item: T
|
||||||
}
|
}
|
||||||
|
|
||||||
// Body expected by the update operation handler
|
// 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
|
cartId?: string
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user