forked from crowetic/commerce
ensure products have at least one variant
This commit is contained in:
parent
a409c373c4
commit
c6d06e60b6
@ -24,13 +24,23 @@ export const handler: MutationHook<Cart, {}, CartItemBody> = {
|
|||||||
message: 'The item quantity has to be a valid integer greater than 0',
|
message: 'The item quantity has to be a valid integer greater than 0',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
const variables: {
|
||||||
|
product_id: string
|
||||||
|
variant_id?: string
|
||||||
|
checkoutId?: string
|
||||||
|
quantity?: number
|
||||||
|
} = {
|
||||||
|
checkoutId: getCheckoutId(),
|
||||||
|
product_id: item.productId,
|
||||||
|
quantity: item.quantity,
|
||||||
|
}
|
||||||
|
if (item.productId !== item.variantId) {
|
||||||
|
variables.variant_id = item.variantId
|
||||||
|
}
|
||||||
|
|
||||||
const response = await fetch<Mutation, MutationCheckoutLineItemsAddArgs>({
|
const response = await fetch<Mutation, MutationCheckoutLineItemsAddArgs>({
|
||||||
...options,
|
...options,
|
||||||
variables: {
|
variables,
|
||||||
checkoutId: getCheckoutId(),
|
|
||||||
product_id: item.productId,
|
|
||||||
quantity: item.quantity,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return checkoutToCart(response) as any
|
return checkoutToCart(response) as any
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { getConfig, SwellConfig } from '../api'
|
import { getConfig, SwellConfig } from '../api'
|
||||||
import { normalizeProduct } from '../utils/normalize'
|
import { normalizeProduct } from '../utils/normalize'
|
||||||
import { Product } from '@commerce/types'
|
import { Product } from '@commerce/types'
|
||||||
|
import { SwellProduct } from '../types'
|
||||||
|
|
||||||
type Variables = {
|
type Variables = {
|
||||||
first?: number
|
first?: number
|
||||||
@ -23,12 +24,10 @@ const getAllProducts = async (options: {
|
|||||||
limit: variables.first,
|
limit: variables.first,
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
const products = results.map((product) => {
|
const products = results.map((product: SwellProduct) =>
|
||||||
if (product.variants) {
|
normalizeProduct(product)
|
||||||
product.variants = product.variants.results
|
)
|
||||||
}
|
|
||||||
return normalizeProduct(product) ?? []
|
|
||||||
})
|
|
||||||
return {
|
return {
|
||||||
products,
|
products,
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { GraphQLFetcherResult } from '@commerce/api'
|
import { GraphQLFetcherResult } from '@commerce/api'
|
||||||
import { getConfig, SwellConfig } from '../api'
|
import { getConfig, SwellConfig } from '../api'
|
||||||
import { normalizeProduct, getProductQuery } from '../utils'
|
import { normalizeProduct } from '../utils'
|
||||||
|
|
||||||
type Variables = {
|
type Variables = {
|
||||||
slug: string
|
slug: string
|
||||||
|
@ -5,6 +5,8 @@ import { normalizeProduct } from '../utils'
|
|||||||
|
|
||||||
import { Product } from '@commerce/types'
|
import { Product } from '@commerce/types'
|
||||||
|
|
||||||
|
import { SwellProduct } from '../types'
|
||||||
|
|
||||||
export default useSearch as UseSearch<typeof handler>
|
export default useSearch as UseSearch<typeof handler>
|
||||||
|
|
||||||
export type SearchProductsInput = {
|
export type SearchProductsInput = {
|
||||||
@ -43,12 +45,9 @@ export const handler: SWRHook<
|
|||||||
variables: { category: categoryId, search, sort: mappedSort },
|
variables: { category: categoryId, search, sort: mappedSort },
|
||||||
})
|
})
|
||||||
|
|
||||||
const products = results.map((product) => {
|
const products = results.map((product: SwellProduct) =>
|
||||||
if (product.variants) {
|
normalizeProduct(product)
|
||||||
product.variants = product.variants?.results
|
)
|
||||||
}
|
|
||||||
return normalizeProduct(product)
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
products,
|
products,
|
||||||
|
42
framework/swell/schema.d.ts
vendored
42
framework/swell/schema.d.ts
vendored
@ -962,28 +962,28 @@ export type CheckoutLineItemUpdateInput = {
|
|||||||
export type CheckoutLineItemsAddPayload = {
|
export type CheckoutLineItemsAddPayload = {
|
||||||
__typename?: 'CheckoutLineItemsAddPayload'
|
__typename?: 'CheckoutLineItemsAddPayload'
|
||||||
/** The updated checkout object. */
|
/** The updated checkout object. */
|
||||||
checkout?: Maybe<Checkout>
|
items?: Maybe<Checkout>
|
||||||
/** List of errors that occurred executing the mutation. */
|
/** List of errors that occurred executing the mutation. */
|
||||||
checkoutUserErrors: Array<CheckoutUserError>
|
// checkoutUserErrors: Array<CheckoutUserError>
|
||||||
/**
|
// /**
|
||||||
* List of errors that occurred executing the mutation.
|
// * List of errors that occurred executing the mutation.
|
||||||
* @deprecated Use `checkoutUserErrors` instead
|
// * @deprecated Use `checkoutUserErrors` instead
|
||||||
*/
|
// */
|
||||||
userErrors: Array<UserError>
|
// userErrors: Array<UserError>
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return type for `checkoutLineItemsRemove` mutation. */
|
/** Return type for `checkoutLineItemsRemove` mutation. */
|
||||||
export type CheckoutLineItemsRemovePayload = {
|
export type CheckoutLineItemsRemovePayload = {
|
||||||
__typename?: 'CheckoutLineItemsRemovePayload'
|
__typename?: 'CheckoutLineItemsRemovePayload'
|
||||||
/** The updated checkout object. */
|
/** The updated checkout object. */
|
||||||
checkout?: Maybe<Checkout>
|
items?: Maybe<Checkout>
|
||||||
/** List of errors that occurred executing the mutation. */
|
/** List of errors that occurred executing the mutation. */
|
||||||
checkoutUserErrors: Array<CheckoutUserError>
|
// checkoutUserErrors: Array<CheckoutUserError>
|
||||||
/**
|
// /**
|
||||||
* List of errors that occurred executing the mutation.
|
// * List of errors that occurred executing the mutation.
|
||||||
* @deprecated Use `checkoutUserErrors` instead
|
// * @deprecated Use `checkoutUserErrors` instead
|
||||||
*/
|
// */
|
||||||
userErrors: Array<UserError>
|
// userErrors: Array<UserError>
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return type for `checkoutLineItemsReplace` mutation. */
|
/** Return type for `checkoutLineItemsReplace` mutation. */
|
||||||
@ -999,14 +999,14 @@ export type CheckoutLineItemsReplacePayload = {
|
|||||||
export type CheckoutLineItemsUpdatePayload = {
|
export type CheckoutLineItemsUpdatePayload = {
|
||||||
__typename?: 'CheckoutLineItemsUpdatePayload'
|
__typename?: 'CheckoutLineItemsUpdatePayload'
|
||||||
/** The updated checkout object. */
|
/** The updated checkout object. */
|
||||||
checkout?: Maybe<Checkout>
|
items?: Maybe<Checkout>
|
||||||
/** List of errors that occurred executing the mutation. */
|
/** List of errors that occurred executing the mutation. */
|
||||||
checkoutUserErrors: Array<CheckoutUserError>
|
// checkoutUserErrors: Array<CheckoutUserError>
|
||||||
/**
|
// /**
|
||||||
* List of errors that occurred executing the mutation.
|
// * List of errors that occurred executing the mutation.
|
||||||
* @deprecated Use `checkoutUserErrors` instead
|
// * @deprecated Use `checkoutUserErrors` instead
|
||||||
*/
|
// */
|
||||||
userErrors: Array<UserError>
|
// userErrors: Array<UserError>
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return type for `checkoutShippingAddressUpdate` mutation. */
|
/** Return type for `checkoutShippingAddressUpdate` mutation. */
|
||||||
|
@ -110,6 +110,7 @@ const normalizeProductVariants = (
|
|||||||
export function normalizeProduct(swellProduct: SwellProduct): Product {
|
export function normalizeProduct(swellProduct: SwellProduct): Product {
|
||||||
const {
|
const {
|
||||||
id,
|
id,
|
||||||
|
name,
|
||||||
description,
|
description,
|
||||||
images,
|
images,
|
||||||
options,
|
options,
|
||||||
@ -118,6 +119,8 @@ export function normalizeProduct(swellProduct: SwellProduct): Product {
|
|||||||
price: value,
|
price: value,
|
||||||
currency: currencyCode,
|
currency: currencyCode,
|
||||||
} = swellProduct
|
} = swellProduct
|
||||||
|
// ProductView accesses variants for each product
|
||||||
|
const emptyVariants = [{ options: [], id, name }]
|
||||||
const productOptions = options
|
const productOptions = options
|
||||||
? options.map((o) => normalizeProductOption(o))
|
? options.map((o) => normalizeProductOption(o))
|
||||||
: []
|
: []
|
||||||
@ -133,7 +136,10 @@ export function normalizeProduct(swellProduct: SwellProduct): Product {
|
|||||||
vendor: '',
|
vendor: '',
|
||||||
path: `/${slug}`,
|
path: `/${slug}`,
|
||||||
images: productImages,
|
images: productImages,
|
||||||
variants: productVariants,
|
variants:
|
||||||
|
productVariants && productVariants.length
|
||||||
|
? productVariants
|
||||||
|
: emptyVariants,
|
||||||
options: productOptions,
|
options: productOptions,
|
||||||
price: {
|
price: {
|
||||||
value,
|
value,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user