aboutsummaryrefslogtreecommitdiff
path: root/src/routes/viewProfile/ProfileProvider.tsx
diff options
context:
space:
mode:
authorAshm Walia <40498934+ashmgarv@users.noreply.github.com>2020-10-27 17:36:03 -0700
committerGitHub <noreply@github.com>2020-10-27 20:36:03 -0400
commit62d6fe2bca4bdd1a48cfe54e4c0c3fe590c3b750 (patch)
tree3afb2c723a10a2649c298e226bc31e10cec92d5a /src/routes/viewProfile/ProfileProvider.tsx
parent795ba089207571ec13226f2d07c149c8697763ce (diff)
[HOT FIX] Refactor to make things clean and make the app work (#82)
* Refactored * Final refactor with an issue * It works * Whoops
Diffstat (limited to 'src/routes/viewProfile/ProfileProvider.tsx')
-rw-r--r--src/routes/viewProfile/ProfileProvider.tsx76
1 files changed, 74 insertions, 2 deletions
diff --git a/src/routes/viewProfile/ProfileProvider.tsx b/src/routes/viewProfile/ProfileProvider.tsx
index 0600b65b..f9b29dc6 100644
--- a/src/routes/viewProfile/ProfileProvider.tsx
+++ b/src/routes/viewProfile/ProfileProvider.tsx
@@ -1,13 +1,23 @@
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, UserType} from '../../types';
+import {
+ ProfileType,
+ SocialAccountType,
+ ProfilePreviewType,
+ UserType,
+ MomentType,
+} from '../../types';
interface ProfileContextProps {
user: UserType;
@@ -19,6 +29,11 @@ interface ProfileContextProps {
updateMoments: (value: boolean) => void;
socialAccounts: Record<string, SocialAccountType>;
socialsNeedUpdate: (_: string[]) => void;
+ moments: MomentType[];
+ followers: ProfilePreviewType[];
+ following: ProfilePreviewType[];
+ followersNeedUpdate: boolean;
+ updateFollowers: (value: boolean) => void;
}
const NO_USER: UserType = {
userId: '',
@@ -46,6 +61,11 @@ export const ProfileContext = createContext<ProfileContextProps>({
updateMoments: () => {},
socialAccounts: NO_SOCIAL_ACCOUNTS,
socialsNeedUpdate: () => {},
+ moments: [],
+ followers: [],
+ following: [],
+ followersNeedUpdate: true,
+ updateFollowers: () => {},
});
/**
@@ -60,6 +80,10 @@ const ProfileProvider: React.FC = ({children}) => {
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,
@@ -70,7 +94,6 @@ const ProfileProvider: React.FC = ({children}) => {
if (!userId) {
return;
}
-
const loadData = async () => {
try {
const token = await AsyncStorage.getItem('token');
@@ -89,6 +112,48 @@ const ProfileProvider: React.FC = ({children}) => {
}, [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) => {
@@ -110,6 +175,10 @@ const ProfileProvider: React.FC = ({children}) => {
cover,
newMomentsAvailable,
socialAccounts,
+ moments,
+ followers,
+ following,
+ followersNeedUpdate,
loadProfile: (id, username) => {
setUser({...user, userId: id, username});
},
@@ -119,6 +188,9 @@ const ProfileProvider: React.FC = ({children}) => {
socialsNeedUpdate: (socials: string[]) => {
setSocialsNeedUpdate(socials);
},
+ updateFollowers: (value) => {
+ setFollowersNeedUpdate(value);
+ },
}}>
{children}
</ProfileContext.Provider>