From 5c0ed0d7730fd698baef9fef66a4164897996573 Mon Sep 17 00:00:00 2001 From: Nuwan Date: Wed, 26 Oct 2022 12:15:11 +0530 Subject: [PATCH] remove currentPage and NextPage variables and just use page variable for pagination --- jam-ui/src/components/page/JKPeopleFilter.js | 73 ++++++++++++-------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/jam-ui/src/components/page/JKPeopleFilter.js b/jam-ui/src/components/page/JKPeopleFilter.js index e5a81432d..a21f34155 100644 --- a/jam-ui/src/components/page/JKPeopleFilter.js +++ b/jam-ui/src/components/page/JKPeopleFilter.js @@ -24,8 +24,10 @@ function JKPeopleFilter() { const [genres, setGenres] = useState([]); const dispatch = useDispatch(); - const currentPage = useRef(0); - const nextPage = useRef(0); + //const currentPage = useRef(0); + //const nextPage = useRef(0); + const page = useRef(0); + const params = useRef({}); const perPageLimit = 20 @@ -141,8 +143,9 @@ function JKPeopleFilter() { const submitForm = event => { event.preventDefault(); - currentPage.current = 0; - nextPage.current = 0; + //currentPage.current = 0; + //nextPage.current = 0; + page.current = 0; dispatch(resetState()); handleSubmit(onSubmit)(); setShow(false); @@ -150,7 +153,8 @@ function JKPeopleFilter() { const isBeforeSecondPageLoad = () => { - return currentPage.current === 0 && nextPage.current === 1 + //return currentPage.current === 0 && nextPage.current === 1 + return page.current === 0 } const onSubmit = (data) => { @@ -171,16 +175,21 @@ function JKPeopleFilter() { params.current = { ...data, genres, joined_within_days, active_within_days }; try { - currentPage.current = nextPage.current + //currentPage.current = nextPage.current - if(currentPage.current === 0 && nextPage.current === 0){ - dispatch(fetchPeople({ data: params.current, page: nextPage.current, limit: perPageLimit })); + //if(currentPage.current === 0 && nextPage.current === 0){ + if(page.current === 0){ + //dispatch(fetchPeople({ data: params.current, page: nextPage.current, limit: perPageLimit })); + dispatch(fetchPeople({ data: params.current, page: page.current, limit: perPageLimit })); + page.current += 1 }else{ if(prefetched.length > 0){ dispatch(loadPrefetched()); } if(hasOffset){ - dispatch(preFetchPeople({ data: params.current, page: nextPage.current, limit: perPageLimit })); + dispatch(preFetchPeople({ data: params.current, page: page.current, limit: perPageLimit })); + //nextPage.current = nextPage.current + 1; + page.current += 1; } } @@ -189,6 +198,28 @@ function JKPeopleFilter() { } }; + useEffect(() => { + if(loadingStatus === 'succeeded'){ + if(people.length === 0 && page.current === 1 && !getValues("from_location")){ + //no results found. let's fetch again with from_location enabled + setValue('from_location', true); + //nextPage.current = 0 + //currentPage.current = 0 + page.current = 0 + submitPageQuery() + }else{ + //nextPage.current = currentPage.current + 1 + //if (isBeforeSecondPageLoad() && hasOffset) { + if (page.current === 1 && hasOffset) { + dispatch(preFetchPeople({ data: params.current, page: page.current, limit: perPageLimit })); + //nextPage.current = nextPage.current + 1; + page.current += 1; + } + } + } + + }, [loadingStatus]) + useEffect(() => { fetchGenres(); fetchInstruments(); @@ -201,31 +232,15 @@ function JKPeopleFilter() { if (resetFilter) { clearFilterOpts(); dispatch(resetState()); - currentPage.current = 0; - nextPage.current = 0; + //currentPage.current = 0; + //nextPage.current = 0; + page.current = 0; handleSubmit(onSubmit)(); setResetFilter(false); } }, [resetFilter]); - useEffect(() => { - if(loadingStatus === 'succeeded'){ - if(people.length === 0 && currentPage.current === 0 && !getValues("from_location")){ - //no results found. let's fetch again with from_location enabled - setValue('from_location', true); - nextPage.current = 0 - currentPage.current = 0 - submitPageQuery() - }else{ - nextPage.current = currentPage.current + 1 - if (isBeforeSecondPageLoad() && hasOffset) { - dispatch(preFetchPeople({ data: params.current, page: nextPage.current, limit: perPageLimit })); - nextPage.current = nextPage.current + 1; - } - } - } - - }, [loadingStatus]) + useEffect(() => { setShowLoadMore(prefetched.length > 0 || hasOffset)