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

temporarily removes the searchResult query cache #484

Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Fixed
- Temporarily removes the `searchResult` query cache.

## [3.92.1] - 2021-01-15
### Fixed
- Updated the app documentation (readme.md file) with links to the new recipe on building a search results page with multiple layouts.
Expand Down
38 changes: 25 additions & 13 deletions react/components/SearchQuery.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useMemo, useRef, useCallback, useEffect, useState } from 'react'
import { useQuery } from 'react-apollo'
import { path } from 'ramda'
import { useRuntime } from 'vtex.render-runtime'

import productSearchQuery from 'vtex.store-resources/QueryProductSearchV3'
import searchMetadataQuery from 'vtex.store-resources/QuerySearchMetadataV2'
import facetsQuery from 'vtex.store-resources/QueryFacetsV2'
Expand Down Expand Up @@ -34,8 +32,9 @@ const includeFacets = (map, query) =>

const useCombinedRefetch = (productRefetch, facetsRefetch) => {
return useCallback(
async refetchVariables => {
let productVariables, facetsVariables
async (refetchVariables) => {
let productVariables
let facetsVariables

if (refetchVariables) {
const {
Expand Down Expand Up @@ -88,6 +87,7 @@ const useCombinedRefetch = (productRefetch, facetsRefetch) => {
productRefetch && productRefetch(productVariables),
facetsRefetch && facetsRefetch(facetsVariables),
])

return {
...searchRefetchResult,
data: {
Expand Down Expand Up @@ -121,6 +121,7 @@ const useCorrectPage = (page, shouldReset) => {
if (shouldReset) {
pageRef.current = DEFAULT_PAGE
}

return pageRef.current
}

Expand Down Expand Up @@ -148,12 +149,18 @@ const useQueries = (variables, facetsArgs) => {
const { getSettings } = useRuntime()
const isLazyFacetsFetchEnabled = getSettings('vtex.store')
?.enableFiltersFetchOptimization
const productSearchResult = useQuery(productSearchQuery, { variables })

const productSearchResult = useQuery(productSearchQuery, {
variables,
fetchPolicy: 'network-only',
})

const {
refetch: searchRefetch,
loading: searchLoading,
fetchMore,
} = productSearchResult

const { data: { searchMetadata } = {} } = useQuery(searchMetadataQuery, {
variables: {
query: variables.query,
Expand Down Expand Up @@ -197,15 +204,18 @@ const useQueries = (variables, facetsArgs) => {
priceRanges: [],
}

const selectedFacetsOutput = path(['queryArgs', 'selectedFacets'], facets)
const selectedFacetsOutput =
facets && facets.queryArgs && facets.queryArgs.selectedFacets

const queryArgs =
selectedFacetsOutput &&
buildQueryArgsFromSelectedFacets(selectedFacetsOutput)

const redirect = path(
['data', 'productSearch', 'redirect'],
productSearchResult
)
const redirect =
productSearchResult &&
productSearchResult.data &&
productSearchResult.data.productSearch &&
productSearchResult.data.productSearch.redirect

return {
loading: searchLoading || redirect,
Expand Down Expand Up @@ -270,18 +280,20 @@ const SearchQuery = ({
/* This is the page of the first query since the component was rendered.
We want this behaviour so we can show the correct items even if the pageQuery
changes. It should change only on a new render or if the query or orderby
change, hence the useCorrectPage that updates its value*/
change, hence the useCorrectPage that updates its value */
const shouldReset = useShouldResetPage(query, map, orderBy)
const page = useCorrectPage(
pageQuery ? parseInt(pageQuery) : DEFAULT_PAGE,
pageQuery ? parseInt(pageQuery, 10) : DEFAULT_PAGE,
shouldReset
)

const { fuzzy, operator, searchState } = useCorrectSearchStateVariables(
fuzzyQuery,
operatorQuery,
searchStateQuery,
fullText
)

const { setRedirect } = useRedirect()

const from = (page - 1) * maxItemsPerPage
Expand Down Expand Up @@ -347,7 +359,7 @@ const SearchQuery = ({
fetchMore,
} = useQueries(variables, facetsArgs)

const redirectUrl = path(['productSearch', 'redirect'], data)
const redirectUrl = data && data.productSearch && data.productSearch.redirect

useEffect(() => {
setRedirect(redirectUrl)
Expand Down