diff options
Diffstat (limited to 'src/services/UserProfileService.ts')
-rw-r--r-- | src/services/UserProfileService.ts | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts index f5523be4..31383f67 100644 --- a/src/services/UserProfileService.ts +++ b/src/services/UserProfileService.ts @@ -1,16 +1,18 @@ //Abstracted common profile api calls out here +import AsyncStorage from '@react-native-community/async-storage'; import {Alert} from 'react-native'; +import RNFetchBlob from 'rn-fetch-blob'; +import {SocialAccountType} from 'src/types'; import { - PROFILE_INFO_ENDPOINT, AVATAR_PHOTO_ENDPOINT, COVER_PHOTO_ENDPOINT, + GET_FB_POSTS_ENDPOINT, + GET_IG_POSTS_ENDPOINT, + GET_TWITTER_POSTS_ENDPOINT, + PROFILE_INFO_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, @@ -83,13 +85,19 @@ export const loadCover = async ( } }; -export const loadSocialPosts = async ( - token: string, +const integratedSocialPostsEndpoints: {[social: string]: string} = { + Facebook: GET_FB_POSTS_ENDPOINT, + Instagram: GET_IG_POSTS_ENDPOINT, + Twitter: GET_TWITTER_POSTS_ENDPOINT, +}; + +export const loadSocialPosts: ( userId: string, socialType: string, - endpoint: string, - socialAccounts: Record<string, SocialAccountType>, -) => { +) => Promise<SocialAccountType> = async (userId, socialType) => { + const token = await AsyncStorage.getItem('token'); + const endpoint = integratedSocialPostsEndpoints[socialType]; + const accountData: SocialAccountType = {}; try { const response = await fetch(endpoint + `${userId}/`, { method: 'GET', @@ -99,16 +107,16 @@ export const loadSocialPosts = async ( }); 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; + accountData.handle = body.handle; + accountData.posts = body.posts; + accountData.profile_pic = body.profile_pic; } else { - throw new Error(await response.json()); + throw 'Unable to fetch posts data from ' + socialType; } } catch (error) { - console.log(error); + console.warn(error); } - return socialAccounts; + return accountData; }; export const loadRecentlySearchedUsers = async (callback: Function) => { |