1
- import React , { useState , useEffect } from 'react' ;
1
+ import React , { useState , useEffect , useRef } from 'react' ;
2
2
import PT from 'prop-types' ;
3
3
import { connect } from 'react-redux' ;
4
4
import TopBanner from 'assets/images/timeline-wall/top-banner.png' ;
@@ -9,15 +9,19 @@ import cn from 'classnames';
9
9
import moment from 'moment' ;
10
10
import { useMediaQuery } from 'react-responsive' ;
11
11
import _ from 'lodash' ;
12
+ import { config } from 'topcoder-react-utils' ;
12
13
import timelineActions from 'actions/timelineWall' ;
13
14
import LoadingIndicator from 'components/LoadingIndicator' ;
14
15
import TimelineEvents from './timeline-events' ;
15
16
import PendingApprovals from './pending-approvals' ;
16
17
17
18
import './styles.scss' ;
18
19
20
+
21
+ const FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL = _ . get ( config , 'TIMELINE.FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL' , 0 ) ;
19
22
function TimelineWallContainer ( props ) {
20
23
const [ tab , setTab ] = useState ( 0 ) ;
24
+ const fetchingApprovalsInterval = useRef ( null ) ;
21
25
const [ showRightFilterMobile , setShowRightFilterMobile ] = useState ( false ) ;
22
26
const [ selectedFilterValue , setSelectedFilterValue ] = useState ( {
23
27
year : 0 ,
@@ -36,6 +40,7 @@ function TimelineWallContainer(props) {
36
40
getAvatar,
37
41
userAvatars,
38
42
pendingApprovals,
43
+ loadingApprovals,
39
44
uploading,
40
45
uploadResult,
41
46
} = props ;
@@ -55,9 +60,25 @@ function TimelineWallContainer(props) {
55
60
} , [ ] ) ;
56
61
57
62
useEffect ( ( ) => {
58
- if ( authToken && isAdmin && ! pendingApprovals . length ) {
63
+ if ( fetchingApprovalsInterval . current ) {
64
+ clearInterval ( fetchingApprovalsInterval . current ) ;
65
+ fetchingApprovalsInterval . current = null ;
66
+ }
67
+ if ( authToken && isAdmin ) {
59
68
getPendingApprovals ( authToken ) ;
69
+ if ( FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL ) {
70
+ fetchingApprovalsInterval . current = setInterval ( ( ) => {
71
+ getPendingApprovals ( authToken ) ;
72
+ } , FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL ) ;
73
+ }
60
74
}
75
+
76
+ return ( ) => {
77
+ if ( fetchingApprovalsInterval . current ) {
78
+ clearInterval ( fetchingApprovalsInterval . current ) ;
79
+ fetchingApprovalsInterval . current = null ;
80
+ }
81
+ } ;
61
82
} , [ isAdmin ] ) ;
62
83
63
84
useEffect ( ( ) => {
@@ -72,7 +93,7 @@ function TimelineWallContainer(props) {
72
93
} , [ events ] ) ;
73
94
74
95
useEffect ( ( ) => {
75
- if ( ( pendingApprovals || [ ] ) . length ) {
96
+ if ( pendingApprovals . length ) {
76
97
_ . uniqBy ( pendingApprovals , 'createdBy' ) . forEach ( ( eventItem ) => {
77
98
const photoURL = _ . get ( userAvatars , eventItem . createdBy ) ;
78
99
if ( ! photoURL ) {
@@ -205,8 +226,10 @@ function TimelineWallContainer(props) {
205
226
/>
206
227
< React . Fragment >
207
228
{
208
- loading ? (
209
- < LoadingIndicator />
229
+ loadingApprovals ? (
230
+ < LoadingIndicator
231
+ styleName = { cn ( { hide : tab === 0 } ) }
232
+ />
210
233
) : (
211
234
< PendingApprovals
212
235
events = { pendingApprovals }
@@ -232,6 +255,7 @@ TimelineWallContainer.defaultProps = {
232
255
auth : null ,
233
256
isAdmin : false ,
234
257
loading : false ,
258
+ loadingApprovals : false ,
235
259
uploading : false ,
236
260
uploadResult : '' ,
237
261
events : [ ] ,
@@ -246,6 +270,7 @@ TimelineWallContainer.propTypes = {
246
270
auth : PT . shape ( ) ,
247
271
isAdmin : PT . bool ,
248
272
loading : PT . bool ,
273
+ loadingApprovals : PT . bool ,
249
274
uploading : PT . bool ,
250
275
uploadResult : PT . string ,
251
276
events : PT . arrayOf ( PT . shape ( ) ) ,
@@ -264,11 +289,13 @@ const mapStateToProps = state => ({
264
289
} ,
265
290
isAdmin : state . timelineWall . isAdmin ,
266
291
loading : state . timelineWall . loading ,
292
+ loadingApprovals : state . timelineWall . loadingApprovals
293
+ && ! ( state . timelineWall . pendingApprovals || [ ] ) . length ,
267
294
uploading : state . timelineWall . uploading ,
268
295
uploadResult : state . timelineWall . uploadResult ,
269
296
events : state . timelineWall . events ,
270
297
userAvatars : state . timelineWall . userAvatars ,
271
- pendingApprovals : state . timelineWall . pendingApprovals ,
298
+ pendingApprovals : state . timelineWall . pendingApprovals || [ ] ,
272
299
} ) ;
273
300
274
301
function mapDispatchToProps ( dispatch ) {
0 commit comments