forked from crowetic/commerce
Making multiple changes
This commit is contained in:
parent
1de3e76513
commit
172b413521
@ -14,6 +14,9 @@ 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,6 +8,7 @@ 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'
|
||||||
|
|
||||||
type OptionSelections = {
|
type OptionSelections = {
|
||||||
option_id: Number
|
option_id: Number
|
||||||
@ -23,37 +24,12 @@ export type ItemBody = {
|
|||||||
|
|
||||||
export type AddItemBody = { item: ItemBody }
|
export type AddItemBody = { item: ItemBody }
|
||||||
|
|
||||||
export type UpdateItemBody = { itemId: string; item: ItemBody }
|
|
||||||
|
|
||||||
export type RemoveItemBody = { itemId: string }
|
export type RemoveItemBody = { itemId: string }
|
||||||
|
|
||||||
// TODO: this type should match:
|
|
||||||
// https://developer.bigcommerce.com/api-reference/cart-checkout/server-server-cart-api/cart/getacart#responses
|
|
||||||
export type Cart = {
|
|
||||||
id: string
|
|
||||||
parent_id?: string
|
|
||||||
customer_id: number
|
|
||||||
email: string
|
|
||||||
currency: { code: string }
|
|
||||||
tax_included: boolean
|
|
||||||
base_amount: number
|
|
||||||
discount_amount: number
|
|
||||||
cart_amount: number
|
|
||||||
line_items: {
|
|
||||||
custom_items: any[]
|
|
||||||
digital_items: any[]
|
|
||||||
gift_certificates: any[]
|
|
||||||
physical_items: any[]
|
|
||||||
}
|
|
||||||
created_time: string
|
|
||||||
discounts?: { id: number; discounted_amount: number }[]
|
|
||||||
// TODO: add missing fields
|
|
||||||
}
|
|
||||||
|
|
||||||
export type CartHandlers = {
|
export type CartHandlers = {
|
||||||
getCart: BigcommerceHandler<Cart, { cartId?: string }>
|
getCart: BigcommerceHandler<Cart, { cartId?: string }>
|
||||||
addItem: BigcommerceHandler<Cart, { cartId?: string } & Partial<AddItemBody>>
|
addItem: BigcommerceHandler<Cart, { cartId?: string } & Partial<AddItemBody>>
|
||||||
updateItem: BigcommerceHandler<Cart, UpdateItemBody>
|
updateItem: BigcommerceHandler<Cart, UpdateCartItemHandlerBody>
|
||||||
removeItem: BigcommerceHandler<
|
removeItem: BigcommerceHandler<
|
||||||
Cart,
|
Cart,
|
||||||
{ cartId?: string } & Partial<RemoveItemBody>
|
{ cartId?: string } & Partial<RemoveItemBody>
|
||||||
|
@ -1,14 +1,21 @@
|
|||||||
import type { ItemBody as WishlistItemBody } from '../wishlist'
|
import type { ItemBody as WishlistItemBody } from '../wishlist'
|
||||||
import type { ItemBody } from '../cart'
|
import type { CartItemBody, OptionSelections } from '../../types'
|
||||||
|
|
||||||
|
type BCCartItemBody = {
|
||||||
|
product_id: number
|
||||||
|
variant_id: number
|
||||||
|
quantity?: number
|
||||||
|
option_selections?: OptionSelections
|
||||||
|
}
|
||||||
|
|
||||||
export const parseWishlistItem = (item: WishlistItemBody) => ({
|
export const parseWishlistItem = (item: WishlistItemBody) => ({
|
||||||
product_id: item.productId,
|
product_id: item.productId,
|
||||||
variant_id: item.variantId,
|
variant_id: item.variantId,
|
||||||
})
|
})
|
||||||
|
|
||||||
export const parseCartItem = (item: ItemBody) => ({
|
export const parseCartItem = (item: CartItemBody): BCCartItemBody => ({
|
||||||
quantity: item.quantity,
|
quantity: item.quantity,
|
||||||
product_id: item.productId,
|
product_id: Number(item.productId),
|
||||||
variant_id: item.variantId,
|
variant_id: Number(item.variantId),
|
||||||
option_selections: item.optionSelections,
|
option_selections: item.optionSelections,
|
||||||
})
|
})
|
||||||
|
@ -5,10 +5,11 @@ import { CommerceError } from '@commerce/utils/errors'
|
|||||||
import useCartUpdateItem from '@commerce/cart/use-update-item'
|
import useCartUpdateItem from '@commerce/cart/use-update-item'
|
||||||
import { normalizeCart } from '../lib/normalize'
|
import { normalizeCart } from '../lib/normalize'
|
||||||
import type {
|
import type {
|
||||||
ItemBody,
|
Cart,
|
||||||
UpdateItemBody,
|
BigcommerceCart,
|
||||||
Cart as BigcommerceCart,
|
UpdateCartItemBody,
|
||||||
} from '../api/cart'
|
UpdateCartItemInput,
|
||||||
|
} from '../types'
|
||||||
import { fetcher as removeFetcher } from './use-remove-item'
|
import { fetcher as removeFetcher } from './use-remove-item'
|
||||||
import useCart from './use-cart'
|
import useCart from './use-cart'
|
||||||
|
|
||||||
@ -17,9 +18,7 @@ const defaultOpts = {
|
|||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
}
|
}
|
||||||
|
|
||||||
export type UpdateItemInput = Partial<{ id: string } & ItemBody>
|
export const fetcher: HookFetcher<Cart | null, UpdateCartItemBody> = async (
|
||||||
|
|
||||||
export const fetcher: HookFetcher<Cart | null, UpdateItemBody> = async (
|
|
||||||
options,
|
options,
|
||||||
{ itemId, item },
|
{ itemId, item },
|
||||||
fetch
|
fetch
|
||||||
@ -47,13 +46,21 @@ export const fetcher: HookFetcher<Cart | null, UpdateItemBody> = async (
|
|||||||
function extendHook(customFetcher: typeof fetcher, cfg?: { wait?: number }) {
|
function extendHook(customFetcher: typeof fetcher, cfg?: { wait?: number }) {
|
||||||
const useUpdateItem = (item?: any) => {
|
const useUpdateItem = (item?: any) => {
|
||||||
const { mutate } = useCart()
|
const { mutate } = useCart()
|
||||||
const fn = useCartUpdateItem<Cart | null, UpdateItemBody>(
|
const fn = useCartUpdateItem<Cart | null, UpdateCartItemBody>(
|
||||||
defaultOpts,
|
defaultOpts,
|
||||||
customFetcher
|
customFetcher
|
||||||
)
|
)
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
debounce(async (input: UpdateItemInput) => {
|
debounce(async (input: UpdateCartItemInput) => {
|
||||||
|
console.log('INPUT', input, {
|
||||||
|
itemId: input.id ?? item?.id,
|
||||||
|
item: {
|
||||||
|
productId: input.productId ?? item?.product_id,
|
||||||
|
variantId: input.productId ?? item?.variant_id,
|
||||||
|
quantity: input.quantity,
|
||||||
|
},
|
||||||
|
})
|
||||||
const data = await fn({
|
const data = await fn({
|
||||||
itemId: input.id ?? item?.id,
|
itemId: input.id ?? item?.id,
|
||||||
item: {
|
item: {
|
||||||
|
@ -1,11 +1,52 @@
|
|||||||
import * as Core from '@commerce/types'
|
import * as Core from '@commerce/types'
|
||||||
|
|
||||||
|
// TODO: this type should match:
|
||||||
|
// https://developer.bigcommerce.com/api-reference/cart-checkout/server-server-cart-api/cart/getacart#responses
|
||||||
|
export type BigcommerceCart = {
|
||||||
|
id: string
|
||||||
|
parent_id?: string
|
||||||
|
customer_id: number
|
||||||
|
email: string
|
||||||
|
currency: { code: string }
|
||||||
|
tax_included: boolean
|
||||||
|
base_amount: number
|
||||||
|
discount_amount: number
|
||||||
|
cart_amount: number
|
||||||
|
line_items: {
|
||||||
|
custom_items: any[]
|
||||||
|
digital_items: any[]
|
||||||
|
gift_certificates: any[]
|
||||||
|
physical_items: any[]
|
||||||
|
}
|
||||||
|
created_time: string
|
||||||
|
discounts?: { id: number; discounted_amount: number }[]
|
||||||
|
// TODO: add missing fields
|
||||||
|
}
|
||||||
|
|
||||||
export interface Cart extends Core.Cart {
|
export interface Cart extends Core.Cart {
|
||||||
lineItems: LineItem[]
|
lineItems: LineItem[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LineItem extends Core.LineItem {}
|
export interface LineItem extends Core.LineItem {}
|
||||||
|
|
||||||
export interface UpdateLineItemBody extends Core.UpdateLineItemBody {}
|
/**
|
||||||
|
* Cart mutations
|
||||||
|
*/
|
||||||
|
|
||||||
export interface UpdateLineItem extends Core.UpdateItemBody {}
|
export type OptionSelections = {
|
||||||
|
option_id: number
|
||||||
|
option_value: number | string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CartItemBody extends Core.CartItemBody {
|
||||||
|
optionSelections?: OptionSelections
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UpdateCartItemBody extends Core.UpdateCartItemBody {
|
||||||
|
item: CartItemBody
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UpdateCartItemInput extends Core.UpdateCartItemInput {}
|
||||||
|
|
||||||
|
export interface UpdateCartItemHandlerBody
|
||||||
|
extends Core.UpdateCartItemHandlerBody {}
|
||||||
|
@ -86,23 +86,25 @@ export interface Cart {
|
|||||||
discounts?: Discount[]
|
discounts?: Discount[]
|
||||||
}
|
}
|
||||||
|
|
||||||
// interface OptionSelections {
|
// Base cart item body used for cart mutations
|
||||||
// option_id: number
|
export interface CartItemBody {
|
||||||
// option_value: number | string
|
|
||||||
// }
|
|
||||||
|
|
||||||
export interface LineItemBody {
|
|
||||||
productId: string
|
productId: string
|
||||||
variantId: string
|
variantId: string
|
||||||
quantity?: number
|
quantity?: number
|
||||||
// optionSelections?: OptionSelections
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UpdateLineItemBody {
|
// Body by the update operation
|
||||||
|
export interface UpdateCartItemBody {
|
||||||
itemId: string
|
itemId: string
|
||||||
item: LineItemBody
|
item: CartItemBody
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UpdateItemBody extends Partial<UpdateLineItemBody> {
|
// Input expected by the `useUpdateItem` hook
|
||||||
|
export interface UpdateCartItemInput extends Partial<CartItemBody> {
|
||||||
|
id?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Body expected by the update operation handler
|
||||||
|
export interface UpdateCartItemHandlerBody extends Partial<UpdateCartItemBody> {
|
||||||
cartId?: string
|
cartId?: string
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user