Skip to content

Commit 85ab96e

Browse files
authored
Merge pull request #2686 from appirio-tech/hotfix/fix-dashboard-auto-refreshing
[HOTFIX] [PROD] fix dashboard auto refreshing
2 parents 9121092 + 077d8b9 commit 85ab96e

File tree

3 files changed

+51
-36
lines changed

3 files changed

+51
-36
lines changed

src/components/NotificationsReader/NotificationsReader.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { markNotificationsReadByCriteria } from '../../routes/notifications/acti
1111

1212
class NotificationsReader extends React.Component {
1313
componentWillMount() {
14-
const { criteria } = this.props
14+
const { criteria, markNotificationsReadByCriteria } = this.props
1515

1616
markNotificationsReadByCriteria(criteria)
1717
}

src/config/constants.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,6 @@ export const DATE_TO_USER_FIELD_MAP = {
564564

565565
// Notifications
566566
export const REFRESH_NOTIFICATIONS_INTERVAL = 1000 * 60 * 1 // 1 minute interval
567-
export const REFRESH_UNREAD_UPDATE_INTERVAL = 1000 * 10 * 1 // 10 second interval
568567
export const NOTIFICATIONS_DROPDOWN_PER_SOURCE = 5
569568
export const NOTIFICATIONS_NEW_PER_SOURCE = 10
570569

src/projects/detail/components/PostsRefreshPrompt/PostsRefreshPrompt.jsx

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
import React from 'react'
1010
import PT from 'prop-types'
11+
import _ from 'lodash'
1112

1213
import Sticky from '../../../../components/Sticky'
1314

@@ -22,46 +23,47 @@ import Refresh from '../../../../assets/icons/icon-refresh.svg'
2223

2324
import styles from './PostsRefreshPrompt.scss'
2425

26+
/**
27+
* Check if there are any new unread notifications related to topics or posts
28+
*
29+
* @param {Array} notifications current notifications
30+
* @param {Array} nextNotifications updated notifications
31+
* @param {Number} projectId project id
32+
*
33+
* @returns {Boolean} true if there are new unread notifications related to topics or posts
34+
*/
35+
const getIsNewTopicAndPostNotificationsArrived = (notifications, nextNotifications, projectId) => {
36+
const newNotifications = _.differenceBy(nextNotifications, notifications, 'id')
37+
38+
const notReadNotifications = filterReadNotifications(newNotifications)
39+
const unreadTopicAndPostChangedNotifications = filterTopicAndPostChangedNotifications(filterNotificationsByProjectId(notReadNotifications, projectId))
40+
41+
return unreadTopicAndPostChangedNotifications.length > 0
42+
}
43+
2544
class PostsRefreshPrompt extends React.Component {
2645
constructor(props) {
2746
super(props)
2847

2948
this.state = {
30-
unreadUpdate: [],
49+
hasToRefresh: false,
3150
scrolled: false,
3251
}
3352

3453
this.onScroll = this.onScroll.bind(this)
35-
this.checkForUnreadPosts = this.checkForUnreadPosts.bind(this)
36-
37-
this.refreshUnreadUpdate = null
38-
}
39-
40-
componentWillMount() {
41-
window.addEventListener('scroll', this.onScroll)
54+
this.refreshFeeds = this.refreshFeeds.bind(this)
4255
}
4356

4457
componentWillUnmount() {
4558
window.removeEventListener('scroll', this.onScroll)
46-
// clearInterval(this.refreshUnreadUpdate)
4759
}
4860

4961
componentDidMount() {
5062
this.setState({
51-
unreadUpdate: [],
63+
hasToRefresh: false,
5264
scrolled: window.scrollY > 0,
5365
})
54-
55-
// this.refreshUnreadUpdate = setInterval(this.checkForUnreadPosts, REFRESH_UNREAD_UPDATE_INTERVAL)
56-
}
57-
58-
getUnreadTopicAndPostChangedNotifications() {
59-
const { notifications, projectId } = this.props
60-
61-
const notReadNotifications = filterReadNotifications(notifications.notifications)
62-
const unreadTopicAndPostChangedNotifications = filterTopicAndPostChangedNotifications(filterNotificationsByProjectId(notReadNotifications, projectId))
63-
64-
return unreadTopicAndPostChangedNotifications
66+
window.addEventListener('scroll', this.onScroll)
6567
}
6668

6769
onScroll() {
@@ -74,31 +76,45 @@ class PostsRefreshPrompt extends React.Component {
7476
}
7577
}
7678

77-
checkForUnreadPosts() {
78-
const unreadUpdate = this.getUnreadTopicAndPostChangedNotifications()
79-
const { refreshFeeds, preventShowing } = this.props
80-
const { scrolled } = this.state
79+
componentWillReceiveProps(nextProps) {
80+
const { notifications: { notifications } } = this.props
81+
const { notifications: { notifications: nextNotifications }, projectId, preventShowing } = nextProps
82+
const { scrolled, hasToRefresh } = this.state
83+
84+
const isNewTopicAndPostNotificationsArrived = getIsNewTopicAndPostNotificationsArrived(notifications, nextNotifications, projectId)
85+
86+
// if there are new notification regarding topics or post, we have to refresh the feed
87+
// we run this only once, so if we already have to refresh the feed, than do nothing
88+
if (!hasToRefresh && isNewTopicAndPostNotificationsArrived) {
89+
this.setState({ hasToRefresh: true }, () => {
90+
// if not scrolled we refresh feed automatically without showing a button for manual refresh
91+
if (!preventShowing && !scrolled) {
92+
this.refreshFeeds()
93+
}
94+
})
95+
}
96+
}
8197

82-
this.setState({ unreadUpdate: this.getUnreadTopicAndPostChangedNotifications() })
98+
refreshFeeds() {
99+
const { refreshFeeds } = this.props
83100

84-
if (!preventShowing && !scrolled && unreadUpdate.length > 0) {
85-
refreshFeeds()
86-
}
101+
refreshFeeds && refreshFeeds()
102+
this.setState({ hasToRefresh: false })
87103
}
88104

89105
render() {
90-
const { preventShowing, refreshFeeds } = this.props
91-
const { unreadUpdate, scrolled } = this.state
106+
const { preventShowing } = this.props
107+
const { hasToRefresh } = this.state
92108

93-
const isShown = unreadUpdate.length > 0 && !preventShowing && scrolled
109+
const isShown = hasToRefresh && !preventShowing
94110

95111
return (
96112
isShown ? (
97-
<Sticky top={SCROLL_TO_MARGIN} innerZ={999}>
113+
<Sticky top={SCROLL_TO_MARGIN} innerZ={10}>
98114
<div styleName="prompt">
99115
<button
100116
className={`tc-btn tc-btn-primary tc-btn-md ${styles.button}`}
101-
onClick={refreshFeeds}
117+
onClick={this.refreshFeeds}
102118
>
103119
<Refresh styleName="icon" />
104120
Reload page to view updates

0 commit comments

Comments
 (0)