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; loadProfile: (userId: string, username: string) => void; avatar: string | null; cover: string | null; newMomentsAvailable: boolean; updateMoments: (value: boolean) => void; socialAccounts: Record; 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 = { Instagram: {posts: []}, Facebook: {posts: []}, Twitter: {posts: []}, }; export const ProfileContext = createContext({ user: NO_USER, profile: NO_PROFILE, loadProfile: () => {}, avatar: null, cover: null, newMomentsAvailable: true, updateMoments: () => {}, 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 */ const ProfileProvider: React.FC = ({children}) => { const [user, setUser] = useState(NO_USER); const [profile, setProfile] = useState(NO_PROFILE); const [avatar, setAvatar] = useState(null); const [cover, setCover] = useState(null); const [newMomentsAvailable, setNewMomentsAvailable] = useState(true); const [socialAccounts, setSocialAccounts] = useState< Record >(NO_SOCIAL_ACCOUNTS); const [moments, setMoments] = useState>([]); const [followers, setFollowers] = useState>([]); const [following, setFollowing] = useState>([]); const [followersNeedUpdate, setFollowersNeedUpdate] = useState(true); // Default update all integrated social lists on start const [socialsNeedUpdate, setSocialsNeedUpdate] = useState([ ...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) => { socialAccounts[social] = accountData; setSocialAccounts(socialAccounts); console.log('Updated posts data', social); }); } setSocialsNeedUpdate([]); } }, [socialAccounts, socialsNeedUpdate, userId]); return ( { setUser({...user, userId: id, username}); }, updateMoments: (value) => { setNewMomentsAvailable(value); }, socialsNeedUpdate: (socials: string[]) => { setSocialsNeedUpdate(socials); }, updateFollowers: (value) => { setFollowersNeedUpdate(value); }, }}> {children} ); }; export default ProfileProvider;