diff options
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/BlockUserService.ts | 23 | ||||
| -rw-r--r-- | src/services/MomentCategoryService.ts | 7 | ||||
| -rw-r--r-- | src/services/MomentServices.ts | 59 | ||||
| -rw-r--r-- | src/services/ReportingService.ts | 10 | ||||
| -rw-r--r-- | src/services/SocialLinkingService.ts | 12 | ||||
| -rw-r--r-- | src/services/UserFriendsServices.ts | 96 | ||||
| -rw-r--r-- | src/services/UserProfileService.ts | 72 |
7 files changed, 186 insertions, 93 deletions
diff --git a/src/services/BlockUserService.ts b/src/services/BlockUserService.ts index 21e259b6..12ea0184 100644 --- a/src/services/BlockUserService.ts +++ b/src/services/BlockUserService.ts @@ -2,6 +2,7 @@ import {Alert} from 'react-native'; import {BLOCK_USER_ENDPOINT} from '../constants'; +import {ERROR_SOMETHING_WENT_WRONG_REFRESH} from '../constants/strings'; export const loadBlockedUsers = async (userId: string, token: string) => { try { @@ -44,18 +45,12 @@ export const blockOrUnblockUser = async ( return true; } else { console.log(await response.json()); - Alert.alert( - 'Something went wrong! ðŸ˜', - "Would you believe me if I told you that I don't know what happened?", - ); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); return false; } } 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?", - ); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); return false; } }; @@ -77,18 +72,12 @@ export const isUserBlocked = async ( if (Math.floor(response.status / 100) === 2) { const data = await response.json(); - return data['is_blocked']; + return data.is_blocked; } else { console.log(await response.json()); - Alert.alert( - 'Something went wrong! ðŸ˜', - "Would you believe me if I told you that I don't know what happened?", - ); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); } } catch (error) { - Alert.alert( - 'Something went wrong! ðŸ˜', - "Would you believe me if I told you that I don't know what happened?", - ); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); } }; diff --git a/src/services/MomentCategoryService.ts b/src/services/MomentCategoryService.ts index 57e64830..bb2c5542 100644 --- a/src/services/MomentCategoryService.ts +++ b/src/services/MomentCategoryService.ts @@ -1,5 +1,6 @@ import {Alert} from 'react-native'; import {MOMENT_CATEGORY_ENDPOINT} from '../constants'; +import {ERROR_CATEGORY_CREATION} from '../constants/strings'; export const loadMomentCategories: ( userId: string, @@ -44,10 +45,10 @@ export const postMomentCategories: ( const status = response.status; const data = await response.json(); if (status === 200) { - return data['profile_completion_stage']; + return data.profile_completion_stage; } else { - Alert.alert('There was a problem updating categories!'); - console.log('Unable to update categories'); + Alert.alert(ERROR_CATEGORY_CREATION); + console.log('Could not post categories!'); } } catch (err) { console.log(err); diff --git a/src/services/MomentServices.ts b/src/services/MomentServices.ts index 91ecf712..514b674c 100644 --- a/src/services/MomentServices.ts +++ b/src/services/MomentServices.ts @@ -1,6 +1,16 @@ import AsyncStorage from '@react-native-community/async-storage'; import {Alert} from 'react-native'; -import {COMMENTS_ENDPOINT, MOMENTS_ENDPOINT} from '../constants'; +import RNFetchBlob from 'rn-fetch-blob'; +import { + COMMENTS_ENDPOINT, + MOMENTS_ENDPOINT, + MOMENT_THUMBNAIL_ENDPOINT, +} from '../constants'; +import { + ERROR_FAILED_TO_COMMENT, + ERROR_UPLOAD, + SUCCESS_PIC_UPLOAD, +} from '../constants/strings'; import {MomentType} from '../types'; import {checkImageUploadStatus} from '../utils'; @@ -48,20 +58,12 @@ export const postMomentComment = async ( }, body: request, }); - const status = response.status; - if (status === 200) { - const response_data = await response.json(); - return response_data; - } else { - Alert.alert('Something went wrong! ðŸ˜', 'Not able to post a comment'); - return {}; + if (response.status !== 200) { + throw 'server error'; } + return await response.json(); } catch (error) { - Alert.alert( - 'Something went wrong! ðŸ˜', - 'Not able to post a comment', - error, - ); + Alert.alert(ERROR_FAILED_TO_COMMENT); return {}; } }; @@ -136,15 +138,15 @@ export const postMoment: ( }); let statusCode = response.status; let data = await response.json(); - if (statusCode === 200 && checkImageUploadStatus(data['moments'])) { - Alert.alert('The picture was uploaded successfully!'); - return data['profile_completion_stage']; + if (statusCode === 200 && checkImageUploadStatus(data.moments)) { + Alert.alert(SUCCESS_PIC_UPLOAD); + return data.profile_completion_stage; } else { - Alert.alert('An error occured while uploading. Please try again!'); + Alert.alert(ERROR_UPLOAD); } } catch (err) { console.log(err); - Alert.alert('An error occured during authenticaion. Please login again!'); + Alert.alert(ERROR_UPLOAD); } return undefined; }; @@ -193,3 +195,24 @@ export const deleteMoment = async (momentId: string) => { return false; } }; + +export const loadMomentThumbnail = async (momentId: string) => { + try { + const token = await AsyncStorage.getItem('token'); + const response = await RNFetchBlob.config({ + fileCache: true, + appendExt: 'jpg', + }).fetch('GET', MOMENT_THUMBNAIL_ENDPOINT + `${momentId}/`, { + Authorization: 'Token ' + token, + }); + const status = response.info().status; + if (status === 200) { + return response.path(); + } else { + return undefined; + } + } catch (error) { + console.log(error); + return undefined; + } +}; diff --git a/src/services/ReportingService.ts b/src/services/ReportingService.ts index 1563d086..8c0a4bfb 100644 --- a/src/services/ReportingService.ts +++ b/src/services/ReportingService.ts @@ -3,6 +3,10 @@ import {REPORT_ISSUE_ENDPOINT} from '../constants'; import {Alert} from 'react-native'; import AsyncStorage from '@react-native-community/async-storage'; +import { + ERROR_SOMETHING_WENT_WRONG, + MARKED_AS_MSG, +} from '../constants/strings'; export const sendReport = async ( moment_id: string, @@ -25,15 +29,15 @@ export const sendReport = async ( let statusCode = response.status; if (statusCode === 200) { - Alert.alert('Marked as ' + message.split(' ')[2]); + Alert.alert(MARKED_AS_MSG(message.split(' ')[2])); } else { - Alert.alert('Something went wrong!', 'Please try again.'); + Alert.alert(ERROR_SOMETHING_WENT_WRONG); } if (callback) { callback(); } } catch (error) { - Alert.alert('Something went wrong!', 'Please try again.'); + Alert.alert(ERROR_SOMETHING_WENT_WRONG); console.log( 'Something went wrong! ðŸ˜', 'Unable able to retrieve data', diff --git a/src/services/SocialLinkingService.ts b/src/services/SocialLinkingService.ts index 4a01ee50..1423c8c0 100644 --- a/src/services/SocialLinkingService.ts +++ b/src/services/SocialLinkingService.ts @@ -12,6 +12,8 @@ import { LINK_TWITTER_ENDPOINT, LINK_TWITTER_OAUTH, } from '../constants'; +import {COMING_SOON_MSG, ERROR_LINK, SUCCESS_LINK} from '../constants/strings'; +import {CategorySelection} from '../screens'; // A list of endpoint strings for all the integrated socials export const integratedEndpoints: {[social: string]: [string, string]} = { @@ -124,7 +126,7 @@ export const handlePressForAuthBrowser: ( ) => Promise<boolean> = async (socialType: string) => { try { if (!(socialType in integratedEndpoints)) { - Alert.alert('Coming soon!'); + Alert.alert(COMING_SOON_MSG); return false; } @@ -168,7 +170,7 @@ export const handlePressForAuthBrowser: ( if (!success) { throw 'Unable to register with backend'; } - Alert.alert(`Successfully linked ${socialType} 🎉`); + Alert.alert(SUCCESS_LINK(socialType)); return true; } else if (response.type === 'cancel') { return false; @@ -178,14 +180,12 @@ export const handlePressForAuthBrowser: ( }) .catch((error) => { console.log(error); - Alert.alert( - `Something went wrong, we can't link with ${socialType} 😔`, - ); + Alert.alert(ERROR_LINK(socialType)); return false; }); } catch (error) { console.log(error); - Alert.alert(`Something went wrong, we can't link with ${socialType} 😔`); + Alert.alert(ERROR_LINK(socialType)); } return false; }; diff --git a/src/services/UserFriendsServices.ts b/src/services/UserFriendsServices.ts index 0b138fc3..f2e15824 100644 --- a/src/services/UserFriendsServices.ts +++ b/src/services/UserFriendsServices.ts @@ -1,7 +1,9 @@ //Abstracted common friends api calls out here import {Alert} from 'react-native'; +import {FriendshipStatusType} from 'src/types'; import {FRIENDS_ENDPOINT} from '../constants'; +import {ERROR_SOMETHING_WENT_WRONG_REFRESH} from '../constants/strings'; export const loadFriends = async (userId: string, token: string) => { try { @@ -26,19 +28,76 @@ export const friendOrUnfriendUser = async ( user: string, friend: string, token: string, - isFriend: boolean, + friendship_status: FriendshipStatusType, ) => { try { - const endpoint = FRIENDS_ENDPOINT + (isFriend ? `${user}/` : ''); + let body; + let method = ''; + let endpoint = FRIENDS_ENDPOINT; + + switch (friendship_status) { + case 'no_record': + method = 'POST'; + body = JSON.stringify({ + requested: friend, + }); + break; + case 'requested': + method = 'DELETE'; + endpoint += `${friend}/`; + body = JSON.stringify({ + reason: 'cancelled', + }); + break; + case 'friends': + method = 'DELETE'; + endpoint += `${friend}/`; + body = JSON.stringify({ + reason: 'unfriended', + }); + } + const response = await fetch(endpoint, { - method: isFriend ? 'DELETE' : 'POST', + method: method, headers: { 'Content-Type': 'application/json', Authorization: 'Token ' + token, }, + body: body, + }); + const status = response.status; + if (Math.floor(status / 100) === 2) { + return true; + } else { + console.log(await response.json()); + Alert.alert( + 'Something went wrong! ðŸ˜', + "Would you believe me if I told you that I don't know what happened?", + ); + return false; + } + } 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?", + ); + return false; + } +}; + +export const declineFriendRequestService = async ( + user_id: string, + token: string | null, +) => { + try { + const response = await fetch(FRIENDS_ENDPOINT + `${user_id}/`, { + method: 'DELETE', + headers: { + Authorization: 'Token ' + token, + }, body: JSON.stringify({ - user, - friend, + reason: 'declined', }), }); const status = response.status; @@ -46,6 +105,33 @@ export const friendOrUnfriendUser = async ( return true; } else { console.log(await response.json()); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); + return false; + } + } catch (error) { + console.log(error); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); + return false; + } +}; + +export const acceptFriendRequestService = async ( + requester_id: string, + token: string | null, +) => { + try { + const response = await fetch(FRIENDS_ENDPOINT + `${requester_id}/`, { + method: 'PATCH', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (Math.floor(status / 100) === 2) { + return true; + } else { + console.log(await response.json()); Alert.alert( 'Something went wrong! ðŸ˜', "Would you believe me if I told you that I don't know what happened?", diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts index 793ee44d..75d7d367 100644 --- a/src/services/UserProfileService.ts +++ b/src/services/UserProfileService.ts @@ -17,6 +17,17 @@ import { SEND_OTP_ENDPOINT, PROFILE_PHOTO_THUMBNAIL_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 { @@ -39,6 +50,8 @@ export const loadProfileInfo = async (token: string, userId: string) => { tiktok, university_class, profile_completion_stage, + friendship_status, + friendship_requester_id, } = info; birthday = birthday && moment(birthday).format('YYYY-MM-DD'); return { @@ -51,15 +64,14 @@ export const loadProfileInfo = async (token: string, userId: string) => { tiktok, university_class, profile_completion_stage, + friendship_status, + friendship_requester_id, }; } else { throw 'Unable to load profile data'; } } catch (error) { - Alert.alert( - 'Something went wrong! ðŸ˜', - "Would you believe me if I told you that I don't know what happened?", - ); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); } }; @@ -174,10 +186,7 @@ export const handlePasswordResetRequest = async (value: string) => { `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( - 'Something went wrong! ðŸ˜', - "Would you believe me if I told you that I don't know what happened?", - ); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); } console.log(response); @@ -185,7 +194,7 @@ export const handlePasswordResetRequest = async (value: string) => { } } catch (error) { console.log(error); - Alert.alert('Something went wrong! ðŸ˜', 'Looks like our servers are down'); + Alert.alert(ERROR_SOMETHING_WENT_WRONG); return false; } }; @@ -211,16 +220,11 @@ export const handlePasswordCodeVerification = async ( 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}`, - ); + Alert.alert(ERROR_PWD_ACCOUNT(TAGG_CUSTOMER_SUPPORT)); } else if (status === 401) { - Alert.alert('Looks like you have entered the wrong code'); + Alert.alert(ERROR_INVALID_PWD_CODE); } else { - Alert.alert( - 'Something went wrong! ðŸ˜', - "Would you believe me if I told you that I don't know what happened?", - ); + Alert.alert(ERROR_SOMETHING_WENT_WRONG); } console.log(response); @@ -228,7 +232,7 @@ export const handlePasswordCodeVerification = async ( } } catch (error) { console.log(error); - Alert.alert('Something went wrong! ðŸ˜', 'Looks like our servers are down'); + Alert.alert(ERROR_SOMETHING_WENT_WRONG); return false; } }; @@ -248,27 +252,22 @@ export const handlePasswordReset = async (value: string, password: string) => { }); const status = response.status; if (status === 200) { - Alert.alert('Your password was reset successfully'); + Alert.alert(SUCCESS_PWD_RESET); 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}`, - ); + Alert.alert(ERROR_PWD_ACCOUNT(TAGG_CUSTOMER_SUPPORT)); } else if (status == 406) { - Alert.alert('You may not use an already used password'); + Alert.alert(ERROR_DUP_OLD_PWD); } else { - Alert.alert( - 'Something went wrong! ðŸ˜', - "Would you believe me if I told you that I don't know what happened?", - ); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); } console.log(response); return false; } } catch (error) { console.log(error); - Alert.alert('Something went wrong! ðŸ˜', 'Looks like our servers are down'); + Alert.alert(ERROR_SOMETHING_WENT_WRONG); return false; } }; @@ -292,17 +291,11 @@ export const verifyOtp = async (phone: string, otp: string) => { 'Try again. Tap the resend code button if you need a new code.', ); } else { - Alert.alert( - 'Something went wrong! ðŸ˜', - "Would you believe me if I told you that I don't know what happened?", - ); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); } } } catch (error) { - Alert.alert( - 'Verifiation failed 😓', - 'Please double-check your network connection and retry.', - ); + Alert.alert(ERROR_VERIFICATION_FAILED_SHORT, ERROR_DOUBLE_CHECK_CONNECTION); return { name: 'Verification error', description: error, @@ -322,13 +315,10 @@ export const sendOtp = async (phone: string) => { let status = response.status; if (status === 200) { - Alert.alert( - 'New verification code sent!', - 'Check your phone messages for your code.', - ); + Alert.alert(SUCCESS_VERIFICATION_CODE_SENT); return true; } else { - Alert.alert('Something went wrong!'); + Alert.alert(ERROR_SOMETHING_WENT_WRONG); return false; } } catch (error) { |
