Skip to content

Commit

Permalink
fix: Reset infinite scroll if search's total pages is less than curre…
Browse files Browse the repository at this point in the history
…nt state's total pages (#2610)

## What's the purpose of this pull request?

This PR intends to fix the infinite scroll behavior when a search's
total pages is different from the current state's total pages.

Currently, if the current state's total pages are 3, for example, and a
product search returns results with only 1 page the previous total pages
will be considered, rendering skeletons and pages wrong.


![image](https://github.com/user-attachments/assets/d13e539d-8a7a-4f1f-ade0-3abb6566030e)

## How it works?

It will reset the infinite scroll (always to the first page) if after a
product search the total pages number is less than the previous total
pages number.

## How to test it?

Navigate to the `Office` PLP, load more than 2 pages then search for
`apple` (in the search input). No skeletons or more than 1 page should
be rendered.

### Starters Deploy Preview

vtex-sites/starter.store#644

---------

Co-authored-by: Lucas Feijó <lucas.portela@vtex.com>
  • Loading branch information
pedromtec and lucasfp13 authored Jan 9, 2025
1 parent 180554c commit 7faedd2
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export default function SearchWrapper({
const router = useRouter()
const {
state: { term, sort, selectedFacets },
pages,
resetInfiniteScroll,
} = useSearch()

const { data: pageProductGalleryData, isValidating } = useProductGalleryQuery(
Expand All @@ -62,6 +64,17 @@ export default function SearchWrapper({
return <EmptySearch />
}

const productGalleryProducts = pageProductGalleryData?.search?.products
const stateTotalPages = pages.length
const searchTotalPages = Math.ceil(
productGalleryProducts?.pageInfo?.totalCount / itemsPerPage
)

// if the total pages is less than the current state total pages, reset the infinite scroll
if (searchTotalPages > 0 && searchTotalPages < stateTotalPages) {
resetInfiniteScroll(0)
}

return (
<SearchPage
page={searchContentType}
Expand Down

0 comments on commit 7faedd2

Please sign in to comment.