fix table infinite scroll stopping after a while because the network doesn’t load all available docs, and we set the internal hook state to prevent getting new pages

This commit is contained in:
Sidney Alcantara
2022-05-30 13:12:44 +10:00
parent d02fcfc037
commit 8e303bf5f3
3 changed files with 2 additions and 9 deletions

View File

@@ -76,7 +76,6 @@ export const tablePageAtom = atom(
(get, set, update: number | ((p: number) => number)) => {
// If loading more or doesnt have next page, dont request another page
const tableNextPage = get(tableNextPageAtom);
console.log("Next page requested", JSON.stringify(tableNextPage));
if (tableNextPage.loading || !tableNextPage.available) return;
const currentPage = get(tablePageAtom);

View File

@@ -170,7 +170,6 @@ export default function Table() {
const isAtBottom =
target.clientHeight + target.scrollTop >= target.scrollHeight - offset;
if (!isAtBottom) return;
console.log("Scrolled to bottom");
// Call for the next page
setTablePage((p) => p + 1);
},

View File

@@ -147,7 +147,6 @@ export function useFirestoreCollectionWithAtom<T = TableRow>(
// Set nextPageAtom if provided and getting the next page
else if (memoizedQuery.page > 0 && nextPageAtom) {
setNextPageAtom((s) => ({ ...s, loading: true }));
console.log("Loading next page", memoizedQuery.page);
}
// Create a listener for the query
@@ -163,6 +162,8 @@ export function useFirestoreCollectionWithAtom<T = TableRow>(
setDataAtom(docs);
// If the snapshot doesnt fill the page, its the last page
if (docs.length < memoizedQuery.limit) setIsLastPage(true);
// Make sure to unset in case of mistake
else setIsLastPage(false);
// Update nextPageAtom if provided
if (nextPageAtom) {
setNextPageAtom((s) => ({
@@ -171,12 +172,6 @@ export function useFirestoreCollectionWithAtom<T = TableRow>(
available: docs.length >= memoizedQuery.limit,
}));
}
console.log(
"Loaded next page",
memoizedQuery.page,
" Next page available:",
docs.length >= memoizedQuery.limit
);
} catch (error) {
if (onError) onError(error as FirestoreError);
else handleError(error);