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
|
import AsyncStorage from '@react-native-community/async-storage';
import React, {createContext, useEffect, useState} from 'react';
import {Value} from 'react-native-reanimated';
import {INTEGRATED_SOCIAL_LIST} from '../../constants';
import {
loadAvatar,
loadCover,
loadProfileInfo,
loadSocialPosts,
loadMoments,
loadFollowers,
loadFollowing,
} from '../../services';
import {
ProfileType,
SocialAccountType,
ProfilePreviewType,
UserType,
MomentType,
} from '../../types';
interface ProfileContextProps {
user: UserType;
profile: ProfileType;
avatar: string | null;
cover: string | null;
socialAccounts: Record<string, SocialAccountType>;
socialsNeedUpdate: (_: string[]) => void;
moments: MomentType[];
followers: ProfilePreviewType[];
following: ProfilePreviewType[];
followersNeedUpdate: boolean;
updateFollowers: (value: boolean) => void;
}
const NO_USER: UserType = {
userId: '',
username: '',
};
const NO_PROFILE: ProfileType = {
biography: '',
website: '',
name: '',
};
const NO_SOCIAL_ACCOUNTS: Record<string, SocialAccountType> = {
Instagram: {posts: []},
Facebook: {posts: []},
Twitter: {posts: []},
};
export const ProfileContext = createContext<ProfileContextProps>({
user: NO_USER,
profile: NO_PROFILE,
avatar: null,
cover: null,
socialAccounts: NO_SOCIAL_ACCOUNTS,
socialsNeedUpdate: () => {},
moments: [],
followers: [],
following: [],
followersNeedUpdate: true,
updateFollowers: () => {},
});
/**
* This is the context provider for user profiles that the logged in user wants to see
* The ProfileProviderProps is used to initialise data as soon as the component is initialised.
*/
type ProfileProviderProps = {
uname: string;
uId: string;
};
const ProfileProvider: React.FC<ProfileProviderProps> = ({
children,
uId,
uname,
}) => {
const [user, setUser] = useState<UserType>({userId: uId, username: uname});
const [profile, setProfile] = useState<ProfileType>(NO_PROFILE);
const [avatar, setAvatar] = useState<string | null>(null);
const [cover, setCover] = useState<string | null>(null);
const [newMomentsAvailable, setNewMomentsAvailable] = useState<boolean>(true);
const [socialAccounts, setSocialAccounts] = useState<
Record<string, SocialAccountType>
>(NO_SOCIAL_ACCOUNTS);
const [moments, setMoments] = useState<Array<MomentType>>([]);
const [followers, setFollowers] = useState<Array<ProfilePreviewType>>([]);
const [following, setFollowing] = useState<Array<ProfilePreviewType>>([]);
const [followersNeedUpdate, setFollowersNeedUpdate] = useState<boolean>(true);
// Default update all integrated social lists on start
const [socialsNeedUpdate, setSocialsNeedUpdate] = useState<string[]>([
...INTEGRATED_SOCIAL_LIST,
]);
const {userId} = user;
useEffect(() => {
if (!userId) {
return;
}
const loadData = async () => {
try {
const token = await AsyncStorage.getItem('token');
if (!token) {
setUser(NO_USER);
return;
}
loadProfileInfo(token, userId, setProfile);
loadAvatar(token, userId, setAvatar);
loadCover(token, userId, setCover);
} catch (err) {
console.log(err);
}
};
loadData();
}, [userId]);
useEffect(() => {
const loadNewMoments = async () => {
try {
const token = await AsyncStorage.getItem('token');
if (!token) {
setUser(NO_USER);
return;
}
const newMoments = await loadMoments(userId, token);
if (newMoments) {
setMoments(newMoments);
}
setNewMomentsAvailable(false);
} catch (error) {
console.log(error);
}
};
if (newMomentsAvailable && userId) {
loadNewMoments();
}
}, [newMomentsAvailable, userId]);
useEffect(() => {
const loadNewFollowers = async () => {
try {
const token = await AsyncStorage.getItem('token');
if (!token) {
setUser(NO_USER);
return;
}
loadFollowers(userId, token, setFollowers);
loadFollowing(userId, token, setFollowing);
setFollowersNeedUpdate(false);
} catch (error) {
console.log(error);
}
};
if (followersNeedUpdate && userId) {
loadNewFollowers();
}
}, [followersNeedUpdate, userId, followers, following]);
useEffect(() => {
if (socialsNeedUpdate.length > 0 && userId) {
for (let social of socialsNeedUpdate) {
loadSocialPosts(userId, social).then((accountData) => {
/**
* Please use the following syntax when updating an object, fixing this problem broke our head. LOLs
* ref1 : https://stackoverflow.com/questions/56423256/set-dynamic-key-in-state-via-usestate-react-hooks
* ref2: https://stackoverflow.com/questions/43638938/updating-an-object-with-setstate-in-react/43639228
* The spread operator {...} helps us make a simple copy of the object
* And form there on we can use the [] to specify the dynamically constructed key and set its value.
*/
setSocialAccounts((prevSocialAccounts) => ({
...prevSocialAccounts,
[social]: accountData,
}));
console.log('Updated posts data', social);
});
}
setSocialsNeedUpdate([]);
}
}, [socialAccounts, socialsNeedUpdate, userId]);
return (
<ProfileContext.Provider
value={{
user,
profile,
avatar,
cover,
socialAccounts,
moments,
followers,
following,
followersNeedUpdate,
socialsNeedUpdate: (socials: string[]) => {
setSocialsNeedUpdate(socials);
},
updateFollowers: (value) => {
setFollowersNeedUpdate(value);
},
}}>
{children}
</ProfileContext.Provider>
);
};
export default ProfileProvider;
|