From c6d06e60b6ba4b5745f98735fc302646f1617bb5 Mon Sep 17 00:00:00 2001 From: Greg Hoskin Date: Tue, 27 Apr 2021 17:54:42 -0500 Subject: [PATCH] ensure products have at least one variant --- framework/swell/cart/use-add-item.tsx | 20 +++++++--- framework/swell/product/get-all-products.ts | 11 +++--- framework/swell/product/get-product.ts | 2 +- framework/swell/product/use-search.tsx | 11 +++--- framework/swell/schema.d.ts | 42 ++++++++++----------- framework/swell/utils/normalize.ts | 8 +++- 6 files changed, 54 insertions(+), 40 deletions(-) diff --git a/framework/swell/cart/use-add-item.tsx b/framework/swell/cart/use-add-item.tsx index 346c6c955b..a86d2ab28d 100644 --- a/framework/swell/cart/use-add-item.tsx +++ b/framework/swell/cart/use-add-item.tsx @@ -24,13 +24,23 @@ export const handler: MutationHook = { 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({ ...options, - variables: { - checkoutId: getCheckoutId(), - product_id: item.productId, - quantity: item.quantity, - }, + variables, }) return checkoutToCart(response) as any diff --git a/framework/swell/product/get-all-products.ts b/framework/swell/product/get-all-products.ts index dff5bf0671..a129c18a2e 100644 --- a/framework/swell/product/get-all-products.ts +++ b/framework/swell/product/get-all-products.ts @@ -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, } diff --git a/framework/swell/product/get-product.ts b/framework/swell/product/get-product.ts index 1923fc75a3..8cd59f6e02 100644 --- a/framework/swell/product/get-product.ts +++ b/framework/swell/product/get-product.ts @@ -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 diff --git a/framework/swell/product/use-search.tsx b/framework/swell/product/use-search.tsx index b93a5598fa..ce116caa3d 100644 --- a/framework/swell/product/use-search.tsx +++ b/framework/swell/product/use-search.tsx @@ -5,6 +5,8 @@ import { normalizeProduct } from '../utils' import { Product } from '@commerce/types' +import { SwellProduct } from '../types' + export default useSearch as UseSearch 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, diff --git a/framework/swell/schema.d.ts b/framework/swell/schema.d.ts index c5309310d1..e77d3c8d9c 100644 --- a/framework/swell/schema.d.ts +++ b/framework/swell/schema.d.ts @@ -962,28 +962,28 @@ export type CheckoutLineItemUpdateInput = { export type CheckoutLineItemsAddPayload = { __typename?: 'CheckoutLineItemsAddPayload' /** The updated checkout object. */ - checkout?: Maybe + items?: Maybe /** List of errors that occurred executing the mutation. */ - checkoutUserErrors: Array - /** - * List of errors that occurred executing the mutation. - * @deprecated Use `checkoutUserErrors` instead - */ - userErrors: Array + // checkoutUserErrors: Array + // /** + // * List of errors that occurred executing the mutation. + // * @deprecated Use `checkoutUserErrors` instead + // */ + // userErrors: Array } /** Return type for `checkoutLineItemsRemove` mutation. */ export type CheckoutLineItemsRemovePayload = { __typename?: 'CheckoutLineItemsRemovePayload' /** The updated checkout object. */ - checkout?: Maybe + items?: Maybe /** List of errors that occurred executing the mutation. */ - checkoutUserErrors: Array - /** - * List of errors that occurred executing the mutation. - * @deprecated Use `checkoutUserErrors` instead - */ - userErrors: Array + // checkoutUserErrors: Array + // /** + // * List of errors that occurred executing the mutation. + // * @deprecated Use `checkoutUserErrors` instead + // */ + // userErrors: Array } /** Return type for `checkoutLineItemsReplace` mutation. */ @@ -999,14 +999,14 @@ export type CheckoutLineItemsReplacePayload = { export type CheckoutLineItemsUpdatePayload = { __typename?: 'CheckoutLineItemsUpdatePayload' /** The updated checkout object. */ - checkout?: Maybe + items?: Maybe /** List of errors that occurred executing the mutation. */ - checkoutUserErrors: Array - /** - * List of errors that occurred executing the mutation. - * @deprecated Use `checkoutUserErrors` instead - */ - userErrors: Array + // checkoutUserErrors: Array + // /** + // * List of errors that occurred executing the mutation. + // * @deprecated Use `checkoutUserErrors` instead + // */ + // userErrors: Array } /** Return type for `checkoutShippingAddressUpdate` mutation. */ diff --git a/framework/swell/utils/normalize.ts b/framework/swell/utils/normalize.ts index 9f69f702e6..57215d4857 100644 --- a/framework/swell/utils/normalize.ts +++ b/framework/swell/utils/normalize.ts @@ -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,