diff options
Diffstat (limited to 'src/services/UserProfileService.ts')
-rw-r--r-- | src/services/UserProfileService.ts | 130 |
1 files changed, 54 insertions, 76 deletions
diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts index a5b42814..085787c3 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 { EDIT_PROFILE_ENDPOINT, GET_FB_POSTS_ENDPOINT, @@ -11,10 +10,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 { @@ -28,7 +27,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 { @@ -40,35 +39,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'; @@ -78,43 +53,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); @@ -130,8 +84,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 = []; @@ -340,19 +297,19 @@ export const sendRegister = async ( password: string, ) => { try { - const form = new FormData() - form.append('first_name', firstName) - form.append('last_name', lastName) - form.append('email', email) - form.append('phone_number', phone) - form.append('username', username) - form.append('password', password) + const form = new FormData(); + form.append('first_name', firstName); + form.append('last_name', lastName); + form.append('email', email); + form.append('phone_number', phone); + form.append('username', username); + form.append('password', password); const response = await fetch(REGISTER_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'multipart/form-data', }, - body: form + body: form, }); return response; } catch (error) { @@ -361,6 +318,27 @@ export const sendRegister = async ( } }; +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; + } +}; + export const patchEditProfile = async (form: FormData, userId: string) => { const endpoint = EDIT_PROFILE_ENDPOINT + `${userId}/`; try { |