aboutsummaryrefslogtreecommitdiff
path: root/src/screens/profile/MomentCommentsScreen.tsx
blob: 490edd530b11efd9ab751a6d51d1bb3860ed1d59 (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
import {
  RouteProp,
  useFocusEffect,
  useNavigation,
} from '@react-navigation/native';
import React, {useCallback, useEffect, useState} from 'react';
import {StyleSheet, View} from 'react-native';
import {SafeAreaView} from 'react-native-safe-area-context';
import {TabsGradient} from '../../components';
import {AddComment} from '../../components/';
import CommentsContainer from '../../components/comments/CommentsContainer';
import {ADD_COMMENT_TEXT} from '../../constants/strings';
import {headerBarOptions, MainStackParams} from '../../routes/main';
import {CommentThreadType, CommentType} from '../../types';
import {HeaderHeight, SCREEN_HEIGHT, SCREEN_WIDTH} from '../../utils';

/**
 * 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<
  MainStackParams,
  'MomentCommentsScreen'
>;

interface MomentCommentsScreenProps {
  route: MomentCommentsScreenRouteProps;
}

type MomentCommentContextType = {
  commentTapped: CommentType | CommentThreadType | undefined;
  setCommentTapped: (
    comment: CommentType | CommentThreadType | undefined,
  ) => void;
  shouldUpdateAllComments: boolean;
  setShouldUpdateAllComments: (available: boolean) => void;
};

export const CommentContext = React.createContext(
  {} as MomentCommentContextType,
);

const MomentCommentsScreen: React.FC<MomentCommentsScreenProps> = ({route}) => {
  const navigation = useNavigation();
  const {moment_id, screenType, comment_id} = route.params;

  //Receives comment length from child CommentsContainer
  const [commentsLength, setCommentsLength] = useState<number>(0);
  const [shouldUpdateAllComments, setShouldUpdateAllComments] =
    React.useState(true);

  //Keeps track of the current comments object in focus so that the application knows which comment to post a reply to
  const [commentTapped, setCommentTapped] = useState<
    CommentType | CommentThreadType | undefined
  >();

  useFocusEffect(
    useCallback(() => {
      navigation.dangerouslyGetParent()?.setOptions({
        tabBarVisible: false,
      });
      return () => {
        navigation.dangerouslyGetParent()?.setOptions({
          tabBarVisible: true,
        });
      };
    }, [navigation]),
  );

  useEffect(() => {
    navigation.setOptions({
      ...headerBarOptions('black', `${commentsLength} Comments`),
    });
  }, [commentsLength, navigation]);

  return (
    <CommentContext.Provider
      value={{
        commentTapped,
        setCommentTapped,
        shouldUpdateAllComments,
        setShouldUpdateAllComments,
      }}>
      <View style={styles.background}>
        <SafeAreaView>
          <View style={styles.body}>
            <CommentsContainer
              objectId={moment_id}
              commentId={comment_id}
              screenType={screenType}
              shouldUpdate={shouldUpdateAllComments}
              setShouldUpdate={setShouldUpdateAllComments}
              isThread={false}
              setCommentsLengthParent={setCommentsLength}
            />
            <AddComment
              placeholderText={
                !commentTapped
                  ? ADD_COMMENT_TEXT()
                  : ADD_COMMENT_TEXT(commentTapped.commenter.username)
              }
              momentId={moment_id}
            />
          </View>
        </SafeAreaView>
        <TabsGradient />
      </View>
    </CommentContext.Provider>
  );
};

const styles = StyleSheet.create({
  background: {
    backgroundColor: 'white',
    height: '100%',
  },
  body: {
    marginTop: HeaderHeight,
    width: SCREEN_WIDTH * 0.95,
    height: SCREEN_HEIGHT * 0.8,
    paddingTop: '3%',
  },
});

export default MomentCommentsScreen;