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

[CHE-129] Add Edit Functionality To Posts #141

80 changes: 68 additions & 12 deletions client/src/components/Forums/ThreadDetails/ThreadDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const ThreadDetail = ({ forumId, threadId }: ThreadDetailProps) => {
const [pending, setPending] = useState(false);
const [error, setError] = useState<string | null>(null);
const [creatingPost, setCreatingPost] = useState(false);
const [editingPostId, setEditingPostId] = useState<string | null>(null);
const [editContent, setEditContent] = useState<string>('');

useEffect(() => {
const fetchThreadDetails = async () => {
Expand Down Expand Up @@ -59,6 +61,28 @@ const ThreadDetail = ({ forumId, threadId }: ThreadDetailProps) => {
}
};

const handleEditPost = (postId: string, content: string) => {
setEditingPostId(postId);
setEditContent(content);
};

const handleUpdatePost = async (postId: string) => {
try {
const response = await axios.put(
`/api/forums/${forumId}/threads/${threadId}/posts/${postId}`,
{ content: editContent },
{ withCredentials: true },
);
setPosts(posts.map((post) => (post._id === postId ? response.data : post)));
setEditingPostId(null);
setEditContent('');
} catch (err) {
const error = err as Error;
setError(error.message);
}
};

// TODO pending, loading, error to be handled by global once migrated
if (pending) return null;
if (loading) return <div>Loading...</div>;
if (error) return <div>Error: {error}</div>;
Expand All @@ -81,18 +105,50 @@ const ThreadDetail = ({ forumId, threadId }: ThreadDetailProps) => {
<h3 className="font-bold text-2xl">Replies</h3>
{posts.map((post) => (
<div key={post._id} className="mb-4">
Comment on lines 105 to 107

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently setup to enable users to edit replies they've posted. Is this the goal or are we trying to enable editing of the actual posts themselves?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Mnelson98 - This PR should only allow users to edit the replies they have posted to threads (topics). There is a subsequent PR up that will allow them to edit the initial thread post itself.

<p>{post.content}</p>
<small>
By {post.user.firstName} {post.user.lastName} on{' '}
{new Date(post.createdAt).toLocaleDateString()}
</small>
{userID === post.user._id && (
<button
onClick={() => handleDeletePost(post._id)}
className="bg-red-500 font-bold hover:bg-red-700 ml-2 py-1 px-2 rounded text-white"
>
Delete
</button>
{editingPostId === post._id ? (
<div>
<textarea
className="w-full p-2 rounded bg-gray-800 text-white"
value={editContent}
onChange={(e) => setEditContent(e.target.value)}
/>
<button
onClick={() => handleUpdatePost(post._id)}
className="bg-green-500 font-bold hover:bg-green-700 ml-2 py-1 px-2 rounded text-white"
>
Save
</button>
<button
onClick={() => setEditingPostId(null)}
className="bg-red-500 font-bold hover:bg-red-700 ml-2 py-1 px-2 rounded text-white"
>
Cancel
</button>
</div>
) : (
<div>
<p>{post.content}</p>
<small>
By {post.user.firstName} {post.user.lastName} on{' '}
{new Date(post.createdAt).toLocaleDateString()}
</small>
{userID === post.user._id && (
<>
<button
onClick={() => handleDeletePost(post._id)}
className="bg-red-500 font-bold hover:bg-red-700 ml-2 py-1 px-2 rounded text-white"
>
Delete
</button>
<button
onClick={() => handleEditPost(post._id, post.content)}
className="bg-yellow-500 font-bold hover:bg-yellow-700 ml-2 py-1 px-2 rounded text-white"
>
Edit
</button>
</>
)}
</div>
)}
</div>
))}
Expand Down
Loading