Skip to content

Commit

Permalink
🚑 fixes infinite scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
kalashshah committed Mar 14, 2022
1 parent 18c7e07 commit 67a5bcc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
12 changes: 10 additions & 2 deletions expense-app/components/TransactionList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useState } from "react";
import {
StyleSheet,
Text,
Expand Down Expand Up @@ -53,9 +53,12 @@ const Item = ({ item, nav }: ItemProps) => (
interface ListProps {
data: ITransaction[];
nav: DrawerNavigationProp<DrawerRoutes, "Transaction">;
endReached: () => void;
}

const TransactionList = ({ data, nav }: ListProps) => {
const TransactionList = ({ data, nav, endReached }: ListProps) => {
const [callOnScrollEnd, setCallOnScrollEnd] = useState(false);

return (
<SafeAreaView style={styles.container}>
{data.length > 0 ? (
Expand All @@ -65,6 +68,11 @@ const TransactionList = ({ data, nav }: ListProps) => {
renderItem={({ item }) => <Item {...{ item, nav }} />}
keyExtractor={(item) => item._id}
showsVerticalScrollIndicator={false}
onEndReached={() => setCallOnScrollEnd(true)}
onMomentumScrollEnd={() => {
if (callOnScrollEnd) endReached();
setCallOnScrollEnd(false);
}}
/>
) : (
<View style={styles.empty}>
Expand Down
45 changes: 43 additions & 2 deletions expense-app/screens/Transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,61 @@ const Transaction = ({
const [isIncome, setIsIncome] = useState<boolean>(false);
const [selected, setSelected] = useState<number>(5);
const [page, setPage] = useState<number>(1);
const [reachedEnd, setReachedEnd] = useState<boolean>(false);
let cancelToken: CancelTokenSource;

useEffect(() => {
if (cancelToken) cancelToken.cancel("Cancelled");
cancelToken = axios.CancelToken.source();
setPage(1);
setReachedEnd(false);

const link = `/transaction/${isIncome ? "income" : "expense"}`;
axiosInstance
.get(link, {
params: {
...(searchPhrase !== "" ? { search: searchPhrase } : {}),
date: getDate(selected),
page,
page: 1,
},
cancelToken: cancelToken.token,
})
.then((res) => setTransactions(res.data))
.catch(() => {});
}, [isIncome, selected, searchPhrase]);

useEffect(() => {
console.log("reached end", reachedEnd);
console.log("page", page);
const link = `/transaction/${isIncome ? "income" : "expense"}`;
axiosInstance
.get(link, {
params: {
...(searchPhrase !== "" ? { search: searchPhrase } : {}),
date: getDate(selected),
page,
},
})
.then((res) => {
setTransactions((current) => {
if (current === null || res.data === undefined) {
setReachedEnd(true);
return [];
} else if (res.data.length === 0) {
setReachedEnd(true);
return current;
}
const transactions: any = {};
if (res.data.length < 10) setReachedEnd(true);
[...current, ...(res.data as ITransaction[])].forEach(
(transaction) => (transactions[transaction._id] = transaction)
);
return Object.values(transactions);
});
})
.catch(() => {});
}, [page]);

if (!transactions) return <Loading />;

return (
Expand All @@ -55,7 +90,13 @@ const Transaction = ({
<View style={styles.button}>
<ToggleButton left="Income" right="Expense" setIsOn={setIsIncome} />
</View>
<TransactionList data={transactions} nav={navigation} />
<TransactionList
data={transactions}
nav={navigation}
endReached={() => {
if (!reachedEnd) setPage((page) => page + 1);
}}
/>
</SafeAreaView>
);
};
Expand Down

0 comments on commit 67a5bcc

Please sign in to comment.