remove currentPage and NextPage variables and just use page variable for pagination

This commit is contained in:
Nuwan 2022-10-26 12:15:11 +05:30
parent 6fd7a6531b
commit 5c0ed0d773
1 changed files with 44 additions and 29 deletions

View File

@ -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)