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/authentication/AuthProvider.tsx | 115 +++-------------------------- 1 file changed, 12 insertions(+), 103 deletions(-) (limited to 'src/routes/authentication') diff --git a/src/routes/authentication/AuthProvider.tsx b/src/routes/authentication/AuthProvider.tsx index e5956eb2..6f577a73 100644 --- a/src/routes/authentication/AuthProvider.tsx +++ b/src/routes/authentication/AuthProvider.tsx @@ -1,20 +1,19 @@ import React, {useEffect} from 'react'; import {createContext, useState} from 'react'; -import RNFetchBlob from 'rn-fetch-blob'; -import AsyncStorage from '@react-native-community/async-storage'; import { UserType, ProfileType, InstagramPostType, ProfilePreviewType, } from '../../types'; +import AsyncStorage from '@react-native-community/async-storage'; import { - PROFILE_INFO_ENDPOINT, - AVATAR_PHOTO_ENDPOINT, - COVER_PHOTO_ENDPOINT, - GET_IG_POSTS_ENDPOINT, -} from '../../constants'; -import {Alert} from 'react-native'; + loadProfileInfo, + loadAvatar, + loadCover, + loadInstaPosts, + loadRecentlySearchedUsers, +} from '../../services'; interface AuthContextProps { user: UserType; @@ -63,96 +62,6 @@ const AuthProvider: React.FC = ({children}) => { if (!userId) { return; } - const loadProfileInfo = async (token: string) => { - try { - const response = await fetch(PROFILE_INFO_ENDPOINT + `${userId}/`, { - method: 'GET', - headers: { - Authorization: 'Token ' + token, - }, - }); - const status = response.status; - if (status === 200) { - const info = await response.json(); - let {name, biography, website} = info; - setProfile({name, biography, website}); - } - } catch (error) { - Alert.alert( - 'Something went wrong! 😭', - "Would you believe me if I told you that I don't know what happened?", - ); - } - }; - const loadAvatar = async (token: string) => { - try { - const response = await RNFetchBlob.config({ - fileCache: true, - appendExt: 'jpg', - }).fetch('GET', AVATAR_PHOTO_ENDPOINT + `${userId}/`, { - Authorization: 'Token ' + token, - }); - const status = response.info().status; - if (status === 200) { - setAvatar(response.path()); - } else { - setAvatar(''); - } - } catch (error) { - console.log(error); - } - }; - const loadCover = async (token: string) => { - try { - let response = await RNFetchBlob.config({ - fileCache: true, - appendExt: 'jpg', - }).fetch('GET', COVER_PHOTO_ENDPOINT + `${userId}/`, { - Authorization: 'Token ' + token, - }); - const status = response.info().status; - if (status === 200) { - setCover(response.path()); - } else { - setCover(''); - } - } catch (error) { - console.log(error); - } - }; - const loadInstaPosts = async (token: string) => { - try { - const response = await fetch(GET_IG_POSTS_ENDPOINT + `${userId}/`, { - method: 'GET', - headers: { - Authorization: 'Token ' + token, - }, - }); - const status = response.status; - if (status === 200) { - let ig_posts = await response.json(); - setInstaPosts(ig_posts); - } else { - setInstaPosts([]); - } - } catch (error) { - console.log(error); - Alert.alert( - 'Something went wrong! 😭', - "Would you believe me if I told you that I don't know what happened?", - ); - } - }; - const loadRecentlySearchedUsers = async () => { - try { - const asyncCache = await AsyncStorage.getItem( - '@recently_searched_users', - ); - asyncCache != null ? setRecentSearches(JSON.parse(asyncCache)) : null; - } catch (e) { - console.log(e); - } - }; const loadData = async () => { try { @@ -161,11 +70,11 @@ const AuthProvider: React.FC = ({children}) => { setUser(NO_USER); return; } - loadProfileInfo(token); - loadAvatar(token); - loadCover(token); - loadInstaPosts(token); - loadRecentlySearchedUsers(); + loadProfileInfo(token, userId, setProfile); + loadAvatar(token, userId, setAvatar); + loadCover(token, userId, setCover); + loadInstaPosts(token, userId, setInstaPosts); + loadRecentlySearchedUsers(setRecentSearches); } catch (err) { console.log(err); } -- cgit v1.2.3-70-g09d2