From 803e9d4014c8d2281accfaddd6b1357e012b4489 Mon Sep 17 00:00:00 2001 From: Nuwan Chathuranga Date: Fri, 8 Oct 2021 16:29:39 +0530 Subject: [PATCH] add infinite scroll to people list --- jam-ui/src/components/page/JKPeople.js | 91 ++++++++++++---------- jam-ui/src/components/page/JKPeopleList.js | 6 +- jam-ui/src/store/features/peopleSlice.js | 2 +- 3 files changed, 53 insertions(+), 46 deletions(-) diff --git a/jam-ui/src/components/page/JKPeople.js b/jam-ui/src/components/page/JKPeople.js index e1e33c0bf..35381a332 100644 --- a/jam-ui/src/components/page/JKPeople.js +++ b/jam-ui/src/components/page/JKPeople.js @@ -1,6 +1,6 @@ -import React, { useState, useEffect, useRef, Fragment } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import PropTypes from 'prop-types'; -import { Alert, Card, CardBody, Col, Row, Button, Pagination, PaginationItem, PaginationLink, Form } from 'reactstrap'; +import { Alert, Card, CardBody, Col, Row, Button, Form } from 'reactstrap'; import Loader from '../common/Loader'; import FalconCardHeader from '../common/FalconCardHeader'; import { isIterableArray } from '../../helpers/utils'; @@ -10,56 +10,44 @@ import { fetchPeople } from '../../store/features/peopleSlice'; import JKPeopleSearch from './JKPeopleSearch'; import JKPeopleList from './JKPeopleList'; -// import { getPeople } from '../../helpers/rest'; import JKPeopleSwiper from './JKPeopleSwiper'; const JKPeople = ({ className }) => { - - //const [people, setPeople] = useState([]); - //const [loading, setLoading] = useState(true); const [showSearch, setShowSearch] = useState(false); const [page, setPage] = useState(1); - //const [totalPages, setTotalPages] = useState(0); - const peopleListRef = useRef(); + const dispatch = useDispatch(); - const dispatch = useDispatch() + const people = useSelector(state => state.people.people); + const totalPages = useSelector(state => state.people.totalPages); + const loadingStatus = useSelector(state => state.people.status); - const people = useSelector(state => state.people.people) - const totalPages = useSelector(state => state.people.totalPages) - const loadingStatus = useSelector(state => state.people.status) + const loadPeople = React.useCallback( + () => { + if(totalPages !== 0 && page > totalPages){ + setPage(totalPages + 1); + return; + } + try { + dispatch(fetchPeople({ page })); + } catch (error) { + console.log('Error fetching people', error); + } + }, + [page] + ); - const loadPeople = React.useCallback(page => { - try { - dispatch(fetchPeople({page})) - } catch (error) { - console.log('Error fetching people', error); - } - + useEffect(() => { + loadPeople(); }, [page]); useEffect(() => { - loadPeople(page); - }, [page]); - - useEffect(() => { - if(loadingStatus === 'succeeded' && peopleListRef.current && page !== 1){ - // peopleListRef.current.scrollIntoView( - // { - // behavior: 'smooth', - // block: 'end', - // inline: 'nearest' - // }) - + if (loadingStatus === 'succeeded' && peopleListRef.current && page !== 1) { } - - }, [loadingStatus]) + }, [loadingStatus]); - const goNextPage = (event) => { - event.preventDefault() - if (page < totalPages) { - setPage(val => ++val); - } + const goNextPage = () => { + setPage(val => ++val); }; const goPrevPage = () => { @@ -68,10 +56,26 @@ const JKPeople = ({ className }) => { } }; + const handleScroll = () => { + if (window.innerHeight + window.scrollY >= document.body.offsetHeight) { + goNextPage(); + } + }; + + useEffect(() => { + window.addEventListener('scroll', handleScroll, { passive: true }); + return () => { + window.removeEventListener('scroll', handleScroll); + }; + }, []); + return ( - +