Skip to content

Commit

Permalink
Merge pull request #52 from engagingnewsproject/lazy-load
Browse files Browse the repository at this point in the history
Added lazy load to user list dashboard
  • Loading branch information
luukee authored Oct 22, 2024
2 parents 9e1a340 + 9d48a98 commit 7d6c6f3
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion components/Users.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,45 @@ const sortByJoinedDate = (users) => {

const Users = () => {
// Initialize authentication context
const [hasMore, setHasMore] = useState(true);
const [page, setPage] = useState(0);
const pageSize = 10;

const fetchMoreData = async () => {
if (isLoading) return;

setIsLoading(true);
try {
const mobileUsersQuerySnapshot = await getDocs(
query(
collection(db, 'mobileUsers'),
limit(pageSize),
startAfter(page * pageSize)
)
);

const newUsers = await Promise.all(
mobileUsersQuerySnapshot.docs.map(async (doc) => {
const userData = doc.data();
userData.mobileUserId = doc.id;
userData.disabled = await fetchUserDetails(doc.id);
userData.joined = getJoinedDate(userData, null);
return userData;
})
);

if (newUsers.length < pageSize) {
setHasMore(false);
}

setLoadedMobileUsers((prevUsers) => [...prevUsers, ...newUsers]);
setPage((prevPage) => prevPage + 1);
} catch (error) {
console.error('Failed to fetch more user data:', error);
} finally {
setIsLoading(false);
}
};
const {
user,
addAdminRole,
Expand Down Expand Up @@ -848,7 +887,9 @@ const Users = () => {
className="overflow-x-auto"
dataLength={endIndex}
inverse={false}
scrollableTarget="scrollableDiv">
scrollableTarget="scrollableDiv"
next={fetchMoreData}
hasMore={hasMore}>
<table className="min-w-full bg-white rounded-xl p-1">
<thead className="border-b dark:border-indigo-100 bg-slate-100">
<tr>
Expand Down

0 comments on commit 7d6c6f3

Please sign in to comment.