Skip to content

Commit

Permalink
Merge pull request #646 from vtex-apps/feat/fetch-ads-if-in-settings
Browse files Browse the repository at this point in the history
Feature: use settings to determine whether or not to fetch ads
  • Loading branch information
Henrique Caúla authored Nov 28, 2023
2 parents e974b07 + 5558b04 commit 7cab124
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions react/components/SearchQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -209,7 +211,7 @@ const useQueries = (
error: sponsoredProductsError,
} = useQuery(sponsoredProductsQuery, {
variables,
skip: sponsoredProductsBehavior === 'skip',
skip: shouldSkipSponsoredProducts(sponsoredProductsBehavior, settings),
})

const sponsoredProductsReturn = sponsoredProductsResult(
Expand Down
23 changes: 23 additions & 0 deletions react/utils/shouldSkipSponsoredProducts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type SponsoredProductsBehavior = 'skip' | string

type Settings = {
fetchSponsoredProductsOnSearch: boolean
} & Record<string, unknown>

/**
* 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

0 comments on commit 7cab124

Please sign in to comment.