aboutsummaryrefslogtreecommitdiff
path: root/src/components/comments/AddComment.tsx
blob: c6c816b9cdd3c5b63e01c65539a14dd19fb32c90 (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
import * as React from 'react';
import {
  Image,
  KeyboardAvoidingView,
  Platform,
  StyleSheet,
  View,
} from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
import {TaggBigInput} from '../onboarding';
import {postMomentComment} from '../../services';
import {logout} from '../../store/actions';
import {useSelector, useDispatch} from 'react-redux';
import {RootState} from '../../store/rootreducer';
import {SCREEN_HEIGHT, SCREEN_WIDTH} from '../../utils';

/**
 * This file provides the add comment view for a user.
 * Displays the logged in user's profile picture to the left and then provides space to add a comment.
 * Comment is posted when enter is pressed as requested by product team.
 */

export interface AddCommentProps {
  setNewCommentsAvailable: Function;
  moment_id: string;
}

const AddComment: React.FC<AddCommentProps> = ({
  setNewCommentsAvailable,
  moment_id,
}) => {
  const [comment, setComment] = React.useState('');

  const dispatch = useDispatch();
  const {
    avatar,
    user: {userId},
  } = useSelector((state: RootState) => state.user);

  const handleCommentUpdate = (comment: string) => {
    setComment(comment);
  };

  const postComment = async () => {
    try {
      const token = await AsyncStorage.getItem('token');
      if (!token) {
        dispatch(logout());
        return;
      }
      const postedComment = await postMomentComment(
        userId,
        comment,
        moment_id,
        token,
      );

      if (postedComment) {
        //Set the current comment to en empty string if the comment was posted successfully.
        handleCommentUpdate('');

        //Indicate the MomentCommentsScreen that it needs to download the new comments again
        setNewCommentsAvailable(true);
      }
    } catch (err) {
      console.log('Error while posting comment!');
    }
  };

  return (
    <KeyboardAvoidingView
      behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
      keyboardVerticalOffset={(1 / 9) * SCREEN_HEIGHT}>
      <View style={styles.container}>
        <Image
          style={styles.avatar}
          source={
            avatar
              ? {uri: avatar}
              : require('../../assets/images/avatar-placeholder.png')
          }
        />

        <TaggBigInput
          style={styles.text}
          containerStyle={styles.textContainer}
          multiline
          placeholder="Add a comment....."
          placeholderTextColor="gray"
          onChangeText={handleCommentUpdate}
          onSubmitEditing={postComment}
          value={comment}
        />
      </View>
    </KeyboardAvoidingView>
  );
};
const styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    justifyContent: 'flex-start',
    width: SCREEN_WIDTH,
    marginTop: '5%',
  },
  textContainer: {
    width: '100%',
    alignItems: 'flex-start',
    marginVertical: 11,
  },
  text: {
    backgroundColor: 'white',
    width: '70%',
    paddingLeft: '2%',
    paddingRight: '2%',
    paddingBottom: '1%',
    paddingTop: '1%',
    height: 60,
  },
  avatar: {
    height: 40,
    width: 40,
    borderRadius: 30,
    marginRight: 10,
    marginLeft: 20,
  },
});

export default AddComment;