1
- import React , { useState , useEffect , useMemo } from 'react' ;
1
+ import React , {
2
+ useState , useEffect , useRef , useMemo ,
3
+ } from 'react' ;
2
4
import PT from 'prop-types' ;
3
5
import { connect } from 'react-redux' ;
4
6
import TopBanner from 'assets/images/timeline-wall/top-banner.png' ;
@@ -10,15 +12,19 @@ import cn from 'classnames';
10
12
import moment from 'moment' ;
11
13
import { useMediaQuery } from 'react-responsive' ;
12
14
import _ from 'lodash' ;
15
+ import { config } from 'topcoder-react-utils' ;
13
16
import timelineActions from 'actions/timelineWall' ;
14
17
import LoadingIndicator from 'components/LoadingIndicator' ;
15
18
import TimelineEvents from './timeline-events' ;
16
19
import PendingApprovals from './pending-approvals' ;
17
20
18
21
import './styles.scss' ;
19
22
23
+
24
+ const FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL = _ . get ( config , 'TIMELINE.FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL' , 0 ) ;
20
25
function TimelineWallContainer ( props ) {
21
26
const [ tab , setTab ] = useState ( 0 ) ;
27
+ const fetchingApprovalsInterval = useRef ( null ) ;
22
28
const [ showRightFilterMobile , setShowRightFilterMobile ] = useState ( false ) ;
23
29
const [ selectedFilterValue , setSelectedFilterValue ] = useState ( {
24
30
year : 0 ,
@@ -37,6 +43,7 @@ function TimelineWallContainer(props) {
37
43
getAvatar,
38
44
userAvatars,
39
45
pendingApprovals,
46
+ loadingApprovals,
40
47
uploading,
41
48
uploadResult,
42
49
} = props ;
@@ -56,9 +63,25 @@ function TimelineWallContainer(props) {
56
63
} , [ ] ) ;
57
64
58
65
useEffect ( ( ) => {
59
- if ( authToken && isAdmin && ! pendingApprovals . length ) {
66
+ if ( fetchingApprovalsInterval . current ) {
67
+ clearInterval ( fetchingApprovalsInterval . current ) ;
68
+ fetchingApprovalsInterval . current = null ;
69
+ }
70
+ if ( authToken && isAdmin ) {
60
71
getPendingApprovals ( authToken ) ;
72
+ if ( FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL ) {
73
+ fetchingApprovalsInterval . current = setInterval ( ( ) => {
74
+ getPendingApprovals ( authToken ) ;
75
+ } , FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL ) ;
76
+ }
61
77
}
78
+
79
+ return ( ) => {
80
+ if ( fetchingApprovalsInterval . current ) {
81
+ clearInterval ( fetchingApprovalsInterval . current ) ;
82
+ fetchingApprovalsInterval . current = null ;
83
+ }
84
+ } ;
62
85
} , [ isAdmin ] ) ;
63
86
64
87
useEffect ( ( ) => {
@@ -73,7 +96,7 @@ function TimelineWallContainer(props) {
73
96
} , [ events ] ) ;
74
97
75
98
useEffect ( ( ) => {
76
- if ( ( pendingApprovals || [ ] ) . length ) {
99
+ if ( pendingApprovals . length ) {
77
100
_ . uniqBy ( pendingApprovals , 'createdBy' ) . forEach ( ( eventItem ) => {
78
101
const photoURL = _ . get ( userAvatars , eventItem . createdBy ) ;
79
102
if ( ! photoURL ) {
@@ -229,8 +252,10 @@ function TimelineWallContainer(props) {
229
252
/>
230
253
< React . Fragment >
231
254
{
232
- loading ? (
233
- < LoadingIndicator />
255
+ loadingApprovals ? (
256
+ < LoadingIndicator
257
+ styleName = { cn ( { hide : tab === 0 } ) }
258
+ />
234
259
) : (
235
260
< PendingApprovals
236
261
events = { pendingApprovals }
@@ -256,6 +281,7 @@ TimelineWallContainer.defaultProps = {
256
281
auth : null ,
257
282
isAdmin : false ,
258
283
loading : false ,
284
+ loadingApprovals : false ,
259
285
uploading : false ,
260
286
uploadResult : '' ,
261
287
events : [ ] ,
@@ -270,6 +296,7 @@ TimelineWallContainer.propTypes = {
270
296
auth : PT . shape ( ) ,
271
297
isAdmin : PT . bool ,
272
298
loading : PT . bool ,
299
+ loadingApprovals : PT . bool ,
273
300
uploading : PT . bool ,
274
301
uploadResult : PT . string ,
275
302
events : PT . arrayOf ( PT . shape ( ) ) ,
@@ -288,11 +315,13 @@ const mapStateToProps = state => ({
288
315
} ,
289
316
isAdmin : state . timelineWall . isAdmin ,
290
317
loading : state . timelineWall . loading ,
318
+ loadingApprovals : state . timelineWall . loadingApprovals
319
+ && ! ( state . timelineWall . pendingApprovals || [ ] ) . length ,
291
320
uploading : state . timelineWall . uploading ,
292
321
uploadResult : state . timelineWall . uploadResult ,
293
322
events : state . timelineWall . events ,
294
323
userAvatars : state . timelineWall . userAvatars ,
295
- pendingApprovals : state . timelineWall . pendingApprovals ,
324
+ pendingApprovals : state . timelineWall . pendingApprovals || [ ] ,
296
325
} ) ;
297
326
298
327
function mapDispatchToProps ( dispatch ) {
0 commit comments