aboutsummaryrefslogtreecommitdiff
path: root/src/services/UserFollowServices.ts
diff options
context:
space:
mode:
authorAshm Walia <40498934+ashmgarv@users.noreply.github.com>2020-10-24 16:12:39 -0700
committerGitHub <noreply@github.com>2020-10-24 19:12:39 -0400
commit84d283b44f2b6cecb757edcd94e717a36c3ba3c3 (patch)
tree532a6c415c57b90bb90243b2d99845bb3e93d058 /src/services/UserFollowServices.ts
parent8b680e97ad4689493d2c398281cc0da8e333aa04 (diff)
[TMA 301] Add follow/unfollow button to profile (#70)
* Follow Unfollow User * Fixed an issue and moved api call to Content.tsx * last * Small changes
Diffstat (limited to 'src/services/UserFollowServices.ts')
-rw-r--r--src/services/UserFollowServices.ts68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/services/UserFollowServices.ts b/src/services/UserFollowServices.ts
new file mode 100644
index 00000000..bfd8058a
--- /dev/null
+++ b/src/services/UserFollowServices.ts
@@ -0,0 +1,68 @@
+//Abstracted common user follow api calls out here
+
+import {Alert} from 'react-native';
+import {
+ FOLLOWERS_ENDPOINT,
+ FOLLOW_USER_ENDPOINT,
+ UNFOLLOW_USER_ENDPOINT,
+} from '../constants';
+
+import {ProfilePreviewType} from 'src/types';
+
+export const loadFollowers = async (userId: string, token: string) => {
+ try {
+ const response = await fetch(FOLLOWERS_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);
+ }
+ return [];
+};
+
+export const followOrUnfollowUser = async (
+ follower: string,
+ followed: string,
+ token: string,
+ isFollowed: boolean,
+) => {
+ try {
+ const endpoint = isFollowed ? UNFOLLOW_USER_ENDPOINT : FOLLOW_USER_ENDPOINT;
+ const response = await fetch(endpoint, {
+ method: 'POST',
+ headers: {
+ Authorization: 'Token ' + token,
+ },
+ body: JSON.stringify({
+ follower,
+ followed,
+ }),
+ });
+ if (Math.floor(response.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;
+ }
+};