Skip to content

Commit 248914c

Browse files
authored
Merge pull request #1131 from topcoder-platform/pm-1448_in-scroll
fix(PM-1448): implemented client side infinite scroll
2 parents 40adb59 + 75aa8cb commit 248914c

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/apps/talent-search/src/routes/search-results-page/SearchResultsPage.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, useCallback, useState } from 'react'
1+
import { FC, useCallback, useEffect, useState } from 'react'
22
import classNames from 'classnames'
33

44
import { Button, ContentLayout, LinkButton, LoadingCircles } from '~/libs/ui'
@@ -17,6 +17,9 @@ import styles from './SearchResultsPage.module.scss'
1717
const SearchResultsPage: FC = () => {
1818
const [showSkillsModal, setShowSkillsModal] = useState(false)
1919

20+
const [currentPage, setCurrentPage] = useState(1)
21+
const itemsPerPage = 10
22+
2023
const [skills, setSkills] = useUrlQuerySearchParms('q')
2124
const {
2225
loading,
@@ -25,6 +28,27 @@ const SearchResultsPage: FC = () => {
2528
hasNext,
2629
total,
2730
}: InfiniteTalentMatchesResposne = useInfiniteTalentMatches(skills)
31+
const paginatedMatches = matches.slice(0, currentPage * itemsPerPage)
32+
33+
useEffect(() => {
34+
const handleScroll: () => void = () => {
35+
const scrollY = window.scrollY
36+
const visibleHeight = window.innerHeight
37+
const fullHeight = document.body.scrollHeight
38+
const footerElem = document.getElementById('footer-nav-el')
39+
const footerHeight = (footerElem && footerElem.offsetHeight) || 650
40+
if (scrollY + visibleHeight >= fullHeight - (footerHeight + 100)) {
41+
// Scroll near bottom
42+
setCurrentPage(prev => {
43+
const maxPages = Math.ceil(matches.length / itemsPerPage)
44+
return prev < maxPages ? prev + 1 : prev
45+
})
46+
}
47+
}
48+
49+
window.addEventListener('scroll', handleScroll)
50+
return () => window.removeEventListener('scroll', handleScroll)
51+
}, [matches])
2852

2953
const toggleSkillsModal = useCallback(() => setShowSkillsModal(s => !s), [])
3054

@@ -100,7 +124,7 @@ const SearchResultsPage: FC = () => {
100124
)}
101125
</div>
102126
<div className={styles.resultsWrap}>
103-
{matches.map(member => (
127+
{paginatedMatches.map(member => (
104128
<TalentCard
105129
queriedSkills={skills}
106130
member={member}

0 commit comments

Comments
 (0)