From d8050f68cdb8c90a5af4ccc77e02834a85a6e8e0 Mon Sep 17 00:00:00 2001 From: timoschneider Date: Wed, 12 Jun 2024 19:26:15 +0200 Subject: [PATCH] recursive comment count --- src/components/Post.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/components/Post.js b/src/components/Post.js index ea3da78..94ed370 100644 --- a/src/components/Post.js +++ b/src/components/Post.js @@ -3,7 +3,6 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faCaretUp, faCaretDown, - faCommentAlt, faComments, faAngleUp, faAngleDown @@ -33,6 +32,14 @@ const Post = ({ post, recursionDepth, selectPost, reload }) => { postApi.reactToPost(post.id, null).then(reload); } + const getComments = (post) => { + let nrOfComments = post.comments.length; + post.comments.forEach(comment => { + nrOfComments += getComments(comment); + }) + return nrOfComments; + } + return (
setShowComments(!showComments)}> @@ -46,7 +53,7 @@ const Post = ({ post, recursionDepth, selectPost, reload }) => {
- {post.comments.length} + {getComments(post)}