From aa1f4960279b7830c60463e45c85096d23c05b59 Mon Sep 17 00:00:00 2001 From: Norman Eremic Date: Thu, 21 Jul 2016 16:33:22 +0200 Subject: [PATCH] #484 check for notifications now only runs if there are notifications present --- .../src/notification-bar/notification-bar.jsx | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/client/lib/yourturn/src/notification-bar/notification-bar.jsx b/client/lib/yourturn/src/notification-bar/notification-bar.jsx index 858b0f47..b3d833c2 100644 --- a/client/lib/yourturn/src/notification-bar/notification-bar.jsx +++ b/client/lib/yourturn/src/notification-bar/notification-bar.jsx @@ -24,8 +24,8 @@ Notification.displayName = 'Notification'; // ======== class NotificationBar extends React.Component { - constructor() { - super(); + constructor(props) { + super(props); this.state = { interval: false }; @@ -34,17 +34,38 @@ class NotificationBar extends React.Component { dismiss(id) { this.props.dispatch(Actions.removeNotification(id)); } + + componentWillReceiveProps(nextProps) { + const { notifications = [] } = nextProps; + + if (!this.state.interval && notifications.length > 0) { + let interval = setInterval(() => { + this.props.dispatch(Actions.removeNotificationsOlderThan(5000)); + }, 5000); + this.setState({ + interval + }); + } else if (this.state.interval && notifications.length == 0) { + clearInterval(this.state.interval); + this.setState({ + interval: false + }); + } + } componentDidMount() { /** * Continually dismiss old notifications. */ - let interval = setInterval(() => { - this.props.dispatch(Actions.removeNotificationsOlderThan(5000)); - }, 5000); - this.setState({ - interval - }); + const { notifications = [] } = this.props; + if (notifications.length > 0) { + let interval = setInterval(() => { + this.props.dispatch(Actions.removeNotificationsOlderThan(5000)); + }, 5000); + this.setState({ + interval + }); + } } componentWillUnmount() {