diff options
author | Ivan Chen <ivan@thetaggid.com> | 2021-01-27 19:32:21 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-27 19:32:21 -0500 |
commit | 19d2a861f260c02031d951f3d43bff8ac672146d (patch) | |
tree | 253abceb267dfb75ac9118995886ff152c1ba74b /src/services/UserFriendsService.ts | |
parent | 60d3d97ad6913369762877a9dbfdaac0473deb28 (diff) | |
parent | dc9556867dd44b706c43678d40802a335ff40658 (diff) |
Merge pull request #203 from IvanIFChen/tma577-profile-slowness
[TMA-577] Profile Slowness
Diffstat (limited to 'src/services/UserFriendsService.ts')
-rw-r--r-- | src/services/UserFriendsService.ts | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/services/UserFriendsService.ts b/src/services/UserFriendsService.ts new file mode 100644 index 00000000..f2e15824 --- /dev/null +++ b/src/services/UserFriendsService.ts @@ -0,0 +1,149 @@ +//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'; + +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, + friendship_status: FriendshipStatusType, +) => { + try { + 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: 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({ + reason: 'declined', + }), + }); + const status = response.status; + if (Math.floor(status / 100) === 2) { + return true; + } else { + console.log(await response.json()); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); + return false; + } + } catch (error) { + console.log(error); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); + 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; + } +}; |