diff options
| author | Shravya Ramesh <shravs1208@gmail.com> | 2021-01-15 03:33:59 -0800 |
|---|---|---|
| committer | Shravya Ramesh <shravs1208@gmail.com> | 2021-01-15 03:33:59 -0800 |
| commit | df6595694c678657fec30d881fb1edcd39b62f17 (patch) | |
| tree | 2953fbbc222fc3f7f092ee61c6d4b0c3414d3f9d /src/services/UserFriendsServices.ts | |
| parent | 82476e27fe6f5dc699370659d223dcd86fd5c76b (diff) | |
friend request
Diffstat (limited to 'src/services/UserFriendsServices.ts')
| -rw-r--r-- | src/services/UserFriendsServices.ts | 104 |
1 files changed, 99 insertions, 5 deletions
diff --git a/src/services/UserFriendsServices.ts b/src/services/UserFriendsServices.ts index 0b138fc3..9235d890 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'; export const loadFriends = async (userId: string, token: string) => { @@ -26,19 +27,77 @@ 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; @@ -61,3 +120,38 @@ export const friendOrUnfriendUser = async ( return false; } }; + +export const acceptFriendRequestService = async ( + requester_id: string, + token: string | null, +) => { + try { + console.log('requester_id: ', requester_id); + console.log('token: ', token); + 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; + } +}; |
