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/viewProfile/ProfileProvider.tsx | 207 ----------------------------- src/routes/viewProfile/index.ts | 2 - 2 files changed, 209 deletions(-) delete mode 100644 src/routes/viewProfile/ProfileProvider.tsx delete mode 100644 src/routes/viewProfile/index.ts (limited to 'src/routes/viewProfile') diff --git a/src/routes/viewProfile/ProfileProvider.tsx b/src/routes/viewProfile/ProfileProvider.tsx deleted file mode 100644 index f2d27a84..00000000 --- a/src/routes/viewProfile/ProfileProvider.tsx +++ /dev/null @@ -1,207 +0,0 @@ -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; - avatar: string | null; - cover: string | null; - 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, - avatar: null, - cover: null, - 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 - * The ProfileProviderProps is used to initialise data as soon as the component is initialised. - */ - -type ProfileProviderProps = { - uname: string; - uId: string; -}; - -const ProfileProvider: React.FC = ({ - children, - uId, - uname, -}) => { - const [user, setUser] = useState({userId: uId, username: uname}); - 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) => { - /** - * Please use the following syntax when updating an object, fixing this problem broke our head. LOLs - * ref1 : https://stackoverflow.com/questions/56423256/set-dynamic-key-in-state-via-usestate-react-hooks - * ref2: https://stackoverflow.com/questions/43638938/updating-an-object-with-setstate-in-react/43639228 - * The spread operator {...} helps us make a simple copy of the object - * And form there on we can use the [] to specify the dynamically constructed key and set its value. - */ - setSocialAccounts((prevSocialAccounts) => ({ - ...prevSocialAccounts, - [social]: accountData, - })); - console.log('Updated posts data', social); - }); - } - setSocialsNeedUpdate([]); - } - }, [socialAccounts, socialsNeedUpdate, userId]); - - return ( - { - setSocialsNeedUpdate(socials); - }, - updateFollowers: (value) => { - setFollowersNeedUpdate(value); - }, - }}> - {children} - - ); -}; - -export default ProfileProvider; diff --git a/src/routes/viewProfile/index.ts b/src/routes/viewProfile/index.ts deleted file mode 100644 index 7035ce4a..00000000 --- a/src/routes/viewProfile/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './ProfileProvider'; -export {default} from './ProfileProvider'; -- cgit v1.2.3-70-g09d2