mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 06:56:59 +00:00
add update-item hook
This commit is contained in:
parent
59d191cc8c
commit
ff41cf0e8f
@ -1,20 +1,79 @@
|
|||||||
import { MutationHook } from '@vercel/commerce/utils/types'
|
import {
|
||||||
|
HookFetcherContext,
|
||||||
|
MutationHook,
|
||||||
|
MutationHookContext,
|
||||||
|
} from '@vercel/commerce/utils/types'
|
||||||
import useUpdateItem, {
|
import useUpdateItem, {
|
||||||
UseUpdateItem,
|
UseUpdateItem,
|
||||||
} from '@vercel/commerce/cart/use-update-item'
|
} from '@vercel/commerce/cart/use-update-item'
|
||||||
|
import { getCartToken } from '../utils/token/cart-token'
|
||||||
|
import { useCallback } from 'react'
|
||||||
|
import useCart from './use-cart'
|
||||||
|
import { LineItem, UpdateItemHook } from '@vercel/commerce/types/cart'
|
||||||
|
import { ValidationError } from '@vercel/commerce/utils/errors'
|
||||||
|
import { normalizeCart } from '../utils/normalize/normalize-cart'
|
||||||
|
|
||||||
export default useUpdateItem as UseUpdateItem<any>
|
export default useUpdateItem as UseUpdateItem<any>
|
||||||
|
|
||||||
export const handler: MutationHook<any> = {
|
export const handler: MutationHook<any> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
query: '',
|
url: '/api/v2/shop/orders',
|
||||||
|
method: 'PATCH',
|
||||||
|
},
|
||||||
|
fetcher: async ({
|
||||||
|
input: { itemId, item },
|
||||||
|
options,
|
||||||
|
fetch,
|
||||||
|
}: HookFetcherContext<UpdateItemHook>) => {
|
||||||
|
const syliusCart = await fetch({
|
||||||
|
url: `${options.url}/${getCartToken()}/items/${itemId}`,
|
||||||
|
method: options.method,
|
||||||
|
body: {
|
||||||
|
quantity: item.quantity,
|
||||||
|
},
|
||||||
|
variables: {
|
||||||
|
contentType: 'application/merge-patch+json',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return normalizeCart(syliusCart)
|
||||||
},
|
},
|
||||||
async fetcher({ input, options, fetch }) {},
|
|
||||||
useHook:
|
useHook:
|
||||||
({ fetch }) =>
|
({ fetch }: MutationHookContext<UpdateItemHook>) =>
|
||||||
() => {
|
(
|
||||||
return async function addItem() {
|
ctx: {
|
||||||
return {}
|
item?: LineItem
|
||||||
}
|
wait?: number
|
||||||
|
} = {}
|
||||||
|
) => {
|
||||||
|
const { item } = ctx
|
||||||
|
const { mutate, data: cartData } = useCart() as any
|
||||||
|
|
||||||
|
return useCallback(
|
||||||
|
async function addItem(input) {
|
||||||
|
const firstLineItem = cartData.lineItems[0]
|
||||||
|
const itemId = item?.id || firstLineItem.id
|
||||||
|
const productId = item?.productId || firstLineItem.productId
|
||||||
|
const variantId = item?.variant.id || firstLineItem.variant.id
|
||||||
|
if (!itemId) {
|
||||||
|
throw new ValidationError({
|
||||||
|
message: 'Invalid input used for this operation',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
const data = await fetch({
|
||||||
|
input: {
|
||||||
|
item: {
|
||||||
|
productId,
|
||||||
|
variantId,
|
||||||
|
quantity: input.quantity,
|
||||||
|
},
|
||||||
|
itemId,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
console.log('update quantity', data)
|
||||||
|
await mutate(data, false)
|
||||||
|
return data
|
||||||
|
},
|
||||||
|
[fetch, mutate]
|
||||||
|
)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -9,13 +9,14 @@ const fetcher: Fetcher = async ({
|
|||||||
body,
|
body,
|
||||||
variables = {
|
variables = {
|
||||||
useToken: true,
|
useToken: true,
|
||||||
|
contentType: 'application/json',
|
||||||
},
|
},
|
||||||
}) => {
|
}) => {
|
||||||
const token = getCustomerToken()
|
const token = getCustomerToken()
|
||||||
const res = await fetch(API_URL + url!, {
|
const res = await fetch(API_URL + url!, {
|
||||||
method: method,
|
method: method,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': variables.contentType,
|
||||||
accept: 'application/json, text/plain',
|
accept: 'application/json, text/plain',
|
||||||
...(token && variables.useToken
|
...(token && variables.useToken
|
||||||
? { Authorization: `Bearer ${token}` }
|
? { Authorization: `Bearer ${token}` }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user