4
0
forked from crowetic/commerce

ensure products have at least one variant

This commit is contained in:
Greg Hoskin 2021-04-27 17:54:42 -05:00
parent a409c373c4
commit c6d06e60b6
6 changed files with 54 additions and 40 deletions

View File

@ -24,13 +24,23 @@ export const handler: MutationHook<Cart, {}, CartItemBody> = {
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>({
...options,
variables: {
checkoutId: getCheckoutId(),
product_id: item.productId,
quantity: item.quantity,
},
variables,
})
return checkoutToCart(response) as any

View File

@ -1,6 +1,7 @@
import { getConfig, SwellConfig } from '../api'
import { normalizeProduct } from '../utils/normalize'
import { Product } from '@commerce/types'
import { SwellProduct } from '../types'
type Variables = {
first?: number
@ -23,12 +24,10 @@ const getAllProducts = async (options: {
limit: variables.first,
},
])
const products = results.map((product) => {
if (product.variants) {
product.variants = product.variants.results
}
return normalizeProduct(product) ?? []
})
const products = results.map((product: SwellProduct) =>
normalizeProduct(product)
)
return {
products,
}

View File

@ -1,6 +1,6 @@
import { GraphQLFetcherResult } from '@commerce/api'
import { getConfig, SwellConfig } from '../api'
import { normalizeProduct, getProductQuery } from '../utils'
import { normalizeProduct } from '../utils'
type Variables = {
slug: string

View File

@ -5,6 +5,8 @@ import { normalizeProduct } from '../utils'
import { Product } from '@commerce/types'
import { SwellProduct } from '../types'
export default useSearch as UseSearch<typeof handler>
export type SearchProductsInput = {
@ -43,12 +45,9 @@ export const handler: SWRHook<
variables: { category: categoryId, search, sort: mappedSort },
})
const products = results.map((product) => {
if (product.variants) {
product.variants = product.variants?.results
}
return normalizeProduct(product)
})
const products = results.map((product: SwellProduct) =>
normalizeProduct(product)
)
return {
products,

View File

@ -962,28 +962,28 @@ export type CheckoutLineItemUpdateInput = {
export type CheckoutLineItemsAddPayload = {
__typename?: 'CheckoutLineItemsAddPayload'
/** The updated checkout object. */
checkout?: Maybe<Checkout>
items?: Maybe<Checkout>
/** List of errors that occurred executing the mutation. */
checkoutUserErrors: Array<CheckoutUserError>
/**
* List of errors that occurred executing the mutation.
* @deprecated Use `checkoutUserErrors` instead
*/
userErrors: Array<UserError>
// checkoutUserErrors: Array<CheckoutUserError>
// /**
// * List of errors that occurred executing the mutation.
// * @deprecated Use `checkoutUserErrors` instead
// */
// userErrors: Array<UserError>
}
/** Return type for `checkoutLineItemsRemove` mutation. */
export type CheckoutLineItemsRemovePayload = {
__typename?: 'CheckoutLineItemsRemovePayload'
/** The updated checkout object. */
checkout?: Maybe<Checkout>
items?: Maybe<Checkout>
/** List of errors that occurred executing the mutation. */
checkoutUserErrors: Array<CheckoutUserError>
/**
* List of errors that occurred executing the mutation.
* @deprecated Use `checkoutUserErrors` instead
*/
userErrors: Array<UserError>
// checkoutUserErrors: Array<CheckoutUserError>
// /**
// * List of errors that occurred executing the mutation.
// * @deprecated Use `checkoutUserErrors` instead
// */
// userErrors: Array<UserError>
}
/** Return type for `checkoutLineItemsReplace` mutation. */
@ -999,14 +999,14 @@ export type CheckoutLineItemsReplacePayload = {
export type CheckoutLineItemsUpdatePayload = {
__typename?: 'CheckoutLineItemsUpdatePayload'
/** The updated checkout object. */
checkout?: Maybe<Checkout>
items?: Maybe<Checkout>
/** List of errors that occurred executing the mutation. */
checkoutUserErrors: Array<CheckoutUserError>
/**
* List of errors that occurred executing the mutation.
* @deprecated Use `checkoutUserErrors` instead
*/
userErrors: Array<UserError>
// checkoutUserErrors: Array<CheckoutUserError>
// /**
// * List of errors that occurred executing the mutation.
// * @deprecated Use `checkoutUserErrors` instead
// */
// userErrors: Array<UserError>
}
/** Return type for `checkoutShippingAddressUpdate` mutation. */

View File

@ -110,6 +110,7 @@ const normalizeProductVariants = (
export function normalizeProduct(swellProduct: SwellProduct): Product {
const {
id,
name,
description,
images,
options,
@ -118,6 +119,8 @@ export function normalizeProduct(swellProduct: SwellProduct): Product {
price: value,
currency: currencyCode,
} = swellProduct
// ProductView accesses variants for each product
const emptyVariants = [{ options: [], id, name }]
const productOptions = options
? options.map((o) => normalizeProductOption(o))
: []
@ -133,7 +136,10 @@ export function normalizeProduct(swellProduct: SwellProduct): Product {
vendor: '',
path: `/${slug}`,
images: productImages,
variants: productVariants,
variants:
productVariants && productVariants.length
? productVariants
: emptyVariants,
options: productOptions,
price: {
value,