diff options
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/UserFriendsServices.ts | 101 | ||||
| -rw-r--r-- | src/services/UserProfileService.ts | 4 |
2 files changed, 100 insertions, 5 deletions
diff --git a/src/services/UserFriendsServices.ts b/src/services/UserFriendsServices.ts index f7a39abf..f2e15824 100644 --- a/src/services/UserFriendsServices.ts +++ b/src/services/UserFriendsServices.ts @@ -1,6 +1,7 @@ //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'; @@ -27,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; @@ -56,3 +114,36 @@ export const friendOrUnfriendUser = async ( 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?", + ); + 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; + } +}; diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts index 37d00659..75d7d367 100644 --- a/src/services/UserProfileService.ts +++ b/src/services/UserProfileService.ts @@ -50,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 { @@ -62,6 +64,8 @@ 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'; |
