From ab7fa09af967e0a8cf2ca53dfb24f8bc8a6886f7 Mon Sep 17 00:00:00 2001 From: Ashm Walia <40498934+ashmgarv@users.noreply.github.com> Date: Sun, 18 Oct 2020 16:37:32 -0700 Subject: [TMA 279] Ability to search and view someone's profile (#58) * Batch one : major changes * WIP checkpoint * The one before the final touch * Probable final touch * ran yarn lint D: * linter broke something * fixed a small bug * Addressed a small nitpick * Well abstracted now Co-authored-by: Ivan Chen --- src/routes/viewProfile/ProfileProvider.tsx | 91 ++++++++++++++++++++++++++++++ src/routes/viewProfile/index.ts | 2 + 2 files changed, 93 insertions(+) create mode 100644 src/routes/viewProfile/ProfileProvider.tsx create 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 new file mode 100644 index 00000000..1af7917d --- /dev/null +++ b/src/routes/viewProfile/ProfileProvider.tsx @@ -0,0 +1,91 @@ +import React, {useEffect} from 'react'; +import {createContext, useState} from 'react'; +import AsyncStorage from '@react-native-community/async-storage'; +import {UserType, ProfileType, InstagramPostType} from '../../types'; + +import { + loadProfileInfo, + loadAvatar, + loadCover, + loadInstaPosts, + loadRecentlySearchedUsers, +} from '../../services'; + +interface ProfileContextProps { + user: UserType; + profile: ProfileType; + loadProfile: (userId: string, username: string) => void; + avatar: string | null; + cover: string | null; + instaPosts: Array; +} +const NO_USER: UserType = { + userId: '', + username: '', +}; +const NO_PROFILE: ProfileType = { + biography: '', + website: '', + name: '', +}; +export const ProfileContext = createContext({ + user: NO_USER, + profile: NO_PROFILE, + loadProfile: () => {}, + avatar: null, + cover: null, + instaPosts: [], +}); + +/** + * 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 [instaPosts, setInstaPosts] = useState>([]); + + 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); + loadInstaPosts(token, userId, setInstaPosts); + } catch (err) { + console.log(err); + } + }; + loadData(); + }, [userId]); + + return ( + { + setUser({...user, userId: id, username}); + }, + }}> + {children} + + ); +}; + +export default ProfileProvider; diff --git a/src/routes/viewProfile/index.ts b/src/routes/viewProfile/index.ts new file mode 100644 index 00000000..7035ce4a --- /dev/null +++ b/src/routes/viewProfile/index.ts @@ -0,0 +1,2 @@ +export * from './ProfileProvider'; +export {default} from './ProfileProvider'; -- cgit v1.2.3-70-g09d2