diff options
author | Ashm Walia <40498934+ashmgarv@users.noreply.github.com> | 2021-01-31 13:16:41 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-31 13:16:41 -0800 |
commit | b7509400433169e698450e4a7667d268439dcf41 (patch) | |
tree | 92fcfb0f91d1498f8a36762a1ee650f17dbb3af8 /src/components/comments/CommentsContainer.tsx | |
parent | ce18efb5318c230944167d42bbde827aaca4ee4a (diff) | |
parent | 60d281814c60a471598746b4dad8f3d18be0931c (diff) |
Merge pull request #209 from ashmgarv/tma-612-611-redesign-comments
[TMA 612/611] Frontend
Diffstat (limited to 'src/components/comments/CommentsContainer.tsx')
-rw-r--r-- | src/components/comments/CommentsContainer.tsx | 97 |
1 files changed, 69 insertions, 28 deletions
diff --git a/src/components/comments/CommentsContainer.tsx b/src/components/comments/CommentsContainer.tsx index a0e94828..c72da2b7 100644 --- a/src/components/comments/CommentsContainer.tsx +++ b/src/components/comments/CommentsContainer.tsx @@ -4,6 +4,7 @@ import {FlatList} from 'react-native-gesture-handler'; import {useDispatch, useSelector} from 'react-redux'; import {CommentTile} from '.'; import {getComments} from '../../services'; +import {updateReplyPosted} from '../../store/actions'; import {RootState} from '../../store/rootReducer'; import {CommentType, ScreenType, TypeOfComment} from '../../types'; import {SCREEN_HEIGHT} from '../../utils'; @@ -41,36 +42,28 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({ const [commentsList, setCommentsList] = useState<CommentType[]>([]); const dispatch = useDispatch(); const ref = useRef<FlatList<CommentType>>(null); - const [initialIndex, setInitialIndex] = useState<number>(0); useEffect(() => { - //Scroll only if a new comment and not a reply was posted - const shouldScroll = () => - (typeOfComment === 'Comment' && !commentObjectInFocus) || - typeOfComment === 'Thread'; const loadComments = async () => { - const comments = await getComments(objectId, typeOfComment === 'Thread'); - setCommentsList(comments); - if (setCommentsLength) { - setCommentsLength(comments.length); - } - setNewCommentsAvailable(false); + await getComments(objectId, typeOfComment === 'Thread').then( + (comments) => { + if (comments && subscribedToLoadComments) { + setCommentsList(comments); + if (setCommentsLength) { + setCommentsLength(comments.length); + } + setNewCommentsAvailable(false); + } + }, + ); }; + let subscribedToLoadComments = true; if (newCommentsAvailable) { loadComments(); - if (shouldScroll()) { - if (commentId) { - const index = commentsList.findIndex( - (item) => item.comment_id === commentId, - ); - setInitialIndex(index); - } else { - setTimeout(() => { - ref.current?.scrollToEnd({animated: true}); - }, 500); - } - } } + return () => { + subscribedToLoadComments = false; + }; }, [ dispatch, objectId, @@ -78,12 +71,61 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({ setNewCommentsAvailable, setCommentsLength, typeOfComment, - commentObjectInFocus, - commentId, - commentsList, ]); - const ITEM_HEIGHT = SCREEN_HEIGHT / 7.5; + // eslint-disable-next-line no-shadow + const swapCommentTo = (commentId: string, toIndex: number) => { + const index = commentsList.findIndex( + (item) => item.comment_id === commentId, + ); + if (index > 0) { + let comments = [...commentsList]; + const temp = comments[index]; + comments[index] = comments[toIndex]; + comments[toIndex] = temp; + setCommentsList(comments); + } + }; + + useEffect(() => { + //Scroll only if a new comment and not a reply was posted + const shouldScroll = () => + typeOfComment === 'Comment' && !commentObjectInFocus; + + const performAction = () => { + if (commentId) { + swapCommentTo(commentId, 0); + } else if (shouldScroll()) { + setTimeout(() => { + ref.current?.scrollToEnd({animated: true}); + }, 500); + } + }; + if (commentsList) { + //Bring the relevant comment to top if a comment id is present else scroll if necessary + performAction(); + } + + //Clean up the reply id present in store + return () => { + if (commentId && typeOfComment === 'Thread') { + setTimeout(() => { + dispatch(updateReplyPosted(undefined)); + }, 200); + } + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [commentsList, commentId]); + + //WIP : TODO : Bring the comment in focus above the keyboard + // useEffect(() => { + // if (commentObjectInFocus && commentsList.length >= 3) { + // swapCommentTo(commentObjectInFocus.comment_id, 2); + // } + // // eslint-disable-next-line react-hooks/exhaustive-deps + // }, [commentObjectInFocus]); + + const ITEM_HEIGHT = SCREEN_HEIGHT / 7.0; const renderComment = ({item}: {item: CommentType}) => ( <CommentTile @@ -108,7 +150,6 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({ snapToInterval={ITEM_HEIGHT} renderItem={renderComment} showsVerticalScrollIndicator={false} - initialScrollIndex={initialIndex} contentContainerStyle={styles.scrollViewContent} getItemLayout={(data, index) => ({ length: ITEM_HEIGHT, |