From 5b241d036bac0eec6bc898cfd3376ebd97cdebee Mon Sep 17 00:00:00 2001 From: tniezg Date: Mon, 13 Sep 2021 15:04:32 +0200 Subject: [PATCH] Allow setting a taxon id for getAllProducts --- framework/spree/.env.template | 1 + framework/spree/api/operations/get-all-products.ts | 10 ++++++++++ framework/spree/isomorphic-config.ts | 5 +++++ .../utils/validate-all-products-taxonomy-id.ts | 13 +++++++++++++ 4 files changed, 29 insertions(+) create mode 100644 framework/spree/utils/validate-all-products-taxonomy-id.ts diff --git a/framework/spree/.env.template b/framework/spree/.env.template index 4f73b3898..5c4f05184 100644 --- a/framework/spree/.env.template +++ b/framework/spree/.env.template @@ -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 diff --git a/framework/spree/api/operations/get-all-products.ts b/framework/spree/api/operations/get-all-products.ts index 833ff70f3..ca6023dc3 100644 --- a/framework/spree/api/operations/get-all-products.ts +++ b/framework/spree/api/operations/get-all-products.ts @@ -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, }, ], } diff --git a/framework/spree/isomorphic-config.ts b/framework/spree/isomorphic-config.ts index 1fb5f3485..ed1bb4a55 100644 --- a/framework/spree/isomorphic-config.ts +++ b/framework/spree/isomorphic-config.ts @@ -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', diff --git a/framework/spree/utils/validate-all-products-taxonomy-id.ts b/framework/spree/utils/validate-all-products-taxonomy-id.ts new file mode 100644 index 000000000..5eaaa0b4b --- /dev/null +++ b/framework/spree/utils/validate-all-products-taxonomy-id.ts @@ -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