Allow setting a taxon id for getAllProducts

This commit is contained in:
tniezg
2021-09-13 15:04:32 +02:00
parent 6c3c94b9c0
commit 5b241d036b
4 changed files with 29 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ NEXT_PUBLIC_SPREE_IMAGE_HOST=http://localhost:4000
NEXT_PUBLIC_SPREE_ALLOWED_IMAGE_DOMAIN=localhost
NEXT_PUBLIC_SPREE_CATEGORIES_TAXONOMY_PERMALINK=categories
NEXT_PUBLIC_SPREE_BRANDS_TAXONOMY_PERMALINK=brands
NEXT_PUBLIC_SPREE_ALL_PRODUCTS_TAXONOMY_ID=false
NEXT_PUBLIC_SPREE_SHOW_SINGLE_VARIANT_OPTIONS=false
NEXT_PUBLIC_SPREE_LAST_UPDATED_PRODUCTS_PRERENDER_COUNT=10
NEXT_PUBLIC_SPREE_PRODUCT_PLACEHOLDER_IMAGE_URL=/product-img-placeholder.svg

View File

@@ -8,6 +8,7 @@ import type { IProducts } from '@spree/storefront-api-v2-sdk/types/interfaces/Pr
import type { SpreeApiConfig, SpreeApiProvider } from '../index'
import type { SpreeSdkVariables } from 'framework/spree/types'
import normalizeProduct from '../../utils/normalize-product'
import { requireConfigValue } from '@framework/isomorphic-config'
export default function getAllProductsOperation({
commerce,
@@ -41,7 +42,15 @@ export default function getAllProductsOperation({
userConfig
)
const defaultProductsTaxonomyId = requireConfigValue(
'allProductsTaxonomyId'
) as string | false
const first = getAllProductsVariables.first
const filter = !defaultProductsTaxonomyId
? {}
: { filter: { taxons: defaultProductsTaxonomyId } }
const variables: SpreeSdkVariables = {
methodPath: 'products.list',
arguments: [
@@ -50,6 +59,7 @@ export default function getAllProductsOperation({
include:
'primary_variant,variants,images,option_types,variants.option_values',
per_page: first,
...filter,
},
],
}

View File

@@ -1,5 +1,6 @@
import forceIsomorphicConfigValues from './utils/force-isomorphic-config-values'
import requireConfig from './utils/require-config'
import validateAllProductsTaxonomyId from './utils/validate-all-products-taxonomy-id'
import validateCookieExpire from './utils/validate-cookie-expire'
import validatePlaceholderImageUrl from './utils/validate-placeholder-image-url'
import validateProductsPrerenderCount from './utils/validate-products-prerender-count'
@@ -16,6 +17,9 @@ const isomorphicConfig = {
process.env.NEXT_PUBLIC_SPREE_CATEGORIES_TAXONOMY_PERMALINK,
brandsTaxonomyPermalink:
process.env.NEXT_PUBLIC_SPREE_BRANDS_TAXONOMY_PERMALINK,
allProductsTaxonomyId: validateAllProductsTaxonomyId(
process.env.NEXT_PUBLIC_SPREE_ALL_PRODUCTS_TAXONOMY_ID
),
showSingleVariantOptions:
process.env.NEXT_PUBLIC_SPREE_SHOW_SINGLE_VARIANT_OPTIONS === 'true',
lastUpdatedProductsPrerenderCount: validateProductsPrerenderCount(
@@ -40,6 +44,7 @@ export default forceIsomorphicConfigValues(
'imageHost',
'categoriesTaxonomyPermalink',
'brandsTaxonomyPermalink',
'allProductsTaxonomyId',
'showSingleVariantOptions',
'lastUpdatedProductsPrerenderCount',
'productPlaceholderImageUrl',

View File

@@ -0,0 +1,13 @@
const validateAllProductsTaxonomyId = (taxonomyId: unknown): string | false => {
if (!taxonomyId || taxonomyId === 'false') {
return false
}
if (typeof taxonomyId === 'string') {
return taxonomyId
}
throw new TypeError('taxonomyId must be a string or falsy.')
}
export default validateAllProductsTaxonomyId