diff --git a/CHANGELOG.md b/CHANGELOG.md index e03758641..c7a3a5725 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +- Use the advanced setting `fetchSponsoredProductsOnSearch` to determine whether or not to fetch sponsored products. + ## [3.127.1] - 2023-10-24 ### Changed diff --git a/react/components/SearchQuery.js b/react/components/SearchQuery.js index 06eb57aae..22ac2834f 100644 --- a/react/components/SearchQuery.js +++ b/react/components/SearchQuery.js @@ -13,6 +13,7 @@ import { detachFiltersByType, buildQueryArgsFromSelectedFacets, } from '../utils/compatibilityLayer' +import shouldSkipSponsoredProducts from '../utils/shouldSkipSponsoredProducts' import { FACETS_RENDER_THRESHOLD } from '../constants/filterConstants' import useRedirect from '../hooks/useRedirect' import useSession from '../hooks/useSession' @@ -191,8 +192,9 @@ const useQueries = ( sponsoredProductsBehavior = 'skip' ) => { const { getSettings, query: runtimeQuery } = useRuntime() - const isLazyFacetsFetchEnabled = - getSettings('vtex.store')?.enableFiltersFetchOptimization + const settings = getSettings('vtex.store') + + const isLazyFacetsFetchEnabled = settings?.enableFiltersFetchOptimization const productSearchResult = useQuery(productSearchQuery, { ssr: false, @@ -209,7 +211,7 @@ const useQueries = ( error: sponsoredProductsError, } = useQuery(sponsoredProductsQuery, { variables, - skip: sponsoredProductsBehavior === 'skip', + skip: shouldSkipSponsoredProducts(sponsoredProductsBehavior, settings), }) const sponsoredProductsReturn = sponsoredProductsResult( diff --git a/react/utils/shouldSkipSponsoredProducts.ts b/react/utils/shouldSkipSponsoredProducts.ts new file mode 100644 index 000000000..5f496ed70 --- /dev/null +++ b/react/utils/shouldSkipSponsoredProducts.ts @@ -0,0 +1,23 @@ +type SponsoredProductsBehavior = 'skip' | string + +type Settings = { + fetchSponsoredProductsOnSearch: boolean +} & Record + +/** + * This function checks the store's settings, as well as the passed value of + * `sponsoredProductsBehavior` passed in the store-theme, to determine if the + * sponsored products request should be skipped or not. + * Some accounts may not have configured the store's settings, so we need to check if the + * `sponsoredProductsBehavior` parameter for compatibility. + */ +const shouldSkipSponsoredProducts = ( + sponsoredProductsBehavior: SponsoredProductsBehavior, + settings: Settings +) => { + const fetchSponsoredProductsConfig = settings?.fetchSponsoredProductsOnSearch + + return !fetchSponsoredProductsConfig && sponsoredProductsBehavior === 'skip' +} + +export default shouldSkipSponsoredProducts