From 2f5f9df6dec1e905f3abf37d124ecd92d0e3a3d9 Mon Sep 17 00:00:00 2001
From: Ashm Walia <ashmwalia@outlook.com>
Date: Wed, 27 Jan 2021 08:29:56 -0800
Subject: Pre-final

---
 src/components/comments/CommentsContainer.tsx | 37 ++++++++++++++++++---------
 1 file changed, 25 insertions(+), 12 deletions(-)

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

diff --git a/src/components/comments/CommentsContainer.tsx b/src/components/comments/CommentsContainer.tsx
index 81ae8e1e..5aa32511 100644
--- a/src/components/comments/CommentsContainer.tsx
+++ b/src/components/comments/CommentsContainer.tsx
@@ -3,54 +3,66 @@ import {StyleSheet} from 'react-native';
 import {ScrollView} from 'react-native-gesture-handler';
 import {useDispatch} from 'react-redux';
 import {CommentTile} from '.';
-import {getMomentComments} from '../../services';
+import {getComments} from '../../services';
 import {CommentType, ScreenType, TypeOfComment} from '../../types';
 export type CommentsContainerProps = {
   screenType: ScreenType;
-  moment_id: string;
+
+  //objectId can be either moment_id or comment_id
+  objectId: string;
   setCommentsLength?: (count: number) => void;
   newCommentsAvailable: boolean;
   setNewCommentsAvailable: (value: boolean) => void;
   typeOfComment: TypeOfComment;
+  setCommentObjectInFocus?: (comment: CommentType | undefined) => void;
+  commentObjectInFocus?: CommentType;
 };
 
+/**
+ * Comments Container to be used for both comments and replies
+ */
+
 const CommentsContainer: React.FC<CommentsContainerProps> = ({
   screenType,
-  moment_id,
+  objectId,
   setCommentsLength,
   newCommentsAvailable,
   setNewCommentsAvailable,
   typeOfComment,
+  setCommentObjectInFocus,
+  commentObjectInFocus,
 }) => {
   const [commentsList, setCommentsList] = useState<CommentType[]>([]);
   const dispatch = useDispatch();
   const ref = useRef<ScrollView>(null);
 
   useEffect(() => {
+    //Scroll only if a new comment and not a reply was posted
+    const shouldScroll = () =>
+      typeOfComment === 'Comment' && !commentObjectInFocus;
     const loadComments = async () => {
-      console.log(moment_id);
-      const comments = await getMomentComments(moment_id);
+      const comments = await getComments(objectId, typeOfComment === 'Thread');
       setCommentsList(comments);
       if (setCommentsLength) {
         setCommentsLength(comments.length);
       }
-      console.log(comments);
       setNewCommentsAvailable(false);
     };
     if (newCommentsAvailable) {
       loadComments();
-      setTimeout(() => {
-        ref.current?.scrollToEnd({
-          animated: true,
-        });
-      }, 500);
+      if (shouldScroll()) {
+        setTimeout(() => {
+          ref.current?.scrollToEnd();
+        }, 500);
+      }
     }
   }, [
     dispatch,
-    moment_id,
+    objectId,
     newCommentsAvailable,
     setNewCommentsAvailable,
     setCommentsLength,
+    typeOfComment,
   ]);
 
   return (
@@ -65,6 +77,7 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
             comment_object={comment}
             screenType={screenType}
             typeOfComment={typeOfComment}
+            setCommentObjectInFocus={setCommentObjectInFocus}
           />
         ))}
     </ScrollView>
-- 
cgit v1.2.3-70-g09d2

