aboutsummaryrefslogtreecommitdiff
path: root/src/routes/authentication/AuthProvider.tsx
diff options
context:
space:
mode:
authorAshm Walia <40498934+ashmgarv@users.noreply.github.com>2020-10-18 16:37:32 -0700
committerGitHub <noreply@github.com>2020-10-18 19:37:32 -0400
commitab7fa09af967e0a8cf2ca53dfb24f8bc8a6886f7 (patch)
tree898e7aa42529eda91964ac1a18aa1881689554f2 /src/routes/authentication/AuthProvider.tsx
parent79d237f616c37940f5d476eb1dca6b5d05cf148a (diff)
[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 <ivan@thetaggid.com>
Diffstat (limited to 'src/routes/authentication/AuthProvider.tsx')
-rw-r--r--src/routes/authentication/AuthProvider.tsx115
1 files changed, 12 insertions, 103 deletions
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);
}