-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHE-64 Added a Post type and created basic ThreadDetail component
- Loading branch information
1 parent
3c2cd9c
commit 3ead519
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
client/src/components/Forums/ThreadDetails/ThreadDetails.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import axios from "axios"; | ||
import { Thread, IPost } from "../../../../types/forums"; | ||
|
||
interface ThreadDetailProps { | ||
forumId: string; | ||
threadId: string; | ||
} | ||
|
||
const ThreadDetail: React.FC<ThreadDetailProps> = ({ forumId, threadId }) => { | ||
const [thread, setThread] = useState<Thread | null>(null); | ||
const [posts, setPosts] = useState<IPost[]>([]); | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchThreadDetails = async () => { | ||
setLoading(true); | ||
try { | ||
const response = await axios.get( | ||
`/api/forums/${forumId}/threads/${threadId}`, | ||
{ | ||
withCredentials: true, | ||
} | ||
); | ||
setThread(response.data.thread); | ||
setPosts(response.data.posts); | ||
setLoading(false); | ||
} catch (err) { | ||
const error = err as Error; | ||
setError(error.message); | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
fetchThreadDetails(); | ||
}, [forumId, threadId]); | ||
|
||
if (loading) return <div>Loading...</div>; | ||
if (error) return <div>Error: {error}</div>; | ||
if (!thread) return <div>Thread not found.</div>; | ||
|
||
return ( | ||
<div> | ||
<h2 className="text-3xl font-bold">{thread.title}</h2> | ||
<p className="my-4">{thread.content}</p> | ||
<div> | ||
<h3 className="text-2xl font-bold">Replies</h3> | ||
{posts.map((post) => ( | ||
<div key={post._id} className="mb-4"> | ||
<p>{post.content}</p> | ||
<small> | ||
By {post.user.firstName} {post.user.lastName} on{" "} | ||
{new Date(post.createdAt).toLocaleDateString()} | ||
</small> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ThreadDetail; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters