aboutsummaryrefslogtreecommitdiff
path: root/src/services/UserFriendsServices.ts
diff options
context:
space:
mode:
authorAshm Walia <40498934+ashmgarv@users.noreply.github.com>2020-12-30 11:36:44 -0800
committerGitHub <noreply@github.com>2020-12-30 14:36:44 -0500
commit38661e00281363b0f4ad32f0b29d739e1ca09164 (patch)
tree316cd837b6cc6ae24783f1d93d6c9ee7fb898f68 /src/services/UserFriendsServices.ts
parentbd2f89805d0bb1c2f1d08fe8d91099aa4f109d35 (diff)
[TMA - 457]Change followers to friends (#149)
* One commit to replace followers with friends * Move block unblock to drawer and some cosmetic changes * Options to edit own profile when viewing * Changes for University Class * Small fix * Made ProfileOnboarding a scroll view and other small changes * Small fix * Small fix thanks to ivan and tanmay * Add ?
Diffstat (limited to 'src/services/UserFriendsServices.ts')
-rw-r--r--src/services/UserFriendsServices.ts63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/services/UserFriendsServices.ts b/src/services/UserFriendsServices.ts
new file mode 100644
index 00000000..0b138fc3
--- /dev/null
+++ b/src/services/UserFriendsServices.ts
@@ -0,0 +1,63 @@
+//Abstracted common friends api calls out here
+
+import {Alert} from 'react-native';
+import {FRIENDS_ENDPOINT} from '../constants';
+
+export const loadFriends = async (userId: string, token: string) => {
+ try {
+ const response = await fetch(FRIENDS_ENDPOINT + `?user_id=${userId}`, {
+ method: 'GET',
+ headers: {
+ Authorization: 'Token ' + token,
+ },
+ });
+ if (response.status === 200) {
+ const body = await response.json();
+ return body;
+ } else {
+ throw new Error(await response.json());
+ }
+ } catch (error) {
+ console.log(error);
+ }
+};
+
+export const friendOrUnfriendUser = async (
+ user: string,
+ friend: string,
+ token: string,
+ isFriend: boolean,
+) => {
+ try {
+ const endpoint = FRIENDS_ENDPOINT + (isFriend ? `${user}/` : '');
+ const response = await fetch(endpoint, {
+ method: isFriend ? 'DELETE' : 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization: 'Token ' + token,
+ },
+ body: JSON.stringify({
+ user,
+ friend,
+ }),
+ });
+ 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;
+ }
+};