From 9da19cdcb6c7596d60afde6d0d559f06a24a0627 Mon Sep 17 00:00:00 2001 From: George Rusu <56009869+grusu6928@users.noreply.github.com> Date: Sun, 25 Oct 2020 15:21:38 -0700 Subject: [TMA-237] Added modal for user registration and redirect (#61) * move async-storage * removed lock files * added lock files to gitignore * added the wrong file to gitignore * added modal for user registration and redirect * api call to get list of linked socials for each user to display appropriate icon * fixed indentation and linting * refactored modal and browser sign-in * now dynamically adding linked and unlinked taggs, added a bunch of TODOs for tomorrow * added svg icons * done? finished taggs bar UI and all the navigations including modal * fixed some bugs and added more TODOs * fixed some bugs and refactored posts fetching logic * fixed taggs bar bug * done, it will update everything correctly * added comments * added tiktok Co-authored-by: hsalhab Co-authored-by: george Co-authored-by: Ivan Chen --- src/routes/authentication/AuthProvider.tsx | 60 ++++++++++++++---------------- src/routes/viewProfile/ProfileProvider.tsx | 49 ++++++++++++------------ 2 files changed, 53 insertions(+), 56 deletions(-) (limited to 'src/routes') 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; 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 = { - Instagram: {}, - Facebook: {}, - Twitter: {}, + Instagram: {posts: []}, + Facebook: {posts: []}, + Twitter: {posts: []}, }; export const AuthContext = createContext({ @@ -57,10 +53,11 @@ export const AuthContext = createContext({ 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 >([]); const [newMomentsAvailable, setNewMomentsAvailable] = useState(true); + // Default update all integrated social lists on start + const [socialsNeedUpdate, setSocialsNeedUpdate] = useState([ + ...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 ( { updateMoments: (value) => { setNewMomentsAvailable(value); }, + socialsNeedUpdate: (socials: string[]) => { + setSocialsNeedUpdate(socials); + }, }}> {children} 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; + 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 = { - Instagram: {}, - Facebook: {}, - Twitter: {}, + Instagram: {posts: []}, + Facebook: {posts: []}, + Twitter: {posts: []}, }; export const ProfileContext = createContext({ @@ -48,6 +45,7 @@ export const ProfileContext = createContext({ newMomentsAvailable: true, updateMoments: () => {}, socialAccounts: NO_SOCIAL_ACCOUNTS, + socialsNeedUpdate: () => {}, }); /** @@ -59,10 +57,14 @@ const ProfileProvider: React.FC = ({children}) => { const [avatar, setAvatar] = useState(null); const [cover, setCover] = useState(null); const [newMomentsAvailable, setNewMomentsAvailable] = useState(true); - const [socialAccounts, setSocialAccounts] = useState< Record >(NO_SOCIAL_ACCOUNTS); + // Default update all integrated social lists on start + const [socialsNeedUpdate, setSocialsNeedUpdate] = useState([ + ...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 (