From 0fd892ad288f2e1eaaa4fdf5e1fd6f15dbd45860 Mon Sep 17 00:00:00 2001 From: Ashm Walia <40498934+ashmgarv@users.noreply.github.com> Date: Fri, 4 Dec 2020 08:50:24 -0800 Subject: [TMA - 398 AND TMA-430] Replace Providers with Redux Store (#125) * First * WIP * Thunk * Some more comments * sc * recent searches and follounfollow * Edit profile dummy * Block / unblock and some cleanup * Replace auth provider * Sc * Delete AP after rebase * Discover users * Cleanup * More cleanup * Replace profile provider * Fixed build failure * Fixed a bug reported * Prevent app crash when backend server is down --- src/routes/authentication/AuthProvider.tsx | 315 ----------------------------- src/routes/authentication/index.ts | 2 - 2 files changed, 317 deletions(-) delete mode 100644 src/routes/authentication/AuthProvider.tsx delete mode 100644 src/routes/authentication/index.ts (limited to 'src/routes/authentication') diff --git a/src/routes/authentication/AuthProvider.tsx b/src/routes/authentication/AuthProvider.tsx deleted file mode 100644 index 46f761e1..00000000 --- a/src/routes/authentication/AuthProvider.tsx +++ /dev/null @@ -1,315 +0,0 @@ -import AsyncStorage from '@react-native-community/async-storage'; -import React, {createContext, useEffect, useState} from 'react'; -import {INTEGRATED_SOCIAL_LIST} from '../../constants'; -import { - loadAvatar, - loadCover, - loadFollowers, - loadFollowing, - loadMoments, - loadProfileInfo, - loadRecentlySearchedUsers, - loadSocialPosts, - getAllTaggUsers, - loadBlockedUsers, -} from '../../services'; -import { - MomentType, - ProfilePreviewType, - ProfileType, - SocialAccountType, - UserType, -} from '../../types'; - -interface AuthContextProps { - user: UserType; - profile: ProfileType; - login: (userId: string, username: string) => void; - logout: () => void; - avatar: string | null; - cover: string | null; - socialAccounts: Record; - recentSearches: Array; - taggUsers: Array; - newMomentsAvailable: boolean; - updateMoments: (value: boolean) => void; - socialsNeedUpdate: (_: string[]) => void; - moments: MomentType[]; - followers: ProfilePreviewType[]; - following: ProfilePreviewType[]; - followersNeedUpdate: boolean; - updateFollowers: (value: boolean) => void; - blockedUsers: ProfilePreviewType[]; - blockedUsersNeedUpdate: boolean; - updateBlockedUsers: (value: boolean) => void; - isEditedProfile: boolean; - updateIsEditedProfile: (value: boolean) => void; -} - -const NO_USER: UserType = { - userId: '', - username: '', -}; - -const NO_PROFILE: ProfileType = { - biography: '', - website: '', - name: '', - gender: '', - birthday: undefined, -}; - -const NO_SOCIAL_ACCOUNTS: Record = { - Instagram: {posts: []}, - Facebook: {posts: []}, - Twitter: {posts: []}, -}; - -export const AuthContext = createContext({ - user: NO_USER, - profile: NO_PROFILE, - login: () => {}, - logout: () => {}, - avatar: null, - cover: null, - recentSearches: [], - taggUsers: [], - newMomentsAvailable: true, - updateMoments: () => {}, - socialAccounts: NO_SOCIAL_ACCOUNTS, - socialsNeedUpdate: () => {}, - moments: [], - followers: [], - following: [], - followersNeedUpdate: true, - updateFollowers: () => {}, - blockedUsers: [], - blockedUsersNeedUpdate: true, - updateBlockedUsers: () => {}, - isEditedProfile: false, - updateIsEditedProfile: () => {}, -}); - -/** - * Authentication provider for the application. - */ -const AuthProvider: 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 [socialAccounts, setSocialAccounts] = useState< - Record - >(NO_SOCIAL_ACCOUNTS); - const [recentSearches, setRecentSearches] = useState< - Array - >([]); - const [taggUsers, setTaggUsers] = useState>([]); - const [newMomentsAvailable, setNewMomentsAvailable] = useState(true); - // Default update all integrated social lists on start - const [socialsNeedUpdate, setSocialsNeedUpdate] = useState([ - ...INTEGRATED_SOCIAL_LIST, - ]); - const [moments, setMoments] = useState>([]); - const [followers, setFollowers] = useState>([]); - const [following, setFollowing] = useState>([]); - const [followersNeedUpdate, setFollowersNeedUpdate] = useState(true); - const [blockedUsers, setBlockedUsers] = useState>( - [], - ); - const [blockedUsersNeedUpdate, setBlockedUsersNeedUpdate] = useState( - true, - ); - const [isEditedProfile, setIsEditedProfile] = useState(false); - const {userId} = user; - - useEffect(() => { - const loadUserInfoFromStorage = async () => { - const [id, username, token] = await Promise.all([ - AsyncStorage.getItem('userId'), - AsyncStorage.getItem('username'), - AsyncStorage.getItem('token'), - ]); - if (id && username && token) { - setUser({...user, userId: id, username}); - } - }; - if (user === NO_USER) { - loadUserInfoFromStorage(); - } - }, [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); - loadRecentlySearchedUsers(setRecentSearches); - } catch (err) { - console.log(err); - } - }; - loadData(); - }, [userId, isEditedProfile]); - - 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, moments]); - - 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('Refreshed posts data:', social); - }); - } - setSocialsNeedUpdate([]); - } - }, [socialAccounts, socialsNeedUpdate, userId]); - - useEffect(() => { - const loadNewBlockedUsers = async () => { - try { - const token = await AsyncStorage.getItem('token'); - if (!token) { - setUser(NO_USER); - return; - } - loadBlockedUsers(userId, token, setBlockedUsers); - setBlockedUsersNeedUpdate(false); - } catch (error) { - console.log(error); - } - }; - if (blockedUsersNeedUpdate && userId) { - loadNewBlockedUsers(); - } - }, [ - setBlockedUsersNeedUpdate, - blockedUsersNeedUpdate, - userId, - setBlockedUsers, - ]); - - useEffect(() => { - const loadTaggUsers = async () => { - try { - const token = await AsyncStorage.getItem('token'); - if (!token) { - setUser(NO_USER); - return; - } - await getAllTaggUsers(token, setTaggUsers); - } catch (error) { - console.log(error); - } - }; - loadTaggUsers(); - }, [userId]); - - return ( - { - setUser({...user, userId: id, username}); - }, - logout: () => { - try { - new Promise(() => { - AsyncStorage.removeItem('token'); - AsyncStorage.removeItem('userId'); - AsyncStorage.removeItem('username'); - }).then(() => { - setUser(NO_USER); - }); - } catch (err) { - console.log(err); - } - }, - recentSearches, - taggUsers, - updateMoments: (value) => { - setNewMomentsAvailable(value); - }, - socialsNeedUpdate: (socials: string[]) => { - setSocialsNeedUpdate(socials); - }, - updateFollowers: (value) => { - setFollowersNeedUpdate(value); - }, - updateBlockedUsers: (value) => { - setBlockedUsersNeedUpdate(value); - }, - updateIsEditedProfile: (value: boolean) => { - setIsEditedProfile(value); - }, - }}> - {children} - - ); -}; - -export default AuthProvider; diff --git a/src/routes/authentication/index.ts b/src/routes/authentication/index.ts deleted file mode 100644 index 9968ae93..00000000 --- a/src/routes/authentication/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './AuthProvider'; -export {default} from './AuthProvider'; -- cgit v1.2.3-70-g09d2