aboutsummaryrefslogtreecommitdiff
path: root/src/services/UserFriendsServices.ts
diff options
context:
space:
mode:
authorAshm Walia <40498934+ashmgarv@users.noreply.github.com>2021-01-16 10:43:03 -0800
committerGitHub <noreply@github.com>2021-01-16 10:43:03 -0800
commitae9cbb026f6128e732d36138751e319c926c72b1 (patch)
tree4946280bb00f8081370c3602a404c041a7cc28d5 /src/services/UserFriendsServices.ts
parent30391867438bb28cbcba9fc9ee2ff6d00027fd86 (diff)
parent23ccc2d6441aca0c89d0ba30e9d990b4aedb73cb (diff)
Merge pull request #187 from shravyaramesh/tma538-friend-requests
[TMA485] friend request frontend
Diffstat (limited to 'src/services/UserFriendsServices.ts')
-rw-r--r--src/services/UserFriendsServices.ts101
1 files changed, 96 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;
+ }
+};