Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: use settings to determine whether or not to fetch ads #646

Merged
merged 7 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@ -125,7 +126,7 @@

const isCurrentDifferent = (ref, currentVal) => ref.current !== currentVal

const useShouldResetPage = (query, map, orderBy, priceRange) => {

Check warning on line 129 in react/components/SearchQuery.js

View workflow job for this annotation

GitHub Actions / Lint

Arrow function has too many parameters (4). Maximum allowed is 3
const queryRef = useRef(query)
const mapRef = useRef(map)
const orderByRef = useRef(orderBy)
Expand Down Expand Up @@ -154,7 +155,7 @@
operator,
searchState,
fullText
) => {

Check warning on line 158 in react/components/SearchQuery.js

View workflow job for this annotation

GitHub Actions / Lint

Arrow function has too many parameters (4). Maximum allowed is 3
const fullTextRef = useRef(fullText)
const shouldReset = isCurrentDifferent(fullTextRef, fullText)

Expand Down Expand Up @@ -189,10 +190,11 @@
facetsArgs,
price,
sponsoredProductsBehavior = 'skip'
) => {

Check warning on line 193 in react/components/SearchQuery.js

View workflow job for this annotation

GitHub Actions / Lint

Arrow function has too many parameters (4). Maximum allowed is 3
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 @@
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
Loading