aboutsummaryrefslogtreecommitdiff
path: root/src/screens/profile/MomentCommentsScreen.tsx
blob: 0d685ad17287aa544f2fac3d436827be385d9286 (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
import * as React from 'react';
import {RouteProp, useNavigation} from '@react-navigation/native';
import {ProfileStackParams} from '../../routes/profile';
import {CenteredView, CommentTile, OverlayView} from '../../components';
import {CommentType} from '../../types';
import {ScrollView, StyleSheet, Text, View} from 'react-native';
import {SCREEN_WIDTH} from '../../utils/screenDimensions';
import {Button} from 'react-native-elements';
import {AddComment} from '../../components/';
import {useEffect} from 'react';
import AsyncStorage from '@react-native-community/async-storage';
import {AuthContext} from '../../routes/authentication';
import {getMomentComments} from '../..//services';

/**
 * Comments Screen for an image uploaded
 * Displays all comments for a particular moment uploaded by the user followed by a text area to add the comment.
 * Comment is posted when return is pressed on the keypad.
 */

type MomentCommentsScreenRouteProps = RouteProp<
  ProfileStackParams,
  'MomentCommentsScreen'
>;

interface MomentCommentsScreenProps {
  route: MomentCommentsScreenRouteProps;
}

const MomentCommentsScreen: React.FC<MomentCommentsScreenProps> = ({route}) => {
  const navigation = useNavigation();
  const {isProfileView, moment_id} = route.params;
  const [commentsList, setCommentsList] = React.useState([]);
  const [newCommentsAvailable, setNewCommentsAvailable] = React.useState(true);
  const {logout} = React.useContext(AuthContext);

  useEffect(() => {
    const loadComments = async () => {
      const token = await AsyncStorage.getItem('token');
      if (!token) {
        logout();
        return;
      }
      getMomentComments(moment_id, setCommentsList, token);
      setNewCommentsAvailable(false);
    };
    if (newCommentsAvailable) {
      loadComments();
    }
  }, [newCommentsAvailable]);

  return (
    <CenteredView>
      <View style={styles.modalView}>
        <View style={styles.header}>
          <Button
            title="X"
            buttonStyle={styles.button}
            titleStyle={styles.buttonText}
            onPress={() => {
              navigation.pop();
            }}
          />
          <Text style={styles.headerText}>
            {commentsList.length + ' Comments'}
          </Text>
        </View>
        <ScrollView
          style={styles.modalScrollView}
          contentContainerStyle={styles.modalScrollViewContent}>
          {commentsList &&
            commentsList.map((comment: CommentType) => (
              <CommentTile key={comment.comment_id} comment_object={comment} />
            ))}
        </ScrollView>
        <AddComment
          setNewCommentsAvailable={setNewCommentsAvailable}
          moment_id={moment_id}
        />
      </View>
    </CenteredView>
  );
};

const styles = StyleSheet.create({
  header: {flexDirection: 'row'},
  headerText: {
    position: 'relative',
    left: '180%',
    alignSelf: 'center',
    fontSize: 18,
    fontWeight: '500',
  },
  container: {
    position: 'relative',
    top: '5%',
    left: '5%',
    backgroundColor: 'white',
    borderRadius: 5,
    width: SCREEN_WIDTH / 1.1,
    height: '55%',
  },
  button: {
    backgroundColor: 'transparent',
  },
  buttonText: {
    color: 'black',
    fontSize: 18,
    fontWeight: '400',
  },
  modalView: {
    width: '85%',
    height: '70%',
    backgroundColor: '#fff',
    shadowColor: '#000',
    shadowOpacity: 30,
    shadowOffset: {width: 0, height: 2},
    shadowRadius: 5,
    borderRadius: 8,
    paddingBottom: 15,
    paddingHorizontal: 20,
    paddingTop: 5,
    justifyContent: 'space-between',
  },
  modalScrollViewContent: {
    justifyContent: 'center',
  },
  modalScrollView: {
    marginBottom: 10,
  },
});

export default MomentCommentsScreen;