1- import React , { useState , useEffect } from 'react' ;
1+ import React , { useState , useEffect , useRef } from 'react' ;
22import PT from 'prop-types' ;
33import { connect } from 'react-redux' ;
44import TopBanner from 'assets/images/timeline-wall/top-banner.png' ;
@@ -9,15 +9,19 @@ import cn from 'classnames';
99import moment from 'moment' ;
1010import { useMediaQuery } from 'react-responsive' ;
1111import _ from 'lodash' ;
12+ import { config } from 'topcoder-react-utils' ;
1213import timelineActions from 'actions/timelineWall' ;
1314import LoadingIndicator from 'components/LoadingIndicator' ;
1415import TimelineEvents from './timeline-events' ;
1516import PendingApprovals from './pending-approvals' ;
1617
1718import './styles.scss' ;
1819
20+
21+ const FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL = _ . get ( config , 'TIMELINE.FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL' , 0 ) ;
1922function TimelineWallContainer ( props ) {
2023 const [ tab , setTab ] = useState ( 0 ) ;
24+ const fetchingApprovalsInterval = useRef ( null ) ;
2125 const [ showRightFilterMobile , setShowRightFilterMobile ] = useState ( false ) ;
2226 const [ selectedFilterValue , setSelectedFilterValue ] = useState ( {
2327 year : 0 ,
@@ -36,6 +40,7 @@ function TimelineWallContainer(props) {
3640 getAvatar,
3741 userAvatars,
3842 pendingApprovals,
43+ loadingApprovals,
3944 uploading,
4045 uploadResult,
4146 } = props ;
@@ -55,9 +60,25 @@ function TimelineWallContainer(props) {
5560 } , [ ] ) ;
5661
5762 useEffect ( ( ) => {
58- if ( authToken && isAdmin && ! pendingApprovals . length ) {
63+ if ( fetchingApprovalsInterval . current ) {
64+ clearInterval ( fetchingApprovalsInterval . current ) ;
65+ fetchingApprovalsInterval . current = null ;
66+ }
67+ if ( authToken && isAdmin ) {
5968 getPendingApprovals ( authToken ) ;
69+ if ( FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL ) {
70+ fetchingApprovalsInterval . current = setInterval ( ( ) => {
71+ getPendingApprovals ( authToken ) ;
72+ } , FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL ) ;
73+ }
6074 }
75+
76+ return ( ) => {
77+ if ( fetchingApprovalsInterval . current ) {
78+ clearInterval ( fetchingApprovalsInterval . current ) ;
79+ fetchingApprovalsInterval . current = null ;
80+ }
81+ } ;
6182 } , [ isAdmin ] ) ;
6283
6384 useEffect ( ( ) => {
@@ -72,7 +93,7 @@ function TimelineWallContainer(props) {
7293 } , [ events ] ) ;
7394
7495 useEffect ( ( ) => {
75- if ( ( pendingApprovals || [ ] ) . length ) {
96+ if ( pendingApprovals . length ) {
7697 _ . uniqBy ( pendingApprovals , 'createdBy' ) . forEach ( ( eventItem ) => {
7798 const photoURL = _ . get ( userAvatars , eventItem . createdBy ) ;
7899 if ( ! photoURL ) {
@@ -205,8 +226,10 @@ function TimelineWallContainer(props) {
205226 />
206227 < React . Fragment >
207228 {
208- loading ? (
209- < LoadingIndicator />
229+ loadingApprovals ? (
230+ < LoadingIndicator
231+ styleName = { cn ( { hide : tab === 0 } ) }
232+ />
210233 ) : (
211234 < PendingApprovals
212235 events = { pendingApprovals }
@@ -232,6 +255,7 @@ TimelineWallContainer.defaultProps = {
232255 auth : null ,
233256 isAdmin : false ,
234257 loading : false ,
258+ loadingApprovals : false ,
235259 uploading : false ,
236260 uploadResult : '' ,
237261 events : [ ] ,
@@ -246,6 +270,7 @@ TimelineWallContainer.propTypes = {
246270 auth : PT . shape ( ) ,
247271 isAdmin : PT . bool ,
248272 loading : PT . bool ,
273+ loadingApprovals : PT . bool ,
249274 uploading : PT . bool ,
250275 uploadResult : PT . string ,
251276 events : PT . arrayOf ( PT . shape ( ) ) ,
@@ -264,11 +289,13 @@ const mapStateToProps = state => ({
264289 } ,
265290 isAdmin : state . timelineWall . isAdmin ,
266291 loading : state . timelineWall . loading ,
292+ loadingApprovals : state . timelineWall . loadingApprovals
293+ && ! ( state . timelineWall . pendingApprovals || [ ] ) . length ,
267294 uploading : state . timelineWall . uploading ,
268295 uploadResult : state . timelineWall . uploadResult ,
269296 events : state . timelineWall . events ,
270297 userAvatars : state . timelineWall . userAvatars ,
271- pendingApprovals : state . timelineWall . pendingApprovals ,
298+ pendingApprovals : state . timelineWall . pendingApprovals || [ ] ,
272299} ) ;
273300
274301function mapDispatchToProps ( dispatch ) {
0 commit comments