aboutsummaryrefslogtreecommitdiff
path: root/src/components/comments/AddComment.tsx
blob: 24b3473cada6b27ddf0652781597c5386ae54da5 (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
import React, {useEffect} from 'react';
import {
  Image,
  Keyboard,
  KeyboardAvoidingView,
  Platform,
  StyleSheet,
  View,
} from 'react-native';
import {TextInput, TouchableOpacity} from 'react-native-gesture-handler';
import {useSelector} from 'react-redux';
import UpArrowIcon from '../../assets/icons/up_arrow.svg';
import {TAGG_LIGHT_BLUE} from '../../constants';
import {postMomentComment} from '../../services';
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 [keyboardVisible, setKeyboardVisible] = React.useState(false);

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

  const postComment = async () => {
    const postedComment = await postMomentComment(
      userId,
      comment.trim(),
      moment_id,
    );

    if (postedComment) {
      setComment('');
      setNewCommentsAvailable(true);
    }
  };

  useEffect(() => {
    const showKeyboard = () => setKeyboardVisible(true);
    Keyboard.addListener('keyboardWillShow', showKeyboard);
    return () => Keyboard.removeListener('keyboardWillShow', showKeyboard);
  }, []);

  useEffect(() => {
    const hideKeyboard = () => setKeyboardVisible(false);
    Keyboard.addListener('keyboardWillHide', hideKeyboard);
    return () => Keyboard.removeListener('keyboardWillHide', hideKeyboard);
  }, []);

  return (
    <KeyboardAvoidingView
      behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
      keyboardVerticalOffset={SCREEN_HEIGHT * 0.1}>
      <View
        style={[
          styles.container,
          keyboardVisible ? {backgroundColor: '#fff'} : {},
        ]}>
        <View style={styles.textContainer}>
          <Image
            style={styles.avatar}
            source={
              avatar
                ? {uri: avatar}
                : require('../../assets/images/avatar-placeholder.png')
            }
          />
          <TextInput
            style={styles.text}
            placeholder="Add a comment..."
            placeholderTextColor="grey"
            onChangeText={setComment}
            value={comment}
            autoCorrect={false}
            multiline={true}
          />
          <View style={styles.submitButton}>
            <TouchableOpacity style={styles.submitButton} onPress={postComment}>
              <UpArrowIcon width={35} height={35} color={'white'} />
            </TouchableOpacity>
          </View>
        </View>
      </View>
    </KeyboardAvoidingView>
  );
};
const styles = StyleSheet.create({
  container: {
    backgroundColor: '#f7f7f7',
    alignItems: 'center',
    width: SCREEN_WIDTH,
  },
  textContainer: {
    width: '95%',
    flexDirection: 'row',
    backgroundColor: '#e8e8e8',
    alignItems: 'center',
    justifyContent: 'space-between',
    margin: '3%',
    borderRadius: 25,
  },
  text: {
    flex: 1,
    padding: '1%',
    marginHorizontal: '1%',
  },
  avatar: {
    height: 35,
    width: 35,
    borderRadius: 30,
    marginRight: 10,
    marginLeft: '3%',
    marginVertical: '2%',
    alignSelf: 'flex-end',
  },
  submitButton: {
    height: 35,
    width: 35,
    backgroundColor: TAGG_LIGHT_BLUE,
    borderRadius: 999,
    justifyContent: 'center',
    alignItems: 'center',
    marginRight: '3%',
    marginVertical: '2%',
    alignSelf: 'flex-end',
  },
});

export default AddComment;