aboutsummaryrefslogtreecommitdiff
path: root/src/services/UserProfileService.ts
diff options
context:
space:
mode:
authorIvan Chen <ivan@tagg.id>2021-03-25 15:00:48 -0400
committerGitHub <noreply@github.com>2021-03-25 15:00:48 -0400
commit99de9c8402f470ead242a81510dc2764ae7d9e66 (patch)
tree7bf2feb0f1a57f1c695ddea51afb9e95abf03f0a /src/services/UserProfileService.ts
parent75c08439fe04666a63431bf56e4b2fa7fb05cbbe (diff)
parent54de628b27647099170bc6d5eea7110c8dd5e8f0 (diff)
Merge pull request #323 from IvanIFChen/tma698-api-profile
[TMA-698] api/profile
Diffstat (limited to 'src/services/UserProfileService.ts')
-rw-r--r--src/services/UserProfileService.ts114
1 files changed, 46 insertions, 68 deletions
diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts
index dd77db9f..e00c0530 100644
--- a/src/services/UserProfileService.ts
+++ b/src/services/UserProfileService.ts
@@ -1,7 +1,6 @@
import AsyncStorage from '@react-native-community/async-storage';
import moment from 'moment';
import {Alert} from 'react-native';
-import RNFetchBlob from 'rn-fetch-blob';
import {
GET_FB_POSTS_ENDPOINT,
GET_IG_POSTS_ENDPOINT,
@@ -10,10 +9,10 @@ import {
PASSWORD_RESET_ENDPOINT,
PROFILE_INFO_ENDPOINT,
PROFILE_PHOTO_ENDPOINT,
- PROFILE_PHOTO_THUMBNAIL_ENDPOINT,
REGISTER_ENDPOINT,
SEND_OTP_ENDPOINT,
TAGG_CUSTOMER_SUPPORT,
+ USER_PROFILE_ENDPOINT,
VERIFY_OTP_ENDPOINT,
} from '../constants';
import {
@@ -27,7 +26,7 @@ import {
SUCCESS_PWD_RESET,
SUCCESS_VERIFICATION_CODE_SENT,
} from '../constants/strings';
-import {SocialAccountType} from '../types';
+import {ProfileInfoType, ProfileType, SocialAccountType} from '../types';
export const loadProfileInfo = async (token: string, userId: string) => {
try {
@@ -39,35 +38,11 @@ export const loadProfileInfo = async (token: string, userId: string) => {
});
const status = response.status;
if (status === 200) {
- const info = await response.json();
- let {
- name,
- biography,
- website,
- birthday,
- gender,
- snapchat,
- tiktok,
- university_class,
- profile_completion_stage,
- suggested_people_linked,
- friendship_status,
- friendship_requester_id,
- } = info;
- birthday = birthday && moment(birthday).format('YYYY-MM-DD');
+ const data: ProfileInfoType = await response.json();
+ const birthday = data.birthday;
return {
- name,
- biography,
- website,
- birthday,
- gender,
- snapchat,
- tiktok,
- university_class,
- profile_completion_stage,
- suggested_people_linked,
- friendship_status,
- friendship_requester_id,
+ ...data,
+ birthday: birthday && moment(birthday).format('YYYY-MM-DD'),
};
} else {
throw 'Unable to load profile data';
@@ -77,43 +52,22 @@ export const loadProfileInfo = async (token: string, userId: string) => {
}
};
-export const loadAvatar = async (userId: string, thumbnail: boolean) => {
- try {
- const token = await AsyncStorage.getItem('token');
- const url = thumbnail
- ? PROFILE_PHOTO_THUMBNAIL_ENDPOINT
- : PROFILE_PHOTO_ENDPOINT;
- const response = await RNFetchBlob.config({
- fileCache: true,
- appendExt: 'jpg',
- }).fetch('GET', url + `${userId}/`, {
- Authorization: 'Token ' + token,
- });
- const status = response.info().status;
- if (status === 200) {
- return response.path();
- } else {
- return '';
- }
- } catch (error) {
- console.log(error);
- return '';
- }
-};
-
-export const loadCover = async (token: string, userId: string) => {
+export const getProfilePic = async (
+ token: string,
+ userId: string,
+ type: 'profile' | 'header',
+) => {
try {
- let response = await RNFetchBlob.config({
- fileCache: true,
- appendExt: 'jpg',
- }).fetch('GET', HEADER_PHOTO_ENDPOINT + `${userId}/`, {
- Authorization: 'Token ' + token,
+ const url =
+ type === 'profile' ? PROFILE_PHOTO_ENDPOINT : HEADER_PHOTO_ENDPOINT;
+ const response = await fetch(url + `${userId}/`, {
+ method: 'GET',
+ headers: {
+ Authorization: 'Token ' + token,
+ },
});
- const status = response.info().status;
- if (status === 200) {
- return response.path();
- } else {
- return '';
+ if (response.status === 200) {
+ return (await response.json()).url;
}
} catch (error) {
console.log(error);
@@ -129,8 +83,11 @@ const integratedSocialPostsEndpoints: {[social: string]: string} = {
export const loadSocialPosts: (
userId: string,
socialType: string,
-) => Promise<SocialAccountType> = async (userId, socialType) => {
- const token = await AsyncStorage.getItem('token');
+ token?: string,
+) => Promise<SocialAccountType> = async (userId, socialType, token) => {
+ if (!token) {
+ token = (await AsyncStorage.getItem('token')) ?? '';
+ }
const endpoint = integratedSocialPostsEndpoints[socialType];
const accountData: SocialAccountType = {};
accountData.posts = [];
@@ -356,3 +313,24 @@ export const sendRegister = async (
return undefined;
}
};
+
+export const fetchUserProfile = async (userId: string, token?: string) => {
+ try {
+ if (!token) {
+ token = (await AsyncStorage.getItem('token')) ?? '';
+ }
+ const response = await fetch(USER_PROFILE_ENDPOINT + userId + '/', {
+ method: 'GET',
+ headers: {
+ Authorization: 'Token ' + token,
+ },
+ });
+ if (response.status === 200) {
+ const data: ProfileType = await response.json();
+ return data;
+ }
+ } catch (error) {
+ console.log(error);
+ return undefined;
+ }
+};