mirror of
https://github.com/vercel/commerce.git
synced 2025-06-20 06:01:21 +00:00
saleor: adding/removing from the cart
This commit is contained in:
parent
2c097b0f7e
commit
7d80fadd82
@ -16,7 +16,7 @@ import {
|
|||||||
checkoutToCart,
|
checkoutToCart,
|
||||||
} from '../utils'
|
} from '../utils'
|
||||||
import { Cart, LineItem } from '../types'
|
import { Cart, LineItem } from '../types'
|
||||||
import { Mutation, MutationCheckoutLineItemsRemoveArgs } from '../schema'
|
import { Mutation, MutationCheckoutLineDeleteArgs } from '../schema'
|
||||||
|
|
||||||
export type RemoveItemFn<T = any> = T extends LineItem
|
export type RemoveItemFn<T = any> = T extends LineItem
|
||||||
? (input?: RemoveItemInput<T>) => Promise<Cart | null>
|
? (input?: RemoveItemInput<T>) => Promise<Cart | null>
|
||||||
@ -37,11 +37,14 @@ export const handler = {
|
|||||||
options,
|
options,
|
||||||
fetch,
|
fetch,
|
||||||
}: HookFetcherContext<RemoveCartItemBody>) {
|
}: HookFetcherContext<RemoveCartItemBody>) {
|
||||||
const data = await fetch<Mutation, MutationCheckoutLineItemsRemoveArgs>({
|
const data = await fetch<Mutation, MutationCheckoutLineDeleteArgs>({
|
||||||
...options,
|
...options,
|
||||||
variables: { checkoutId: getCheckoutId(), lineItemIds: [itemId] },
|
variables: {
|
||||||
|
checkoutId: getCheckoutId().checkoutId,
|
||||||
|
lineId: itemId
|
||||||
|
},
|
||||||
})
|
})
|
||||||
return checkoutToCart(data.checkoutLineItemsRemove)
|
return checkoutToCart(data.checkoutLinesUpdate)
|
||||||
},
|
},
|
||||||
useHook: ({
|
useHook: ({
|
||||||
fetch,
|
fetch,
|
||||||
|
@ -15,7 +15,7 @@ import { handler as removeItemHandler } from './use-remove-item'
|
|||||||
import type { Cart, LineItem, UpdateCartItemBody } from '../types'
|
import type { Cart, LineItem, UpdateCartItemBody } from '../types'
|
||||||
import { checkoutToCart } from '../utils'
|
import { checkoutToCart } from '../utils'
|
||||||
import { getCheckoutId, checkoutLineItemUpdateMutation } from '../utils'
|
import { getCheckoutId, checkoutLineItemUpdateMutation } from '../utils'
|
||||||
import { Mutation, MutationCheckoutLineItemsUpdateArgs } from '../schema'
|
import { Mutation, MutationCheckoutLinesUpdateArgs } from '../schema'
|
||||||
|
|
||||||
export type UpdateItemInput<T = any> = T extends LineItem
|
export type UpdateItemInput<T = any> = T extends LineItem
|
||||||
? Partial<UpdateItemInputBase<LineItem>>
|
? Partial<UpdateItemInputBase<LineItem>>
|
||||||
@ -46,23 +46,25 @@ export const handler = {
|
|||||||
message: 'The item quantity has to be a valid integer',
|
message: 'The item quantity has to be a valid integer',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const { checkoutLineItemsUpdate } = await fetch<
|
|
||||||
|
const checkoutId = getCheckoutId().checkoutId;
|
||||||
|
const { checkoutLinesUpdate } = await fetch<
|
||||||
Mutation,
|
Mutation,
|
||||||
MutationCheckoutLineItemsUpdateArgs
|
MutationCheckoutLinesUpdateArgs
|
||||||
>({
|
>({
|
||||||
...options,
|
...options,
|
||||||
variables: {
|
variables: {
|
||||||
checkoutId: getCheckoutId(),
|
checkoutId,
|
||||||
lineItems: [
|
lineItems: [
|
||||||
{
|
{
|
||||||
id: itemId,
|
variantId: item.variantId,
|
||||||
quantity: item.quantity,
|
quantity: item.quantity,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
return checkoutToCart(checkoutLineItemsUpdate)
|
return checkoutToCart(checkoutLinesUpdate)
|
||||||
},
|
},
|
||||||
useHook: ({
|
useHook: ({
|
||||||
fetch,
|
fetch,
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
import { checkoutDetailsFragment } from '../queries/get-checkout-query'
|
import { checkoutDetailsFragment } from '../queries/get-checkout-query'
|
||||||
|
|
||||||
const checkoutLineItemRemoveMutation = /* GraphQL */ `
|
const checkoutLineItemRemoveMutation = /* GraphQL */ `
|
||||||
mutation checkoutLineItemRemove($checkoutId: ID!, $lineItemIds: [ID!]!) {
|
mutation checkoutLineItemRemove($checkoutId: ID!, $lineId: ID!) {
|
||||||
checkoutLineItemsRemove(
|
checkoutLineDelete(
|
||||||
checkoutId: $checkoutId
|
checkoutId: $checkoutId
|
||||||
lineItemIds: $lineItemIds
|
lineId: $lineId
|
||||||
) {
|
) {
|
||||||
checkoutUserErrors {
|
errors {
|
||||||
code
|
code
|
||||||
field
|
field
|
||||||
message
|
message
|
||||||
}
|
}
|
||||||
# checkout {
|
checkout {
|
||||||
# ${checkoutDetailsFragment}
|
${checkoutDetailsFragment}
|
||||||
# }
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { checkoutDetailsFragment } from '../queries/get-checkout-query'
|
import { checkoutDetailsFragment } from '../queries/get-checkout-query'
|
||||||
|
|
||||||
const checkoutLineItemUpdateMutation = /* GraphQL */ `
|
const checkoutLineItemUpdateMutation = /* GraphQL */ `
|
||||||
mutation checkoutLineItemUpdate($checkoutId: ID!, $lineItems: [CheckoutLineItemUpdateInput!]!) {
|
mutation checkoutLineItemUpdate($checkoutId: ID!, $lineItems: [CheckoutLineInput!]!) {
|
||||||
checkoutLinesUpdate(checkoutId: $checkoutId, lineItems: $lineItems) {
|
checkoutLinesUpdate(checkoutId: $checkoutId, lines: $lineItems) {
|
||||||
errors {
|
errors {
|
||||||
code
|
code
|
||||||
field
|
field
|
||||||
|
Loading…
x
Reference in New Issue
Block a user