forked from crowetic/commerce
Fix Commerce.js product normalizer (#877)
* Fix normalizer, since assets is optional now. * Fix missing categories & options * Update cart normalizer
This commit is contained in:
parent
190420a1fb
commit
90aa798891
@ -27,7 +27,7 @@ export default function getSiteInfoOperation({
|
|||||||
const { sdkFetch } = commerce.getConfig(config)
|
const { sdkFetch } = commerce.getConfig(config)
|
||||||
const { data: categories } = await sdkFetch('categories', 'list')
|
const { data: categories } = await sdkFetch('categories', 'list')
|
||||||
|
|
||||||
const formattedCategories = categories.map(normalizeCategory)
|
const formattedCategories = categories?.map(normalizeCategory) ?? []
|
||||||
|
|
||||||
return {
|
return {
|
||||||
categories: formattedCategories,
|
categories: formattedCategories,
|
||||||
|
@ -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 })
|
||||||
|
@ -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,
|
||||||
|
@ -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 ?? '',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -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,
|
||||||
|
@ -54,6 +54,7 @@ export function normalizeProduct(
|
|||||||
): Product {
|
): Product {
|
||||||
const { id, name, description, permalink, assets, price, variant_groups } =
|
const { id, name, description, permalink, assets, price, variant_groups } =
|
||||||
commercejsProduct
|
commercejsProduct
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
@ -61,15 +62,19 @@ export function normalizeProduct(
|
|||||||
descriptionHtml: description,
|
descriptionHtml: description,
|
||||||
slug: permalink,
|
slug: permalink,
|
||||||
path: `/${permalink}`,
|
path: `/${permalink}`,
|
||||||
images: assets.map(({ url, description, filename }) => ({
|
images:
|
||||||
url,
|
assets?.map(({ url, description, filename }) => ({
|
||||||
alt: description || filename,
|
url,
|
||||||
})),
|
alt: description || filename,
|
||||||
|
})) || [],
|
||||||
price: {
|
price: {
|
||||||
value: price.raw,
|
value: price.raw,
|
||||||
currencyCode: 'USD',
|
currencyCode: 'USD',
|
||||||
},
|
},
|
||||||
variants: normalizeVariants(commercejsProductVariants, variant_groups),
|
variants: normalizeVariants(
|
||||||
options: getOptionsFromVariantGroups(variant_groups),
|
commercejsProductVariants,
|
||||||
|
variant_groups || []
|
||||||
|
),
|
||||||
|
options: variant_groups ? getOptionsFromVariantGroups(variant_groups) : [],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user