From 8e295367d7c992894ca2ac5c2175926e0ed3ab40 Mon Sep 17 00:00:00 2001
From: Brian Kim <brian@tagg.id>
Date: Mon, 10 May 2021 17:02:24 -0700
Subject: Fixed comment counting situation for moments

---
 src/components/comments/CommentsContainer.tsx | 11 +++++++++++
 1 file changed, 11 insertions(+)

(limited to 'src/components/comments/CommentsContainer.tsx')

diff --git a/src/components/comments/CommentsContainer.tsx b/src/components/comments/CommentsContainer.tsx
index cd9ecb02..0bfd5ad6 100644
--- a/src/components/comments/CommentsContainer.tsx
+++ b/src/components/comments/CommentsContainer.tsx
@@ -18,6 +18,7 @@ export type CommentsContainerProps = {
   shouldUpdate: boolean;
   setShouldUpdate: (update: boolean) => void;
   isThread: boolean;
+  setCommentsLengthParent: (length: number) => void;
 };
 
 /**
@@ -31,6 +32,7 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
   shouldUpdate,
   setShouldUpdate,
   commentId,
+  setCommentsLengthParent,
 }) => {
   const {setCommentsLength, commentTapped} = useContext(CommentContext);
   const {username: loggedInUsername} = useSelector(
@@ -41,6 +43,14 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
   const ref = useRef<FlatList<CommentType>>(null);
   const ITEM_HEIGHT = SCREEN_HEIGHT / 7.0;
 
+  const countComments = (comments: CommentType[]) => {
+    let count = 0;
+    for (let i = 0; i < comments.length; i++) {
+      count += 1 + comments[i].replies_count;
+    }
+    return count;
+  }
+
   useEffect(() => {
     const loadComments = async () => {
       await getComments(objectId, isThread).then((comments) => {
@@ -51,6 +61,7 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
           }
           setShouldUpdate(false);
         }
+        setCommentsLengthParent(countComments(comments));
       });
     };
     let subscribedToLoadComments = true;
-- 
cgit v1.2.3-70-g09d2

