aboutsummaryrefslogtreecommitdiff
path: root/src/routes/authentication/AuthProvider.tsx
diff options
context:
space:
mode:
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);
}