Skip to content

Commit 5558b04

Browse files
author
Henrique Caúla
committed
feat: extract skipSponsoredProducts function
1 parent 1b126d9 commit 5558b04

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

react/components/SearchQuery.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
detachFiltersByType,
1414
buildQueryArgsFromSelectedFacets,
1515
} from '../utils/compatibilityLayer'
16+
import shouldSkipSponsoredProducts from '../utils/shouldSkipSponsoredProducts'
1617
import { FACETS_RENDER_THRESHOLD } from '../constants/filterConstants'
1718
import useRedirect from '../hooks/useRedirect'
1819
import useSession from '../hooks/useSession'
@@ -169,12 +170,6 @@ const useCorrectSearchStateVariables = (
169170
return result
170171
}
171172

172-
const skipSponsoredProducts = ({ sponsoredProductsBehavior, settings }) => {
173-
const fetchSponsoredProductsConfig = settings?.fetchSponsoredProductsOnSearch
174-
175-
return !fetchSponsoredProductsConfig && sponsoredProductsBehavior === 'skip'
176-
}
177-
178173
const sponsoredProductsResult = (
179174
sponsoredProductsLoading,
180175
sponsoredProductsError,
@@ -216,7 +211,7 @@ const useQueries = (
216211
error: sponsoredProductsError,
217212
} = useQuery(sponsoredProductsQuery, {
218213
variables,
219-
skip: skipSponsoredProducts({ sponsoredProductsBehavior, settings }),
214+
skip: shouldSkipSponsoredProducts(sponsoredProductsBehavior, settings),
220215
})
221216

222217
const sponsoredProductsReturn = sponsoredProductsResult(
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
type SponsoredProductsBehavior = 'skip' | string
2+
3+
type Settings = {
4+
fetchSponsoredProductsOnSearch: boolean
5+
} & Record<string, unknown>
6+
7+
/**
8+
* This function checks the store's settings, as well as the passed value of
9+
* `sponsoredProductsBehavior` passed in the store-theme, to determine if the
10+
* sponsored products request should be skipped or not.
11+
* Some accounts may not have configured the store's settings, so we need to check if the
12+
* `sponsoredProductsBehavior` parameter for compatibility.
13+
*/
14+
const shouldSkipSponsoredProducts = (
15+
sponsoredProductsBehavior: SponsoredProductsBehavior,
16+
settings: Settings
17+
) => {
18+
const fetchSponsoredProductsConfig = settings?.fetchSponsoredProductsOnSearch
19+
20+
return !fetchSponsoredProductsConfig && sponsoredProductsBehavior === 'skip'
21+
}
22+
23+
export default shouldSkipSponsoredProducts

0 commit comments

Comments
 (0)