mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
enable add and remove items (update) from cart. Update Search
This commit is contained in:
@@ -23,11 +23,7 @@ export const handler: MutationHook<Cart, {}, CartItemBody> = {
|
||||
message: 'The item quantity has to be a valid integer greater than 0',
|
||||
})
|
||||
}
|
||||
|
||||
const { checkoutLineItemsAdd } = await fetch<
|
||||
Mutation,
|
||||
MutationCheckoutLineItemsAddArgs
|
||||
>({
|
||||
const response = await fetch<Mutation, MutationCheckoutLineItemsAddArgs>({
|
||||
...options,
|
||||
variables: {
|
||||
checkoutId: getCheckoutId(),
|
||||
|
@@ -1,12 +1,12 @@
|
||||
import useCart, { UseCart } from '@commerce/cart/use-cart'
|
||||
import { Customer } from '@commerce/types'
|
||||
import { Cart } from '@commerce/types'
|
||||
import { SWRHook } from '@commerce/utils/types'
|
||||
import { normalizeCart } from '../utils/normalize'
|
||||
// import { getCustomerQuery, getCustomerToken } from '../utils'
|
||||
|
||||
export default useCart as UseCart<typeof handler>
|
||||
|
||||
export const handler: SWRHook<Customer | null> = {
|
||||
export const handler: SWRHook<Cart | null> = {
|
||||
fetchOptions: {
|
||||
query: 'cart',
|
||||
method: 'get',
|
||||
|
@@ -14,7 +14,6 @@ import useCart from './use-cart'
|
||||
import { handler as removeItemHandler } from './use-remove-item'
|
||||
import type { Cart, LineItem, UpdateCartItemBody } from '../types'
|
||||
import { checkoutToCart } from './utils'
|
||||
import { getCheckoutId, checkoutLineItemUpdateMutation } from '../utils'
|
||||
import { Mutation, MutationCheckoutLineItemsUpdateArgs } from '../schema'
|
||||
|
||||
export type UpdateItemInput<T = any> = T extends LineItem
|
||||
@@ -25,7 +24,8 @@ export default useUpdateItem as UseUpdateItem<typeof handler>
|
||||
|
||||
export const handler = {
|
||||
fetchOptions: {
|
||||
query: checkoutLineItemUpdateMutation,
|
||||
query: 'cart',
|
||||
method: 'updateItem',
|
||||
},
|
||||
async fetcher({
|
||||
input: { itemId, item },
|
||||
@@ -46,23 +46,14 @@ export const handler = {
|
||||
message: 'The item quantity has to be a valid integer',
|
||||
})
|
||||
}
|
||||
const { checkoutLineItemsUpdate } = await fetch<
|
||||
Mutation,
|
||||
MutationCheckoutLineItemsUpdateArgs
|
||||
>({
|
||||
...options,
|
||||
variables: {
|
||||
checkoutId: getCheckoutId(),
|
||||
lineItems: [
|
||||
{
|
||||
id: itemId,
|
||||
quantity: item.quantity,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
const response = await fetch<Mutation, MutationCheckoutLineItemsUpdateArgs>(
|
||||
{
|
||||
...options,
|
||||
variables: [item.itemId, { quantity: item.quantity }],
|
||||
}
|
||||
)
|
||||
|
||||
return checkoutToCart(checkoutLineItemsUpdate)
|
||||
return checkoutToCart(response)
|
||||
},
|
||||
useHook: ({
|
||||
fetch,
|
||||
@@ -75,13 +66,13 @@ export const handler = {
|
||||
} = {}
|
||||
) => {
|
||||
const { item } = ctx
|
||||
const { mutate } = useCart() as any
|
||||
const { mutate, data: cartData } = useCart() as any
|
||||
|
||||
return useCallback(
|
||||
debounce(async (input: UpdateItemInput<T>) => {
|
||||
const itemId = input.id ?? item?.id
|
||||
const productId = input.productId ?? item?.productId
|
||||
const variantId = input.productId ?? item?.variantId
|
||||
const itemId = cartData.lineItems[0].id
|
||||
const productId = cartData.lineItems[0].productId
|
||||
const variantId = cartData.lineItems[0].variant.id
|
||||
if (!itemId || !productId || !variantId) {
|
||||
throw new ValidationError({
|
||||
message: 'Invalid input used for this operation',
|
||||
@@ -91,6 +82,7 @@ export const handler = {
|
||||
const data = await fetch({
|
||||
input: {
|
||||
item: {
|
||||
itemId,
|
||||
productId,
|
||||
variantId,
|
||||
quantity: input.quantity,
|
||||
|
@@ -17,26 +17,11 @@ export type CheckoutPayload =
|
||||
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
|
||||
if (!checkoutPayload) {
|
||||
throw new CommerceError({
|
||||
message: 'Invalid response from Shopify',
|
||||
message: 'Invalid response from Swell',
|
||||
})
|
||||
}
|
||||
|
||||
const checkout = checkoutPayload?.checkout
|
||||
const userErrors = checkoutPayload?.userErrors
|
||||
|
||||
if (userErrors && userErrors.length) {
|
||||
throw new ValidationError({
|
||||
message: userErrors[0].message,
|
||||
})
|
||||
}
|
||||
|
||||
if (!checkout) {
|
||||
throw new CommerceError({
|
||||
message: 'Invalid response from Shopify',
|
||||
})
|
||||
}
|
||||
|
||||
return normalizeCart(checkout)
|
||||
return normalizeCart(checkoutPayload)
|
||||
}
|
||||
|
||||
export default checkoutToCart
|
||||
|
Reference in New Issue
Block a user