diff options
Diffstat (limited to 'src/routes/viewProfile/ProfileProvider.tsx')
-rw-r--r-- | src/routes/viewProfile/ProfileProvider.tsx | 49 |
1 files changed, 25 insertions, 24 deletions
diff --git a/src/routes/viewProfile/ProfileProvider.tsx b/src/routes/viewProfile/ProfileProvider.tsx index a4b6cb14..c4942ea0 100644 --- a/src/routes/viewProfile/ProfileProvider.tsx +++ b/src/routes/viewProfile/ProfileProvider.tsx @@ -1,9 +1,6 @@ import AsyncStorage from '@react-native-community/async-storage'; import React, {createContext, useEffect, useState} from 'react'; -import { - GET_IG_POSTS_ENDPOINT, - GET_TWITTER_POSTS_ENDPOINT, -} from '../../constants'; +import {INTEGRATED_SOCIAL_LIST} from '../../constants'; import { loadAvatar, loadCover, @@ -21,6 +18,7 @@ interface ProfileContextProps { newMomentsAvailable: boolean; updateMoments: (value: boolean) => void; socialAccounts: Record<string, SocialAccountType>; + socialsNeedUpdate: (_: string[]) => void; } const NO_USER: UserType = { userId: '', @@ -32,11 +30,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 ProfileContext = createContext<ProfileContextProps>({ @@ -48,6 +45,7 @@ export const ProfileContext = createContext<ProfileContextProps>({ newMomentsAvailable: true, updateMoments: () => {}, socialAccounts: NO_SOCIAL_ACCOUNTS, + socialsNeedUpdate: () => {}, }); /** @@ -59,10 +57,14 @@ const ProfileProvider: React.FC = ({children}) => { 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); + // Default update all integrated social lists on start + const [socialsNeedUpdate, setSocialsNeedUpdate] = useState<string[]>([ + ...INTEGRATED_SOCIAL_LIST, + ]); + const {userId} = user; useEffect(() => { if (!userId) { @@ -79,26 +81,25 @@ const ProfileProvider: React.FC = ({children}) => { loadProfileInfo(token, userId, setProfile); loadAvatar(token, userId, setAvatar); loadCover(token, userId, setCover); - loadSocialPosts( - token, - userId, - 'Instagram', - GET_IG_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 ( <ProfileContext.Provider |