diff options
author | Ashm Walia <ashmwalia@outlook.com> | 2021-01-25 18:00:44 -0800 |
---|---|---|
committer | Ashm Walia <ashmwalia@outlook.com> | 2021-01-25 18:00:44 -0800 |
commit | 755f4f540d3e759ff9ad3bc35c64b6f7fc83998b (patch) | |
tree | 6619de58ffcd0a5c9e32a612ffc82f0d65d041d9 /src/components/comments/CommentsContainer.tsx | |
parent | 6cd49ed14f99fe953026e54969abc6232f3aec57 (diff) |
Add provision to show and hide replies
Diffstat (limited to 'src/components/comments/CommentsContainer.tsx')
-rw-r--r-- | src/components/comments/CommentsContainer.tsx | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/components/comments/CommentsContainer.tsx b/src/components/comments/CommentsContainer.tsx new file mode 100644 index 00000000..81ae8e1e --- /dev/null +++ b/src/components/comments/CommentsContainer.tsx @@ -0,0 +1,83 @@ +import React, {useRef, useEffect, useState} from 'react'; +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 {CommentType, ScreenType, TypeOfComment} from '../../types'; +export type CommentsContainerProps = { + screenType: ScreenType; + moment_id: string; + setCommentsLength?: (count: number) => void; + newCommentsAvailable: boolean; + setNewCommentsAvailable: (value: boolean) => void; + typeOfComment: TypeOfComment; +}; + +const CommentsContainer: React.FC<CommentsContainerProps> = ({ + screenType, + moment_id, + setCommentsLength, + newCommentsAvailable, + setNewCommentsAvailable, + typeOfComment, +}) => { + const [commentsList, setCommentsList] = useState<CommentType[]>([]); + const dispatch = useDispatch(); + const ref = useRef<ScrollView>(null); + + useEffect(() => { + const loadComments = async () => { + console.log(moment_id); + const comments = await getMomentComments(moment_id); + setCommentsList(comments); + if (setCommentsLength) { + setCommentsLength(comments.length); + } + console.log(comments); + setNewCommentsAvailable(false); + }; + if (newCommentsAvailable) { + loadComments(); + setTimeout(() => { + ref.current?.scrollToEnd({ + animated: true, + }); + }, 500); + } + }, [ + dispatch, + moment_id, + newCommentsAvailable, + setNewCommentsAvailable, + setCommentsLength, + ]); + + return ( + <ScrollView + ref={ref} + style={styles.scrollView} + contentContainerStyle={styles.scrollViewContent}> + {commentsList && + commentsList.map((comment: CommentType) => ( + <CommentTile + key={comment.comment_id} + comment_object={comment} + screenType={screenType} + typeOfComment={typeOfComment} + /> + ))} + </ScrollView> + ); +}; + +const styles = StyleSheet.create({ + scrollView: { + paddingHorizontal: 20, + }, + scrollViewContent: { + justifyContent: 'center', + }, +}); + +export default CommentsContainer; |