Update cart normalizer

This commit is contained in:
Catalin Pinte 2022-11-15 17:27:20 +02:00
parent 05c537e418
commit 39ba0ab33d
4 changed files with 10 additions and 8 deletions

View File

@ -22,17 +22,17 @@ export const handler: MutationHook<AddItemHook> = {
variables.push(item.variantId) variables.push(item.variantId)
} }
const { cart } = await fetch<{ cart: CommercejsCart }>({ const cart = await fetch<CommercejsCart>({
query: options.query, query: options.query,
method: options.method, method: options.method,
variables, variables,
}) })
return normalizeCart(cart) return normalizeCart(cart)
}, },
useHook: ({ fetch }) => useHook: ({ fetch }) =>
function useHook() { function useHook() {
const { mutate } = useCart() const { mutate } = useCart()
return useCallback( return useCallback(
async function addItem(input) { async function addItem(input) {
const cart = await fetch({ input }) const cart = await fetch({ input })

View File

@ -16,7 +16,7 @@ export const handler: MutationHook<RemoveItemHook> = {
method: 'remove', method: 'remove',
}, },
async fetcher({ input, options, fetch }) { async fetcher({ input, options, fetch }) {
const { cart } = await fetch<{ cart: CommercejsCart }>({ const cart = await fetch<CommercejsCart>({
query: options.query, query: options.query,
method: options.method, method: options.method,
variables: input.itemId, variables: input.itemId,

View File

@ -30,7 +30,7 @@ export const handler = {
}, },
async fetcher({ input, options, fetch }: HookFetcherContext<UpdateItemHook>) { async fetcher({ input, options, fetch }: HookFetcherContext<UpdateItemHook>) {
const variables = [input.itemId, { quantity: input.item.quantity }] const variables = [input.itemId, { quantity: input.item.quantity }]
const { cart } = await fetch<{ cart: CommercejsCart }>({ const cart = await fetch<CommercejsCart>({
query: options.query, query: options.query,
method: options.method, method: options.method,
variables, variables,
@ -57,7 +57,7 @@ export const handler = {
const variantId = input.productId ?? item?.variantId const variantId = input.productId ?? item?.variantId
const quantity = input?.quantity ?? item?.quantity const quantity = input?.quantity ?? item?.quantity
if (!itemId || !productId || !variantId) { if (!itemId || !productId) {
throw new ValidationError({ throw new ValidationError({
message: 'Invalid input for updating cart item', message: 'Invalid input for updating cart item',
}) })
@ -69,7 +69,7 @@ export const handler = {
item: { item: {
quantity, quantity,
productId, productId,
variantId, variantId: variantId ?? '',
}, },
}, },
}) })

View File

@ -44,14 +44,16 @@ const normalizeLineItem = (
} }
} }
export const normalizeCart = (commercejsCart: CommercejsCart): Cart => { export const normalizeCart = (
commercejsCart: CommercejsCart | { cart: CommercejsCart }
): Cart => {
const { const {
id, id,
created, created,
subtotal: { raw: rawPrice }, subtotal: { raw: rawPrice },
currency, currency,
line_items, line_items,
} = commercejsCart } = 'cart' in commercejsCart ? commercejsCart.cart : commercejsCart
return { return {
id, id,