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
This commit is contained in:
Nuwan 2022-10-23 16:19:57 +05:30
parent 78b9cd0156
commit 6e78f61f9c
1 changed files with 10 additions and 7 deletions

View File

@ -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;
}
}
}