From bb3900d23b4a9ad2f3e8e4882d0495eb9ddbd955 Mon Sep 17 00:00:00 2001 From: JustAnyone Date: Thu, 6 Nov 2025 20:35:25 +0200 Subject: [PATCH] Fix comment count being broken for first comment --- Client/app/routes/post.tsx | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Client/app/routes/post.tsx b/Client/app/routes/post.tsx index 5e48a6d..5c27128 100644 --- a/Client/app/routes/post.tsx +++ b/Client/app/routes/post.tsx @@ -213,13 +213,21 @@ export default function Post() { const onAddComment = async (postId: number, commentText: string) => { - if (!user || !paginatedComments) return; + if (!user) return; const res = await API.postComment(postId, commentText, user); if (!res.ok) { alert("Failed to add comment"); return; } const newComment: Comment = await res.json(); + + // Refetch comments if this is the first one. + if (!paginatedComments || paginatedComments.totalCount === 0) { + const comments = await API.fetchCommentsByPostId(idNum, 1); + setPaginatedComments(comments); + setCurrentCommentPage(1); + return; + } const totalPages = Math.ceil((paginatedComments.totalCount + 1) / paginatedComments.pageSize); if (currentCommentPage !== totalPages) { @@ -232,7 +240,7 @@ export default function Post() { return { ...prev, items: [...prev.items, newComment], - total: prev.totalCount + 1 + totalCount: prev.totalCount + 1 }; } else { // This case is unlikely if totalPages is calculated correctly, but as a fallback, we refetch. @@ -259,10 +267,13 @@ export default function Post() { setPaginatedComments(prev => { if (!prev) return null; const newItems = prev.items.filter(c => c.id !== commentId); + const newTotalCount = prev.totalCount - 1; + if (newItems.length === 0 && currentCommentPage > 1) { setCurrentCommentPage(currentCommentPage - 1); + return { ...prev, items: newItems, totalCount: newTotalCount }; } - return { ...prev, items: newItems, total: prev.totalCount - 1 }; + return { ...prev, items: newItems, totalCount: newTotalCount }; }); };