aboutsummaryrefslogtreecommitdiff
path: root/src/screens/profile/ProfileScreen.tsx
blob: a2a1b5bda3f852baa7ee476c3ad9a891f1a4b084 (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
import React, {useContext, useEffect, useState} from 'react';
import {StatusBar} from 'react-native';
import Animated from 'react-native-reanimated';
import {Content, Cover, TabsGradient} from '../../components';
import {RouteProp, useFocusEffect} from '@react-navigation/native';
import {ProfileStackParams, ProfileProvider} from '../../routes/';
import {resetScreenType} from '../../store/actions';
import {useDispatch, useStore} from 'react-redux';
import {ScreenType} from '../../types';
import {DUMMY_USERID} from '../../store/initialStates';

/**r
 * Profile Screen for a user's profile
 * including posts, messaging, and settings
 */

type ProfileScreenRouteProps = RouteProp<ProfileStackParams, 'Profile'>;

interface ProfileOnboardingProps {
  route: ProfileScreenRouteProps;
}

const ProfileScreen: React.FC<ProfileOnboardingProps> = ({route}) => {
  const {screenType} = route.params;
  let {userXId} = route.params;
  const y = Animated.useValue(0);
  const dispatch = useDispatch();

  /**
   * This is a double safety check to avoid app crash.
   * Checks if the required userXId is present in the store, if not userXId is set to dummy id
   */
  if (userXId && !(userXId in useStore().getState().userX[screenType])) {
    userXId = DUMMY_USERID;
  }

  /**
   * Code under useFocusEffect gets executed every time the screen comes under focus / is being viewed by the user.
   * This is done to reset the users stored in our store for the Search screen.
   * Read more about useFocusEffect here : https://reactnavigation.org/docs/function-after-focusing-screen/
   */
  useFocusEffect(() => {
    if (!userXId) {
      dispatch(resetScreenType(screenType));
    }
  });

  return (
    <>
      <StatusBar />
      <Cover {...{y, userXId, screenType}} />
      <Content {...{y, userXId, screenType}} />
      <TabsGradient />
    </>
  );
};

export default ProfileScreen;