Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick ThreadDisplay Functionality Update #75

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions client/src/components/Forums/ThreadsDisplay/ThreadsDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import React, { useEffect, useState } from "react";
import axios from "axios";
import { Thread } from "../../../../types/forums";
import { Thread, IForum } from "../../../../types/forums";

interface ThreadsDisplayProps {
forumId?: string | null;
}

const ThreadsDisplay: React.FC<ThreadsDisplayProps> = ({ forumId }) => {
const [threads, setThreads] = useState<Thread[]>([]);
const [forum, setForum] = useState<IForum | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
const fetchThreads = async () => {
const fetchForumAndThreads = async () => {
setLoading(true);
try {
const endpoint = forumId
? `/api/forums/${forumId}/threads`
? `/api/forums/${forumId}`
: "/api/forums/threads";
const { data } = await axios.get(endpoint, {
withCredentials: true,
});
setThreads(data);
if (forumId) {
setForum(data.forum);
setThreads(data.threads);
} else {
setForum(null);
setThreads(data);
}
setLoading(false);
} catch (err) {
const error = err as Error;
Expand All @@ -30,7 +37,7 @@ const ThreadsDisplay: React.FC<ThreadsDisplayProps> = ({ forumId }) => {
}
};

fetchThreads();
fetchForumAndThreads();
}, [forumId]);

if (loading) return <div>Loading...</div>;
Expand All @@ -39,9 +46,7 @@ const ThreadsDisplay: React.FC<ThreadsDisplayProps> = ({ forumId }) => {
return (
<div>
<h3 className="text-xl font-bold mb-4">
{forumId
? `Showing threads for forum ${forumId}`
: "Showing all threads"}
{forum ? `Showing threads for ${forum.title}` : "Showing all threads"}
</h3>
<ul>
{threads.map((thread) => (
Expand Down
5 changes: 5 additions & 0 deletions client/types/forums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ export interface Thread {
user: IUser;
createdAt: string;
}

export interface IForum {
_id: string;
title: string;
}
2 changes: 1 addition & 1 deletion server/controllers/forumController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const getAllForums = async (
};

// ENDPOINT GET api/forums/:forumId
// PURPOSE Retrieve a list of all forums
// PURPOSE Retrieve a specific forum and its threads
// ACCESS all users
const getForumById = async (
req: Request,
Expand Down
Loading