From 8459102c741a0f00f5e8c0a64ad8b5af05172a3f Mon Sep 17 00:00:00 2001 From: Travis Kohlbeck Date: Mon, 17 Feb 2020 14:53:35 -0500 Subject: [PATCH] removes follow account button on click --- src/pages/Notifications.tsx | 75 ++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 10 deletions(-) diff --git a/src/pages/Notifications.tsx b/src/pages/Notifications.tsx index 73919e2..0cc9111 100644 --- a/src/pages/Notifications.tsx +++ b/src/pages/Notifications.tsx @@ -53,6 +53,11 @@ interface INotificationsPageState { */ notifications?: [Notification]; + /** + * The ids of accounts you follow associated with a notification + */ + followingAccounts?: string[]; + /** * Whether the view is still loading. */ @@ -122,7 +127,7 @@ class NotificationsPage extends Component { /** * Perform pre-mount tasks. */ - componentWillMount() { + /*componentWillMount() { // Get the list of notifications and update the state. this.client .get("/notifications") @@ -149,6 +154,44 @@ class NotificationsPage extends Component { viewDidErrorCode: err.message }); }); + }*/ + + async componentWillMount() { + try { + let resp: any = await this.client.get("/notifications"); + let notifications: [Notification] = resp.data; + let notifMenus: Dictionary = {}; + notifications.forEach( + (n: Notification) => (notifMenus[n.id] = false) + ); + resp = await this.client.get( + `/accounts/${sessionStorage.getItem("id")}/following` + ); + let followingAcctIds: string[] = resp.data.map( + (acct: Account) => acct.id + ); + let notifAcctIds: string[] = notifications.map( + (n: Notification) => n.account.id + ); + let followingNotifAcctIds = followingAcctIds.filter((id: string) => + notifAcctIds.includes(id) + ); + console.log(followingNotifAcctIds); + this.setState({ + notifications, + followingAccounts: followingNotifAcctIds, + viewIsLoading: false, + viewDidLoad: true, + mobileMenuOpen: notifMenus + }); + } catch (e) { + this.setState({ + viewDidLoad: true, + viewIsLoading: false, + viewDidError: true, + viewDidErrorCode: e.message + }); + } } /** @@ -359,6 +402,13 @@ class NotificationsPage extends Component { this.props.enqueueSnackbar( "You are now following this account." ); + let followingAccounts: string[] + if (this.state.followingAccounts) { + followingAccounts = this.state.followingAccounts.concat(acct.id) + } else { + followingAccounts = [acct.id] + } + this.setState({followingAccounts}) }) .catch((err: Error) => { this.props.enqueueSnackbar( @@ -444,15 +494,20 @@ class NotificationsPage extends Component { - - - this.followMember(notif.account) - } - > - - - + {this.state.followingAccounts && + !this.state.followingAccounts.includes( + notif.account.id + ) ? ( + + + this.followMember(notif.account) + } + > + + + + ) : null} ) : notif.status ? (