88 */
99import React from 'react'
1010import PT from 'prop-types'
11+ import _ from 'lodash'
1112
1213import Sticky from '../../../../components/Sticky'
1314
@@ -22,46 +23,47 @@ import Refresh from '../../../../assets/icons/icon-refresh.svg'
2223
2324import 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+
2544class 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