aboutsummaryrefslogtreecommitdiff
path: root/src/components/comments/CommentTile.tsx
blob: f11c5e33a39ca07af89e533120828c580d9e2e46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React, {useState} from 'react';
import {Text, View} from 'react-native-animatable';
import {ProfilePreview} from '../profile';
import {CommentType, ScreenType, TypeOfComment} from '../../types';
import {StyleSheet} from 'react-native';
import ClockIcon from '../../assets/icons/clock-icon-01.svg';
import {COMMENT_REPLIES} from '../../constants';
import {TouchableOpacity} from 'react-native-gesture-handler';
import {getTimePosted, normalize, SCREEN_WIDTH} from '../../utils';
import Arrow from '../../assets/icons/back-arrow-colored.svg';
import CommentsContainer from './CommentsContainer';

/**
 * Displays users's profile picture, comment posted by them and the time difference between now and when a comment was posted.
 */

interface CommentTileProps {
  comment_object: CommentType;
  screenType: ScreenType;
  typeOfComment: TypeOfComment;
  setCommentObjectInFocus?: (comment: CommentType | undefined) => void;
}

const CommentTile: React.FC<CommentTileProps> = ({
  comment_object,
  screenType,
  typeOfComment,
  setCommentObjectInFocus,
}) => {
  const timePosted = getTimePosted(comment_object.date_created);
  const [showReplies, setShowReplies] = useState<boolean>(false);

  const toggleReplies = () => {
    if (setCommentObjectInFocus) {
      if (!showReplies) {
        setCommentObjectInFocus(comment_object);
      } else {
        setCommentObjectInFocus(undefined);
      }
    }
    setShowReplies(!showReplies);
  };

  const getRepliesText = () =>
    showReplies
      ? 'Hide'
      : comment_object.replies_count > 0
      ? `Replies (${comment_object.replies_count})`
      : 'Replies';

  return (
    <>
      <View
        style={[
          styles.container,
          typeOfComment === 'Thread' ? styles.moreMarginWithThread : {},
        ]}>
        <ProfilePreview
          profilePreview={{
            id: comment_object.commenter.id,
            username: comment_object.commenter.username,
            first_name: comment_object.commenter.first_name,
            last_name: comment_object.commenter.last_name,
          }}
          previewType={'Comment'}
          screenType={screenType}
        />
        <TouchableOpacity style={styles.body} onPress={toggleReplies}>
          <Text style={styles.comment}>{comment_object.comment}</Text>
          <View style={styles.clockIconAndTime}>
            <ClockIcon style={styles.clockIcon} />
            <Text style={styles.date_time}>{' ' + timePosted}</Text>
            <View style={styles.flexer} />
            {typeOfComment === 'Comment' && comment_object.replies_count > 0 && (
              <View style={styles.repliesTextAndIconContainer}>
                <Text style={styles.repliesText}>{getRepliesText()}</Text>
                <Arrow
                  width={12}
                  height={11}
                  color={COMMENT_REPLIES}
                  style={
                    !showReplies
                      ? styles.repliesDownArrow
                      : styles.repliesUpArrow
                  }
                />
              </View>
            )}
          </View>
        </TouchableOpacity>
      </View>
      {showReplies && (
        <View
          style={{
            width: SCREEN_WIDTH,
          }}>
          <CommentsContainer
            objectId={comment_object.comment_id}
            screenType={screenType}
            setNewCommentsAvailable={() => {}}
            newCommentsAvailable={true}
            typeOfComment={'Thread'}
          />
        </View>
      )}
    </>
  );
};

const styles = StyleSheet.create({
  container: {
    marginHorizontal: '3%',
    borderBottomWidth: 1,
    borderColor: 'lightgray',
    marginBottom: '3%',
    flexDirection: 'column',
    flex: 1,
  },
  moreMarginWithThread: {
    marginHorizontal: '7%',
  },
  body: {
    marginLeft: 56,
  },
  comment: {
    marginBottom: '2%',
    top: -5,
  },
  date_time: {
    color: 'gray',
    fontSize: normalize(12),
  },
  clockIcon: {
    width: 12,
    height: 12,
    alignSelf: 'center',
  },
  clockIconAndTime: {
    flexDirection: 'row',
    marginBottom: '3%',
  },

  flexer: {
    flex: 1,
  },

  repliesTextAndIconContainer: {
    flexDirection: 'row',
    alignItems: 'center',
  },

  repliesText: {
    color: COMMENT_REPLIES,
    fontWeight: '500',
    fontSize: normalize(12),
    marginRight: '3%',
  },
  repliesBody: {
    width: SCREEN_WIDTH,
  },

  repliesDownArrow: {
    transform: [{rotate: '270deg'}],
    marginTop: '7%',
  },

  repliesUpArrow: {
    transform: [{rotate: '90deg'}],
    marginTop: '7%',
  },
});

export default CommentTile;