diff options
author | Ashm Walia <40498934+ashmgarv@users.noreply.github.com> | 2020-10-18 16:37:32 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-18 19:37:32 -0400 |
commit | ab7fa09af967e0a8cf2ca53dfb24f8bc8a6886f7 (patch) | |
tree | 898e7aa42529eda91964ac1a18aa1881689554f2 /src/services/UserProfileService.ts | |
parent | 79d237f616c37940f5d476eb1dca6b5d05cf148a (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/services/UserProfileService.ts')
-rw-r--r-- | src/services/UserProfileService.ts | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts new file mode 100644 index 00000000..4c3af06a --- /dev/null +++ b/src/services/UserProfileService.ts @@ -0,0 +1,121 @@ +//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, +} from '../constants'; + +import AsyncStorage from '@react-native-community/async-storage'; +import RNFetchBlob from 'rn-fetch-blob'; + +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 loadInstaPosts = async ( + token: string, + userId: string, + callback: Function, +) => { + 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(); + callback(ig_posts); + } else { + callback([]); + } + } 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?", + ); + } +}; + +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); + } +}; |