From 6e78f61f9c0682f553d7491f871d8e9b0efe8f9c Mon Sep 17 00:00:00 2001 From: Nuwan Date: Sun, 23 Oct 2022 16:19:57 +0530 Subject: [PATCH] improve prefetching Only fetch the 'next set of records that aren't yet shown' once the initial request is done. When you click 'Load', it should be only making one request at that point -- which would be the next page after the current set being shown --- jam-ui/src/components/page/JKPeopleFilter.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/jam-ui/src/components/page/JKPeopleFilter.js b/jam-ui/src/components/page/JKPeopleFilter.js index 3a42697ef..274d3344a 100644 --- a/jam-ui/src/components/page/JKPeopleFilter.js +++ b/jam-ui/src/components/page/JKPeopleFilter.js @@ -26,6 +26,7 @@ function JKPeopleFilter() { const currentPage = useRef(0); const nextPage = useRef(0); + const params = useRef({}); const perPageLimit = 20 @@ -167,7 +168,7 @@ function JKPeopleFilter() { active_within_days = data.active_within_days.value; } - const params = { ...data, genres, joined_within_days, active_within_days }; + params.current = { ...data, genres, joined_within_days, active_within_days }; try { dispatch(loadPrefetched()); @@ -176,12 +177,7 @@ function JKPeopleFilter() { dispatch(fetchPeople({ data: params, page: currentPage.current, limit: perPageLimit })); - nextPage.current = currentPage.current + 1 - - if (hasNextPage || isBeforeSecondPageLoad()) { //reason for isBeforeSecondPageLoad(): after the first fetchPeople() there is a possibility that hasNextPage may not been sat in redux store. - dispatch(preFetchPeople({ data: params, page: nextPage.current, limit: perPageLimit })); - nextPage.current = nextPage.current + 1; - } + } catch (error) { console.log('error fetching people', error); } @@ -216,6 +212,13 @@ function JKPeopleFilter() { nextPage.current = 0 currentPage.current = 0 submitPageQuery() + }else{ + nextPage.current = currentPage.current + 1 + + if (isBeforeSecondPageLoad() && hasNextPage) { + dispatch(preFetchPeople({ data: params.current, page: nextPage.current, limit: perPageLimit })); + nextPage.current = nextPage.current + 1; + } } }