diff --git a/client/src/components/Forums/ThreadDetails/ThreadDetails.tsx b/client/src/components/Forums/ThreadDetails/ThreadDetails.tsx index 77bf0e5..8f95567 100644 --- a/client/src/components/Forums/ThreadDetails/ThreadDetails.tsx +++ b/client/src/components/Forums/ThreadDetails/ThreadDetails.tsx @@ -17,7 +17,7 @@ const ThreadDetail = ({ forumId, threadId }: ThreadDetailProps) => { const [pending, setPending] = useState(false); const [error, setError] = useState(null); const [creatingPost, setCreatingPost] = useState(false); - const [editingPostId, setEditingPost] = useState(null); + const [editingPostId, setEditingPostId] = useState(null); const [editContent, setEditContent] = useState(''); useEffect(() => { @@ -62,10 +62,26 @@ const ThreadDetail = ({ forumId, threadId }: ThreadDetailProps) => { }; const handleEditPost = (postId: string, content: string) => { - setEditingPost(postId); + 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); + } + }; + if (loading) return
Loading...
; if (error) return
Error: {error}
; if (!thread) return
Thread not found.
; @@ -87,26 +103,50 @@ const ThreadDetail = ({ forumId, threadId }: ThreadDetailProps) => {

Replies

{posts.map((post) => (
-

{post.content}

- - By {post.user.firstName} {post.user.lastName} on{' '} - {new Date(post.createdAt).toLocaleDateString()} - - {userID === post.user._id && ( - <> + {editingPostId === post._id ? ( +
+