aboutsummaryrefslogtreecommitdiff
path: root/src/components/profile/Content.tsx
blob: df692a3fe465f9ce598dc260f5680dfddb420a3c (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {useScrollToTop} from '@react-navigation/native';
import React, {useCallback, useEffect, useRef, useState} from 'react';
import {LayoutChangeEvent, RefreshControl, StyleSheet} from 'react-native';
import Animated, {
  useAnimatedScrollHandler,
  useSharedValue,
} from 'react-native-reanimated';
import {useDispatch, useSelector, useStore} from 'react-redux';
import {MomentUploadProgressBar} from '..';
import {
  blockUnblockUser,
  loadFriendsData,
  updateUserXFriends,
} from '../../store/actions';
import {
  EMPTY_PROFILE_PREVIEW_LIST,
  NO_PROFILE,
  NO_USER,
} from '../../store/initialStates';
import {RootState} from '../../store/rootreducer';
import {ScreenType} from '../../types';
import {
  canViewProfile,
  fetchUserX,
  getUserAsProfilePreviewType,
  userLogin,
} from '../../utils';
import TaggsBar from '../taggs/TaggsBar';
import Cover from './Cover';
import PrivateProfile from './PrivateProfile';
import ProfileBody from './ProfileBody';
import ProfileCutout from './ProfileCutout';
import ProfileHeader from './ProfileHeader';
import PublicProfile from './PublicProfile';

interface ContentProps {
  userXId: string | undefined;
  screenType: ScreenType;
}
const Content: React.FC<ContentProps> = ({userXId, screenType}) => {
  const dispatch = useDispatch();
  const {user = NO_USER, profile = NO_PROFILE} = useSelector(
    (state: RootState) =>
      userXId ? state.userX[screenType][userXId] : state.user,
  );
  const {blockedUsers = EMPTY_PROFILE_PREVIEW_LIST} = useSelector(
    (state: RootState) => state.blocked,
  );
  const {user: loggedInUser = NO_USER} = useSelector(
    (state: RootState) => state.user,
  );
  const state: RootState = useStore().getState();

  /*
   * Used to imperatively scroll to the top when presenting the moment tutorial.
   */
  const scrollViewRef = useRef(null);
  /*
   * If scrolling is enabled. Set to false before scrolling up for the tutorial.
   */
  const [scrollEnabled, setScrollEnabled] = useState<boolean>(true);
  const y = useSharedValue<number>(0);

  /**
   * States
   */
  const [isBlocked, setIsBlocked] = useState<boolean>(false);
  const [profileBodyHeight, setProfileBodyHeight] = useState(0);
  const [socialsBarHeight, setSocialsBarHeight] = useState(0);
  const [refreshing, setRefreshing] = useState<boolean>(false);

  const onRefresh = useCallback(() => {
    const refrestState = async () => {
      setRefreshing(true);
      if (!userXId) {
        await userLogin(dispatch, loggedInUser);
      } else {
        await fetchUserX(dispatch, user, screenType);
      }
    };
    refrestState().then(() => {
      setRefreshing(false);
    });
  }, []);

  const onLayout = (e: LayoutChangeEvent) => {
    const {height} = e.nativeEvent.layout;
    setProfileBodyHeight(height);
  };

  const onSocialsBarLayout = (e: LayoutChangeEvent) => {
    const {height} = e.nativeEvent.layout;
    setSocialsBarHeight(height);
  };

  useEffect(() => {
    const isActuallyBlocked = blockedUsers.some(
      (cur_user) => user.username === cur_user.username,
    );
    if (isBlocked !== isActuallyBlocked) {
      setIsBlocked(isActuallyBlocked);
    }
  }, [blockedUsers, user]);

  /**
   * Handles a click on the block / unblock button.
   * loadFriends updates friends list for the logged in user
   * updateUserXFriends updates friends list for the user.
   */
  const handleBlockUnblock = async (callback?: () => void) => {
    dispatch(
      blockUnblockUser(
        loggedInUser,
        getUserAsProfilePreviewType(user, profile),
        isBlocked,
      ),
    );
    dispatch(loadFriendsData(loggedInUser.userId));
    dispatch(updateUserXFriends(user.userId, state));
    if (callback) {
      callback();
    }
  };

  const scrollHandler = useAnimatedScrollHandler((event) => {
    y.value = event.contentOffset.y;
  });

  useScrollToTop(scrollViewRef);

  return (
    <Animated.ScrollView
      ref={scrollViewRef}
      contentContainerStyle={styles.contentContainer}
      style={styles.container}
      onScroll={scrollHandler}
      showsVerticalScrollIndicator={false}
      scrollEventThrottle={1}
      stickyHeaderIndices={[5]}
      scrollEnabled={scrollEnabled}
      refreshControl={
        <RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
      }>
      {!userXId && <MomentUploadProgressBar />}
      <Cover {...{userXId, screenType}} />
      <ProfileCutout />
      <ProfileHeader
        {...{
          userXId,
          screenType,
          handleBlockUnblock,
          isBlocked,
          isPrivate: !canViewProfile(state, userXId, screenType),
        }}
      />
      <ProfileBody
        {...{
          onLayout,
          userXId,
          screenType,
          handleBlockUnblock,
          isBlocked,
        }}
      />
      <TaggsBar
        {...{y, profileBodyHeight, userXId, screenType}}
        onLayout={onSocialsBarLayout}
      />
      {canViewProfile(state, userXId, screenType) ? (
        <PublicProfile
          {...{
            y,
            userXId,
            screenType,
            setScrollEnabled,
            profileBodyHeight,
            socialsBarHeight,
            scrollViewRef,
          }}
        />
      ) : (
        <PrivateProfile />
      )}
    </Animated.ScrollView>
  );
};

const styles = StyleSheet.create({
  container: {
    backgroundColor: '#fff',
    flex: 1,
  },
  contentContainer: {
    flexGrow: 1,
  },
});

export default Content;