From 38661e00281363b0f4ad32f0b29d739e1ca09164 Mon Sep 17 00:00:00 2001 From: Ashm Walia <40498934+ashmgarv@users.noreply.github.com> Date: Wed, 30 Dec 2020 11:36:44 -0800 Subject: [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 ? --- src/services/UserFriendsServices.ts | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/services/UserFriendsServices.ts (limited to 'src/services/UserFriendsServices.ts') 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; + } +}; -- cgit v1.2.3-70-g09d2