//Abstracted common profile api calls out here import {Alert} from 'react-native'; import { PROFILE_INFO_ENDPOINT, AVATAR_PHOTO_ENDPOINT, COVER_PHOTO_ENDPOINT, GET_IG_POSTS_ENDPOINT, GET_TWITTER_POSTS_ENDPOINT, } from '../constants'; import AsyncStorage from '@react-native-community/async-storage'; import RNFetchBlob from 'rn-fetch-blob'; import {SocialAccountType} from 'src/types'; export const loadProfileInfo = async ( token: string, userId: string, callback: Function, ) => { 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; callback({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?", ); } }; export const loadAvatar = async ( token: string, userId: string, callback: Function, ) => { 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) { callback(response.path()); } else { callback(''); } } catch (error) { console.log(error); } }; export const loadCover = async ( token: string, userId: string, callback: Function, ) => { 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) { callback(response.path()); } else { callback(''); } } catch (error) { console.log(error); } }; export const loadSocialPosts = async ( token: string, userId: string, socialType: string, endpoint: string, socialAccounts: Record, ) => { try { const response = await fetch(endpoint + `${userId}/`, { method: 'GET', headers: { Authorization: 'Token ' + token, }, }); if (response.status === 200) { const body = await response.json(); socialAccounts[socialType].handle = body.handle; socialAccounts[socialType].posts = body.posts; socialAccounts[socialType].profile_pic = body.profile_pic; } else { throw new Error(await response.json()); } } catch (error) { console.log(error); } return socialAccounts; }; export const loadRecentlySearchedUsers = async (callback: Function) => { try { const asyncCache = await AsyncStorage.getItem('@recently_searched_users'); asyncCache != null ? callback(JSON.parse(asyncCache)) : null; } catch (e) { console.log(e); } };