From 4185d6bad8d96131bca3f9ca94d5b8c8af1a2092 Mon Sep 17 00:00:00 2001 From: Marquis Kurt Date: Thu, 12 Dec 2019 11:44:14 -0500 Subject: [PATCH] Get relationship and evaluate before following (HD-3) --- src/pages/Notifications.tsx | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/pages/Notifications.tsx b/src/pages/Notifications.tsx index 654027b..5322a8c 100644 --- a/src/pages/Notifications.tsx +++ b/src/pages/Notifications.tsx @@ -33,6 +33,7 @@ import NotificationsIcon from "@material-ui/icons/Notifications"; import Mastodon from "megalodon"; import { Notification } from "../types/Notification"; import { Account } from "../types/Account"; +import { Relationship } from "../types/Relationship"; import { withSnackbar } from "notistack"; interface INotificationsPageState { @@ -295,18 +296,35 @@ class NotificationsPage extends Component { followMember(acct: Account) { this.client - .post(`/accounts/${acct.id}/follow`) + .get(`/accounts/relationships`, { id: acct.id }) .then((resp: any) => { - this.props.enqueueSnackbar( - "You are now following this account." - ); + let relationship: Relationship = resp.data[0]; + + if (relationship.following == false) { + this.client + .post(`/accounts/${acct.id}/follow`) + .then((resp: any) => { + this.props.enqueueSnackbar( + "You are now following this account." + ); + }) + .catch((err: Error) => { + this.props.enqueueSnackbar( + "Couldn't follow account: " + err.name, + { variant: "error" } + ); + console.error(err.message); + }); + } else { + this.props.enqueueSnackbar( + "You already follow this account." + ); + } }) .catch((err: Error) => { - this.props.enqueueSnackbar( - "Couldn't follow account: " + err.name, - { variant: "error" } - ); - console.error(err.message); + this.props.enqueueSnackbar("Couldn't find relationship.", { + variant: "error" + }); }); }