-
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.
Merge pull request #77 from Code-Hammers/CHE-64/subtask/Create-Thread…
…Details-Component [CHE-64] Create ThreadDetails Component
- Loading branch information
Showing
6 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import axios from "axios"; | ||
import { Thread, IPost } from "../../../../types/forums"; | ||
|
||
interface ThreadDetailProps { | ||
forumId: string | null; | ||
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 endpoint = forumId | ||
? `/api/forums/${forumId}/threads/${threadId}` | ||
: `/api/forums/threads/${threadId}`; | ||
const response = await axios.get(endpoint, { 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
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
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
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
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