aboutsummaryrefslogtreecommitdiff
path: root/src/components/comments
diff options
context:
space:
mode:
authorIvan Chen <ivan@thetaggid.com>2021-01-29 17:37:11 -0500
committerGitHub <noreply@github.com>2021-01-29 17:37:11 -0500
commitc08699f541089aed989f3f481d10f890c3064170 (patch)
tree1951a6b12f451d6ffe53c886c96fb0e360568efe /src/components/comments
parent77002bb0e78d5c47e6daca14e8c699706a3f94a2 (diff)
parentcf77496e68b9ef97c833da35fcef2a29bd1f9ae5 (diff)
Merge pull request #207 from ashmgarv/tma605
[TMA 605] Allow comment reply redirection from notifications tab
Diffstat (limited to 'src/components/comments')
-rw-r--r--src/components/comments/CommentTile.tsx34
-rw-r--r--src/components/comments/CommentsContainer.tsx76
2 files changed, 76 insertions, 34 deletions
diff --git a/src/components/comments/CommentTile.tsx b/src/components/comments/CommentTile.tsx
index b631a985..e775a609 100644
--- a/src/components/comments/CommentTile.tsx
+++ b/src/components/comments/CommentTile.tsx
@@ -6,13 +6,19 @@ import {Alert, Animated, StyleSheet} from 'react-native';
import ClockIcon from '../../assets/icons/clock-icon-01.svg';
import {TAGG_LIGHT_BLUE} from '../../constants';
import {RectButton, TouchableOpacity} from 'react-native-gesture-handler';
-import {getTimePosted, normalize, SCREEN_WIDTH} from '../../utils';
+import {
+ getTimePosted,
+ normalize,
+ SCREEN_HEIGHT,
+ SCREEN_WIDTH,
+} from '../../utils';
import Arrow from '../../assets/icons/back-arrow-colored.svg';
import Trash from '../../assets/ionicons/trash-outline.svg';
import CommentsContainer from './CommentsContainer';
import Swipeable from 'react-native-gesture-handler/Swipeable';
import {deleteComment} from '../../services';
import {ERROR_FAILED_TO_DELETE_COMMENT} from '../../constants/strings';
+import {min} from 'moment';
/**
* Displays users's profile picture, comment posted by them and the time difference between now and when a comment was posted.
@@ -155,14 +161,22 @@ const CommentTile: React.FC<CommentTileProps> = ({
</View>
{/*** Show replies if toggle state is true */}
- {showReplies && (
- <CommentsContainer
- objectId={comment_object.comment_id}
- screenType={screenType}
- setNewCommentsAvailable={setNewThreadAvailable}
- newCommentsAvailable={newThreadAvailable}
- typeOfComment={'Thread'}
- />
+ {showReplies && comment_object.replies_count > 0 && (
+ <View
+ style={{
+ height: Math.min(
+ SCREEN_HEIGHT / 2.4,
+ (SCREEN_HEIGHT / 7.5) * comment_object.replies_count,
+ ),
+ }}>
+ <CommentsContainer
+ objectId={comment_object.comment_id}
+ screenType={screenType}
+ setNewCommentsAvailable={setNewThreadAvailable}
+ newCommentsAvailable={newThreadAvailable}
+ typeOfComment={'Thread'}
+ />
+ </View>
)}
</Swipeable>
);
@@ -173,9 +187,9 @@ const styles = StyleSheet.create({
borderBottomWidth: 1,
borderColor: 'lightgray',
backgroundColor: 'white',
- paddingTop: '3%',
flexDirection: 'column',
flex: 1,
+ paddingTop: '3%',
marginLeft: '7%',
},
swipeActions: {
diff --git a/src/components/comments/CommentsContainer.tsx b/src/components/comments/CommentsContainer.tsx
index d8134caf..a0e94828 100644
--- a/src/components/comments/CommentsContainer.tsx
+++ b/src/components/comments/CommentsContainer.tsx
@@ -1,16 +1,17 @@
import React, {useEffect, useRef, useState} from 'react';
import {StyleSheet} from 'react-native';
-import {ScrollView} from 'react-native-gesture-handler';
+import {FlatList} from 'react-native-gesture-handler';
import {useDispatch, useSelector} from 'react-redux';
import {CommentTile} from '.';
import {getComments} from '../../services';
import {RootState} from '../../store/rootReducer';
import {CommentType, ScreenType, TypeOfComment} from '../../types';
+import {SCREEN_HEIGHT} from '../../utils';
export type CommentsContainerProps = {
screenType: ScreenType;
-
//objectId can be either moment_id or comment_id
objectId: string;
+ commentId?: string;
setCommentsLength?: (count: number) => void;
newCommentsAvailable: boolean;
setNewCommentsAvailable: (value: boolean) => void;
@@ -32,18 +33,21 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
typeOfComment,
setCommentObjectInFocus,
commentObjectInFocus,
+ commentId,
}) => {
const {username: loggedInUsername} = useSelector(
(state: RootState) => state.user.user,
);
const [commentsList, setCommentsList] = useState<CommentType[]>([]);
const dispatch = useDispatch();
- const ref = useRef<ScrollView>(null);
+ 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 === 'Comment' && !commentObjectInFocus) ||
+ typeOfComment === 'Thread';
const loadComments = async () => {
const comments = await getComments(objectId, typeOfComment === 'Thread');
setCommentsList(comments);
@@ -55,9 +59,16 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
if (newCommentsAvailable) {
loadComments();
if (shouldScroll()) {
- setTimeout(() => {
- ref.current?.scrollToEnd();
- }, 500);
+ if (commentId) {
+ const index = commentsList.findIndex(
+ (item) => item.comment_id === commentId,
+ );
+ setInitialIndex(index);
+ } else {
+ setTimeout(() => {
+ ref.current?.scrollToEnd({animated: true});
+ }, 500);
+ }
}
}
}, [
@@ -68,27 +79,44 @@ const CommentsContainer: React.FC<CommentsContainerProps> = ({
setCommentsLength,
typeOfComment,
commentObjectInFocus,
+ commentId,
+ commentsList,
]);
+ const ITEM_HEIGHT = SCREEN_HEIGHT / 7.5;
+
+ const renderComment = ({item}: {item: CommentType}) => (
+ <CommentTile
+ key={item.comment_id}
+ comment_object={item}
+ screenType={screenType}
+ typeOfComment={typeOfComment}
+ setCommentObjectInFocus={setCommentObjectInFocus}
+ newCommentsAvailable={newCommentsAvailable}
+ setNewCommentsAvailable={setNewCommentsAvailable}
+ canDelete={item.commenter.username === loggedInUsername}
+ />
+ );
+
return (
- <ScrollView
+ <FlatList
+ data={commentsList}
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}
- setCommentObjectInFocus={setCommentObjectInFocus}
- newCommentsAvailable={newCommentsAvailable}
- setNewCommentsAvailable={setNewCommentsAvailable}
- canDelete={comment.commenter.username === loggedInUsername}
- />
- ))}
- </ScrollView>
+ keyExtractor={(item, index) => index.toString()}
+ decelerationRate={'fast'}
+ snapToAlignment={'start'}
+ snapToInterval={ITEM_HEIGHT}
+ renderItem={renderComment}
+ showsVerticalScrollIndicator={false}
+ initialScrollIndex={initialIndex}
+ contentContainerStyle={styles.scrollViewContent}
+ getItemLayout={(data, index) => ({
+ length: ITEM_HEIGHT,
+ offset: ITEM_HEIGHT * index,
+ index,
+ })}
+ pagingEnabled
+ />
);
};