Skip to content

Commit

Permalink
CHE-64 Added a Post type and created basic ThreadDetail component
Browse files Browse the repository at this point in the history
  • Loading branch information
brok3turtl3 committed Apr 20, 2024
1 parent 3c2cd9c commit 3ead519
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
63 changes: 63 additions & 0 deletions client/src/components/Forums/ThreadDetails/ThreadDetails.tsx
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;
7 changes: 7 additions & 0 deletions client/types/forums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ export interface IForum {
_id: string;
title: string;
}

export interface IPost {
_id: string;
user: IUser;
content: string;
createdAt: string;
}

0 comments on commit 3ead519

Please sign in to comment.