Skip to content

Commit

Permalink
ensure products have at least one variant
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Hoskin authored and Greg Hoskin committed Apr 28, 2021
1 parent a409c37 commit c6d06e6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 40 deletions.
20 changes: 15 additions & 5 deletions framework/swell/cart/use-add-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions framework/swell/product/get-all-products.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion framework/swell/product/get-product.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 5 additions & 6 deletions framework/swell/product/use-search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down
42 changes: 21 additions & 21 deletions framework/swell/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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. */
Expand Down
8 changes: 7 additions & 1 deletion framework/swell/utils/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const normalizeProductVariants = (
export function normalizeProduct(swellProduct: SwellProduct): Product {
const {
id,
name,
description,
images,
options,
Expand All @@ -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))
: []
Expand All @@ -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,
Expand Down

0 comments on commit c6d06e6

Please sign in to comment.