From 2f5f9df6dec1e905f3abf37d124ecd92d0e3a3d9 Mon Sep 17 00:00:00 2001 From: Ashm Walia Date: Wed, 27 Jan 2021 08:29:56 -0800 Subject: Pre-final --- src/components/comments/AddComment.tsx | 17 +++++++----- src/components/comments/CommentTile.tsx | 14 ++++++++++ src/components/comments/CommentsContainer.tsx | 37 ++++++++++++++++++--------- 3 files changed, 50 insertions(+), 18 deletions(-) (limited to 'src/components') diff --git a/src/components/comments/AddComment.tsx b/src/components/comments/AddComment.tsx index 86f4170c..46086e81 100644 --- a/src/components/comments/AddComment.tsx +++ b/src/components/comments/AddComment.tsx @@ -25,14 +25,14 @@ export interface AddCommentProps { setNewCommentsAvailable: Function; objectId: string; placeholderText: string; - isThreadInFocus: boolean; + isCommentInFocus: boolean; } const AddComment: React.FC = ({ setNewCommentsAvailable, objectId, placeholderText, - isThreadInFocus, + isCommentInFocus, }) => { const [comment, setComment] = React.useState(''); const [keyboardVisible, setKeyboardVisible] = React.useState(false); @@ -43,7 +43,7 @@ const AddComment: React.FC = ({ const postedComment = await postComment( comment.trim(), objectId, - isThreadInFocus, + isCommentInFocus, ); if (postedComment) { @@ -65,11 +65,13 @@ const AddComment: React.FC = ({ }, []); const ref = useRef(null); + + //If a comment is in Focus, bring the keyboard up so user is able to type in a reply useEffect(() => { - if (isThreadInFocus) { + if (isCommentInFocus) { ref.current?.focus(); } - }, [isThreadInFocus]); + }, [isCommentInFocus]); return ( = ({ = ({ const timePosted = getTimePosted(comment_object.date_created); const [showReplies, setShowReplies] = useState(false); + /** + * Case : A COMMENT IS IN FOCUS && REPLY SECTION IS HIDDEN + * Bring the current comment to focus + * Case : No COMMENT IS IN FOCUS && REPLY SECTION IS SHOWN + * Unfocus comment in focus + * In any case toggle value of showReplies + */ const toggleReplies = () => { if (setCommentObjectInFocus) { if (!showReplies) { @@ -41,6 +48,9 @@ const CommentTile: React.FC = ({ setShowReplies(!showReplies); }; + /** + * Method to compute text to be shown for replies button + */ const getRepliesText = () => showReplies ? 'Hide' @@ -71,6 +81,8 @@ const CommentTile: React.FC = ({ {' ' + timePosted} + + {/*** Show replies text only if there are some replies present */} {typeOfComment === 'Comment' && comment_object.replies_count > 0 && ( {getRepliesText()} @@ -89,6 +101,8 @@ const CommentTile: React.FC = ({ + + {/*** Show replies if toggle state is true */} {showReplies && ( 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 = ({ screenType, - moment_id, + objectId, setCommentsLength, newCommentsAvailable, setNewCommentsAvailable, typeOfComment, + setCommentObjectInFocus, + commentObjectInFocus, }) => { const [commentsList, setCommentsList] = useState([]); const dispatch = useDispatch(); const ref = useRef(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 = ({ comment_object={comment} screenType={screenType} typeOfComment={typeOfComment} + setCommentObjectInFocus={setCommentObjectInFocus} /> ))} -- cgit v1.2.3-70-g09d2