aboutsummaryrefslogtreecommitdiff
path: root/src/routes/authentication/AuthProvider.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/authentication/AuthProvider.tsx')
-rw-r--r--src/routes/authentication/AuthProvider.tsx60
1 files changed, 28 insertions, 32 deletions
diff --git a/src/routes/authentication/AuthProvider.tsx b/src/routes/authentication/AuthProvider.tsx
index a705f074..5bd4278d 100644
--- a/src/routes/authentication/AuthProvider.tsx
+++ b/src/routes/authentication/AuthProvider.tsx
@@ -1,10 +1,6 @@
import AsyncStorage from '@react-native-community/async-storage';
import React, {createContext, useEffect, useState} from 'react';
-import {
- GET_FB_POSTS_ENDPOINT,
- GET_IG_POSTS_ENDPOINT,
- GET_TWITTER_POSTS_ENDPOINT,
-} from '../../constants';
+import {INTEGRATED_SOCIAL_LIST} from '../../constants';
import {
loadAvatar,
loadCover,
@@ -30,6 +26,7 @@ interface AuthContextProps {
recentSearches: Array<ProfilePreviewType>;
newMomentsAvailable: boolean;
updateMoments: (value: boolean) => void;
+ socialsNeedUpdate: (_: string[]) => void;
}
const NO_USER: UserType = {
@@ -43,11 +40,10 @@ const NO_PROFILE: ProfileType = {
name: '',
};
-// Not necessary, but safer, in case SocialAccountType object is undefined
const NO_SOCIAL_ACCOUNTS: Record<string, SocialAccountType> = {
- Instagram: {},
- Facebook: {},
- Twitter: {},
+ Instagram: {posts: []},
+ Facebook: {posts: []},
+ Twitter: {posts: []},
};
export const AuthContext = createContext<AuthContextProps>({
@@ -57,10 +53,11 @@ export const AuthContext = createContext<AuthContextProps>({
logout: () => {},
avatar: null,
cover: null,
- socialAccounts: NO_SOCIAL_ACCOUNTS,
recentSearches: [],
newMomentsAvailable: true,
updateMoments: () => {},
+ socialAccounts: NO_SOCIAL_ACCOUNTS,
+ socialsNeedUpdate: () => {},
});
/**
@@ -78,6 +75,10 @@ const AuthProvider: React.FC = ({children}) => {
Array<ProfilePreviewType>
>([]);
const [newMomentsAvailable, setNewMomentsAvailable] = 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) {
@@ -95,33 +96,25 @@ const AuthProvider: React.FC = ({children}) => {
loadAvatar(token, userId, setAvatar);
loadCover(token, userId, setCover);
loadRecentlySearchedUsers(setRecentSearches);
- loadSocialPosts(
- token,
- userId,
- 'Instagram',
- GET_IG_POSTS_ENDPOINT,
- socialAccounts,
- ).then((newSocialAccounts) => setSocialAccounts(newSocialAccounts));
- loadSocialPosts(
- token,
- userId,
- 'Facebook',
- GET_FB_POSTS_ENDPOINT,
- socialAccounts,
- ).then((newSocialAccounts) => setSocialAccounts(newSocialAccounts));
- loadSocialPosts(
- token,
- userId,
- 'Twitter',
- GET_TWITTER_POSTS_ENDPOINT,
- socialAccounts,
- ).then((newSocialAccounts) => setSocialAccounts(newSocialAccounts));
} catch (err) {
console.log(err);
}
};
loadData();
- }, [socialAccounts, userId]);
+ }, [userId]);
+
+ useEffect(() => {
+ if (socialsNeedUpdate.length > 0 && userId) {
+ for (let social of socialsNeedUpdate) {
+ loadSocialPosts(userId, social).then((accountData) => {
+ socialAccounts[social] = accountData;
+ setSocialAccounts(socialAccounts);
+ console.log('Updated posts data', social);
+ });
+ }
+ setSocialsNeedUpdate([]);
+ }
+ }, [socialAccounts, socialsNeedUpdate, userId]);
return (
<AuthContext.Provider
@@ -150,6 +143,9 @@ const AuthProvider: React.FC = ({children}) => {
updateMoments: (value) => {
setNewMomentsAvailable(value);
},
+ socialsNeedUpdate: (socials: string[]) => {
+ setSocialsNeedUpdate(socials);
+ },
}}>
{children}
</AuthContext.Provider>