Skip to content

Commit

Permalink
feat: forum home func
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonNotJson committed Sep 7, 2023
1 parent ab059b0 commit 8939038
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
2 changes: 1 addition & 1 deletion apps/forum/src/components/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const Board = () => {
(thread: Thread) => thread.board_id === boardId
);
setBoardThreads(threads);
console.log(boardThreads);
console.log(threads);

var filteredThreads = filterThreadsByTags(threads, currentTags);
filteredThreads = filterThreadsByGroups(filteredThreads, currentGroups);
Expand Down
2 changes: 1 addition & 1 deletion apps/forum/src/components/FilterMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const FilterMenu = () => {
className="w-full p-2 bg-light-main text-white rounded-lg my-1"
onClick={toggleSchoolFilter}
>
Select the School
Select a School
</button>
{openSchoolModal && (
<SchoolFilterForm
Expand Down
77 changes: 53 additions & 24 deletions apps/forum/src/components/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,72 @@ const Home = () => {

// fetching the all thread data
useEffect(() => {
getAllThreads();
}, []);
const fetchData = async () => {
try {
let userId = userToken;
if (userId.length === 0) {
const userAttr = await getUserAttr();
if (userAttr) {
userId = userAttr.id;
setUserToken(userId);
}
}

useEffect(() => {
console.log("allThreads has updated:", allThreads);
}, [allThreads]);
console.log(`Fetching data for userId: ${userId}`); // Debugging

const getAllThreads = async () => {
try {
let userId = userToken;
if (userId.length === 0) {
const userAttr = await getUserAttr();
if (userAttr) {
userId = userAttr.id;
setUserToken(userId);
}
const response = await API.get(
"wasedatime-dev",
`/forum?uid=${userId}`,
{
headers: {
"Content-Type": "application/json",
},
}
);

console.log("API Response:", response.data); // Debugging

setAllThreads([...response.data]);
} catch (error) {
console.error("An error occurred:", error); // Enhanced error logging
}
};

const response = await API.get("wasedatime-dev", `/forum?uid=${userId}`, {
headers: {
"Content-Type": "application/json",
},
});
setAllThreads([...response.data]);
} catch (error) {
console.error("Error fetching threads:", error);
}
};
fetchData();
}, []);

useEffect(() => {
console.log("filter Threads has updated:", filteredThreads);
}, [filteredThreads]);

useEffect(() => {
var filteredThreads = filterThreadsByTags(allThreads, currentTags);
filteredThreads = filterThreadsByGroups(filteredThreads, currentGroups);
if (filteredThreads.length > threadCountPerPage * page)
filteredThreads = filteredThreads.slice(0, threadCountPerPage * page);
if (filteredThreads.length > threadCountPerPage * page)
filteredThreads = filteredThreads.slice(0, threadCountPerPage * page);
setFilteredThreads(filteredThreads);
}, [currentTags, currentGroups]);

const displayMoreThread = () => {
console.log("displayMoreThread called"); // Debugging
setTimeout(() => {
console.log(
"Inside setTimeout, allThreads.length:",
allThreads.length,
"threadCountPerPage:",
threadCountPerPage,
"page:",
page
); // Debugging
if (allThreads.length < threadCountPerPage * page) return;
const nextPage = page + 1;
setPage(nextPage);
var threads = filterThreadsByTags(allThreads, currentTags);
threads = filterThreadsByGroups(threads, currentGroups);
threads = threads.slice(0, threadCountPerPage * nextPage);
console.log("Setting filteredThreads:", threads); // Debugging
setFilteredThreads(threads);
}, 1000);
};
Expand Down

0 comments on commit 8939038

Please sign in to comment.