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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
import React, {useCallback, useEffect, useState} from 'react';
import {
LayoutChangeEvent,
NativeScrollEvent,
NativeSyntheticEvent,
RefreshControl,
StyleSheet,
View,
} from 'react-native';
import Animated from 'react-native-reanimated';
import {
MomentType,
ProfilePreviewType,
ProfileType,
ScreenType,
UserType,
} from '../../types';
import {COVER_HEIGHT, defaultMoments} from '../../constants';
import {fetchUserX, SCREEN_HEIGHT, userLogin} from '../../utils';
import TaggsBar from '../taggs/TaggsBar';
import {Moment} from '../moments';
import ProfileBody from './ProfileBody';
import ProfileCutout from './ProfileCutout';
import ProfileHeader from './ProfileHeader';
import {useDispatch, useSelector, useStore} from 'react-redux';
import {RootState} from '../../store/rootreducer';
import {
followUnfollowUser,
blockUnblockUser,
loadFollowData,
updateUserXFollowersAndFollowing,
} from '../../store/actions';
import {
NO_USER,
NO_PROFILE,
EMPTY_PROFILE_PREVIEW_LIST,
EMPTY_MOMENTS_LIST,
} from '../../store/initialStates';
import {Cover} from '.';
import {Background} from '../onboarding';
interface ContentProps {
y: Animated.Value<number>;
userXId: string | undefined;
screenType: ScreenType;
}
const Content: React.FC<ContentProps> = ({y, userXId, screenType}) => {
const dispatch = useDispatch();
const {user = NO_USER, profile = NO_PROFILE} = userXId
? useSelector((state: RootState) => state.userX[screenType][userXId])
: useSelector((state: RootState) => state.user);
const {followers = EMPTY_PROFILE_PREVIEW_LIST} = userXId
? useSelector((state: RootState) => state.userX[screenType][userXId])
: useSelector((state: RootState) => state.follow);
const {moments = EMPTY_MOMENTS_LIST} = userXId
? useSelector((state: RootState) => state.userX[screenType][userXId])
: useSelector((state: RootState) => state.moments);
const {blockedUsers = EMPTY_PROFILE_PREVIEW_LIST} = useSelector(
(state: RootState) => state.blocked,
);
const {user: loggedInUser = NO_USER} = useSelector(
(state: RootState) => state.user,
);
const state = useStore().getState();
/**
* States
*/
const [imagesMap, setImagesMap] = useState<Map<string, MomentType[]>>(
new Map(),
);
const [isFollowed, setIsFollowed] = useState<boolean>(false);
const [isBlocked, setIsBlocked] = useState<boolean>(false);
const [profileBodyHeight, setProfileBodyHeight] = useState(0);
const [shouldBounce, setShouldBounce] = useState<boolean>(true);
const [refreshing, setRefreshing] = useState<boolean>(false);
const onRefresh = useCallback(() => {
const refrestState = async () => {
if (!userXId) {
await userLogin(dispatch, loggedInUser);
} else {
await fetchUserX(dispatch, user, screenType);
}
};
setRefreshing(true);
refrestState().then(() => {
setRefreshing(false);
});
}, []);
/**
* If own profile is being viewed then do not show the follow button.
*/
const isOwnProfile = loggedInUser.username === user.username;
const onLayout = (e: LayoutChangeEvent) => {
const {height} = e.nativeEvent.layout;
setProfileBodyHeight(height);
};
const createImagesMap = useCallback(() => {
var map = new Map();
moments.forEach(function (imageObject) {
var moment_category = imageObject.moment_category;
if (map.has(moment_category)) {
map.get(moment_category).push(imageObject);
} else {
map.set(moment_category, [imageObject]);
}
});
setImagesMap(map);
}, [moments]);
useEffect(() => {
createImagesMap();
}, [createImagesMap]);
/**
* This hook is called on load of profile and when you update the followers list.
*/
useEffect(() => {
const isActuallyFollowed = followers.some(
(follower) => follower.username === loggedInUser.username,
);
if (isFollowed != isActuallyFollowed) {
setIsFollowed(isActuallyFollowed);
}
}, [followers]);
useEffect(() => {
const isActuallyBlocked = blockedUsers.some(
(cur_user) => user.username === cur_user.username,
);
if (isBlocked != isActuallyBlocked) {
setIsBlocked(isActuallyBlocked);
}
}, [blockedUsers, user]);
/**
* The object returned by this method is added to the list of blocked / followed users by the reducer.
* Which helps us prevent an extra api call to the backend just to fetch a user.
*/
const getUserAsProfilePreviewType = (
passedInUser: UserType,
passedInProfile: ProfileType,
): ProfilePreviewType => {
const fullName = passedInProfile.name.split(' ');
return {
id: passedInUser.userId,
username: passedInUser.username,
first_name: fullName[0],
last_name: fullName[1],
};
};
/**
* Handles a click on the follow / unfollow button.
* followUnfollowUser takes care of updating the following list for loggedInUser
* updateUserXFollowersAndFollowing updates followers and following list for the followed user.
*/
const handleFollowUnfollow = async () => {
await dispatch(
followUnfollowUser(
loggedInUser,
getUserAsProfilePreviewType(user, profile),
isFollowed,
),
);
await dispatch(updateUserXFollowersAndFollowing(user.userId, state));
};
/**
* Handles a click on the block / unblock button.
* loadFollowData updates followers / following list for the logged in user
* updateUserXFollowersAndFollowing updates followers and following list for the followed user.
*/
const handleBlockUnblock = async () => {
await dispatch(
blockUnblockUser(
loggedInUser,
getUserAsProfilePreviewType(user, profile),
isBlocked,
),
);
await dispatch(loadFollowData(loggedInUser.userId));
await dispatch(updateUserXFollowersAndFollowing(user.userId, state));
};
const handleScroll = (e: NativeSyntheticEvent<NativeScrollEvent>) => {
/**
* Set the new y position
*/
const newY = e.nativeEvent.contentOffset.y;
y.setValue(newY);
/**
* Do not allow overflow of scroll on bottom of the screen
* SCREEN_HEIGHT - COVER_HEIGHT = Height of the scroll view
*/
if (newY >= SCREEN_HEIGHT - COVER_HEIGHT) {
setShouldBounce(false);
} else if (newY === 0) {
setShouldBounce(true);
}
};
return (
<Animated.ScrollView
style={styles.container}
onScroll={(e) => handleScroll(e)}
bounces={shouldBounce}
showsVerticalScrollIndicator={false}
scrollEventThrottle={1}
refreshControl={
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
}>
<Cover {...{userXId, screenType}} />
<ProfileCutout />
<ProfileHeader {...{userXId, screenType}} />
<ProfileBody
{...{
onLayout,
userXId,
screenType,
isOwnProfile,
isFollowed,
handleFollowUnfollow,
isBlocked,
handleBlockUnblock,
}}
/>
<TaggsBar {...{y, profileBodyHeight, userXId, screenType}} />
<View style={styles.momentsContainer}>
{defaultMoments.map((title, index) => (
<Moment
key={index}
title={title}
images={imagesMap.get(title)}
userXId={userXId}
screenType={screenType}
/>
))}
</View>
</Animated.ScrollView>
);
};
const styles = StyleSheet.create({
refreshControlContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
container: {
flex: 1,
},
momentsContainer: {
backgroundColor: '#f2f2f2',
paddingBottom: SCREEN_HEIGHT / 10,
},
});
export default Content;
|