Skip to content

Commit

Permalink
trying to solve infinite scroll – needs work #66
Browse files Browse the repository at this point in the history
with some suggestions by tutor John
  • Loading branch information
blahosyl committed Aug 10, 2024
1 parent 6140fe0 commit 25d4106
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/pages/profiles/ProfileList.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import React, { useState } from "react";
import React from "react";
import Container from "react-bootstrap/Container";

import appStyles from "../../App.module.css";
import Asset from "../../components/Asset";
import { useProfileData } from "../../contexts/ProfileDataContext";
import { useProfileData, useSetProfileData } from "../../contexts/ProfileDataContext";
import Profile from "./Profile";
import { Link } from "react-router-dom/cjs/react-router-dom.min";
import { useCurrentUser } from "../../contexts/CurrentUserContext";
import InfiniteScroll from "react-infinite-scroll-component";
import { fetchMoreData } from "../../utils/utils";

console.log(useSetProfileData)

/**
* Render the list of profiles from most to least recently updated
*/
const ProfileList = () => {
const { profileList } = useProfileData();
const { setProfileData } = useSetProfileData();
const currentUser = useCurrentUser();
const [profileList, setProfileList] = useState(useProfileData());
console.log('profileList', profileList)
console.log('profileList.results.length', profileList.results.length)
console.log('!!profileList.next', !!profileList.next)

return (
// only render the component if a user is logged in
Expand All @@ -29,19 +35,21 @@ const ProfileList = () => {
<i className="fa-solid fa-users-line"></i>Teammates
</h3>
</Link>
{/* list of users excluding the logged-in user */}
<InfiniteScroll
children={profileList.results.map(
(profile) =>
profile &&
/* list of users excluding the logged-in user */
Number(profile.id) !== Number(currentUser.pk) && (
<Profile key={profile.id} profile={profile} />
)
)}
dataLength={profileList.results.length}
loader={<Asset spinner />}
hasMore={!!profileList.next}
next={() => fetchMoreData(profileList, setProfileList)}
scrollThreshold={5}
next={() => {fetchMoreData(useProfileData, useSetProfileData)}
}
/>
</>
) : (
Expand Down
1 change: 1 addition & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { axiosReq } from "../api/axiosDefaults";
import jwtDecode from "jwt-decode";

export const fetchMoreData = async (resource, setResource) => {
console.log('fetchMoreData');
try {
const { data } = await axiosReq.get(resource.next);
setResource((prevResource) => ({
Expand Down

0 comments on commit 25d4106

Please sign in to comment.