//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; } };