import AsyncStorage from '@react-native-community/async-storage'; import moment from 'moment'; import {Alert} from 'react-native'; import RNFetchBlob from 'rn-fetch-blob'; import {SocialAccountType, UserType} from '../types'; import { PROFILE_PHOTO_ENDPOINT, HEADER_PHOTO_ENDPOINT, GET_FB_POSTS_ENDPOINT, GET_IG_POSTS_ENDPOINT, GET_TWITTER_POSTS_ENDPOINT, PROFILE_INFO_ENDPOINT, PASSWORD_RESET_ENDPOINT, TAGG_CUSTOMER_SUPPORT, VERIFY_OTP_ENDPOINT, SEND_OTP_ENDPOINT, PROFILE_PHOTO_THUMBNAIL_ENDPOINT, EDIT_PROFILE_ENDPOINT, } from '../constants'; import { ERROR_DOUBLE_CHECK_CONNECTION, ERROR_DUP_OLD_PWD, ERROR_INVALID_PWD_CODE, ERROR_PWD_ACCOUNT, ERROR_SOMETHING_WENT_WRONG, ERROR_SOMETHING_WENT_WRONG_REFRESH, ERROR_VERIFICATION_FAILED_SHORT, SUCCESS_PWD_RESET, SUCCESS_VERIFICATION_CODE_SENT, } from '../constants/strings'; export const loadProfileInfo = async (token: string, userId: string) => { 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, birthday, gender, snapchat, tiktok, university_class, profile_completion_stage, sp_swipe_tutorial, friendship_status, friendship_requester_id, } = info; birthday = birthday && moment(birthday).format('YYYY-MM-DD'); console.log( 'Suggested People loaded from backend for logged in user: ', sp_swipe_tutorial, ); return { name, biography, website, birthday, gender, snapchat, tiktok, university_class, profile_completion_stage, sp_swipe_tutorial, friendship_status, friendship_requester_id, }; } else { throw 'Unable to load profile data'; } } catch (error) { Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); } }; 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) => { try { let response = await RNFetchBlob.config({ fileCache: true, appendExt: 'jpg', }).fetch('GET', HEADER_PHOTO_ENDPOINT + `${userId}/`, { Authorization: 'Token ' + token, }); const status = response.info().status; if (status === 200) { return response.path(); } else { return ''; } } catch (error) { console.log(error); } }; 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, ) => Promise = async (userId, socialType) => { const token = await AsyncStorage.getItem('token'); const endpoint = integratedSocialPostsEndpoints[socialType]; const accountData: SocialAccountType = {}; accountData.posts = []; try { const response = await fetch(endpoint + `${userId}/`, { method: 'GET', headers: { Authorization: 'Token ' + token, }, }); if (response.status === 200) { const body = await response.json(); accountData.handle = body.handle; accountData.posts = body.posts; accountData.profile_pic = body.profile_pic; } else { throw 'Unable to fetch posts data from ' + socialType; } } catch (error) { console.log(error); } return accountData; }; export const loadRecentlySearchedUsers = async () => { try { const asyncCache = await AsyncStorage.getItem('@recently_searched_users'); return asyncCache != null ? JSON.parse(asyncCache) : null; } catch (e) { console.log(e); } }; export const handlePasswordResetRequest = async (value: string) => { try { const response = await fetch(PASSWORD_RESET_ENDPOINT + 'request/', { method: 'POST', body: JSON.stringify({ value, }), }); const status = response.status; if (status === 200) { Alert.alert( 'A code was sent to your registered phone number, please use the same to reset your password', ); return true; } else { if (status == 404) { Alert.alert( `Please make sure that the email / username entered is registered with us. You may contact our customer support at ${TAGG_CUSTOMER_SUPPORT}`, ); } else { Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); } console.log(response); return false; } } catch (error) { console.log(error); Alert.alert(ERROR_SOMETHING_WENT_WRONG); return false; } }; export const handlePasswordCodeVerification = async ( value: string, otp: string, ) => { try { const response = await fetch(PASSWORD_RESET_ENDPOINT + 'verify/', { method: 'POST', body: JSON.stringify({ value, otp, }), }); const status = response.status; if (status === 200) { return true; } else { if (status == 404) { Alert.alert(ERROR_PWD_ACCOUNT(TAGG_CUSTOMER_SUPPORT)); } else if (status === 401) { Alert.alert(ERROR_INVALID_PWD_CODE); } else { Alert.alert(ERROR_SOMETHING_WENT_WRONG); } console.log(response); return false; } } catch (error) { console.log(error); Alert.alert(ERROR_SOMETHING_WENT_WRONG); return false; } }; export const handlePasswordReset = async (value: string, password: string) => { try { const response = await fetch(PASSWORD_RESET_ENDPOINT + 'reset/', { method: 'POST', body: JSON.stringify({ value, password, }), }); const status = response.status; if (status === 200) { Alert.alert(SUCCESS_PWD_RESET); return true; } else { if (status == 404) { Alert.alert(ERROR_PWD_ACCOUNT(TAGG_CUSTOMER_SUPPORT)); } else if (status == 406) { Alert.alert(ERROR_DUP_OLD_PWD); } else { Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); } console.log(response); return false; } } catch (error) { console.log(error); Alert.alert(ERROR_SOMETHING_WENT_WRONG); return false; } }; export const verifyOtp = async (phone: string, otp: string) => { try { let response = await fetch(VERIFY_OTP_ENDPOINT, { method: 'POST', body: JSON.stringify({ phone_number: '+1' + phone, otp, }), }); let statusCode = response.status; if (statusCode === 200) { return true; } else { if (statusCode === 401) { Alert.alert( 'Invalid verification code 🤔', 'Try again. Tap the resend code button if you need a new code.', ); } else { Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); } } } catch (error) { Alert.alert(ERROR_VERIFICATION_FAILED_SHORT, ERROR_DOUBLE_CHECK_CONNECTION); return { name: 'Verification error', description: error, }; } }; export const sendOtp = async (phone: string) => { try { console.log(phone); let response = await fetch(SEND_OTP_ENDPOINT, { method: 'POST', body: JSON.stringify({ phone_number: '+1' + phone, }), }); let status = response.status; if (status === 200) { Alert.alert(SUCCESS_VERIFICATION_CODE_SENT); return true; } else { Alert.alert(ERROR_SOMETHING_WENT_WRONG); return false; } } catch (error) { console.log(error); return false; } }; export const editSPSwipeTutorial = async (user: UserType) => { try { const request = new FormData(); request.append('sp_swipe_tutorial', 1); const endpoint = EDIT_PROFILE_ENDPOINT + `${user.userId}/`; const token = await AsyncStorage.getItem('token'); let response = await fetch(endpoint, { method: 'PATCH', headers: { 'Content-Type': 'multipart/form-data', Authorization: 'Token ' + token, }, body: request, }); if (response.status === 200) { return true; } else { return false; } } catch (error) { console.log('Error updating animated tutorial close button press'); } };