1- import { SyntheticEvent , useCallback , useEffect , useState } from 'react' ;
1+ import { SyntheticEvent , useCallback , useEffect , useRef , useState } from 'react' ;
22import { useHistory , useLocation } from 'react-router-dom' ;
33import { Popup } from 'semantic-ui-react' ;
44import classNames from 'classnames' ;
@@ -9,14 +9,14 @@ import { PROJECT_PATH } from 'router/routes';
99import { getUserInfo } from 'services/user' ;
1010import { getApprovalList } from 'services/approval' ;
1111import { IUser } from 'interfaces/member' ;
12+ import { IApprovalList } from 'interfaces/approval' ;
1213import { I18NContainer } from 'hooks' ;
1314import { APPROVAL_ROUTE_LIST , PROJECT_ROUTE_LIST , SETTING_ROUTE_LIST } from 'constants/pathname' ;
1415import logo from 'images/logo.svg' ;
1516import logoWhite from 'images/logo-white.svg' ;
1617import { HeaderContainer } from './hooks' ;
1718import { EventTrack } from 'utils/track' ;
1819import styles from './pageHeader.module.scss' ;
19- import { IApprovalList } from 'interfaces/approval' ;
2020
2121const PROJECT_NAV = 'projects' ;
2222const SETTING_NAV = 'settings' ;
@@ -27,7 +27,7 @@ const PageHeader = () => {
2727 const history = useHistory ( ) ;
2828 const location = useLocation ( ) ;
2929 const intl = useIntl ( ) ;
30- const { userInfo , saveUserInfo } = HeaderContainer . useContainer ( ) ;
30+ const { approvalCount , saveUserInfo, saveApprovalCount } = HeaderContainer . useContainer ( ) ;
3131
3232 const [ selectedNav , setSelectedNav ] = useState < string > ( '' ) ;
3333 const [ account , setAccount ] = useState < string > ( '' ) ;
@@ -40,6 +40,8 @@ const PageHeader = () => {
4040 setI18n
4141 } = I18NContainer . useContainer ( ) ;
4242
43+ const timer : { current : NodeJS . Timeout | null } = useRef ( null ) ;
44+
4345 const headerCls = classNames (
4446 styles [ 'header' ] ,
4547 {
@@ -85,43 +87,48 @@ const PageHeader = () => {
8587 return ( ) => window . removeEventListener ( 'click' , handler ) ;
8688 } , [ menuOpen , helpMenuOpen , i18nMenuOpen ] ) ;
8789
88- useEffect ( ( ) => {
89- let result : IUser ;
9090
91+ useEffect ( ( ) => {
9192 getUserInfo < IUser > ( ) . then ( res => {
9293 const { success } = res ;
9394 if ( success ) {
94- const { data } = res ;
95- if ( data ) {
96- setAccount ( data ?. account ) ;
97- saveUserInfo ( {
98- ...data ,
99- approvalCount : userInfo . approvalCount ,
100- } ) ;
101- EventTrack . setUserId ( data . account ) ;
102- result = data ;
95+ if ( res . data ) {
96+ setAccount ( res . data . account ) ;
97+ saveUserInfo ( res . data ) ;
98+ EventTrack . setUserId ( res . data . account ) ;
10399 }
104100 } else {
105101 message . error ( intl . formatMessage ( { id : 'header.getuser.error.text' } ) ) ;
106102 }
107103 } ) ;
104+ } , [ intl , saveUserInfo ] ) ;
108105
106+ const initApprovalList = useCallback ( ( ) => {
109107 getApprovalList < IApprovalList > ( {
110108 pageIndex : 0 ,
111- status : 'PENDING' ,
109+ status : [ 'PENDING' ] ,
112110 type : 'APPROVAL' ,
113111 keyword : '' ,
114112 } ) . then ( res => {
115113 const { success, data } = res ;
116114 if ( success && data ) {
117115 const { totalElements } = data ;
118- saveUserInfo ( {
119- ...result ,
120- approvalCount : totalElements ,
121- } ) ;
116+ saveApprovalCount ( totalElements ) ;
122117 }
123118 } ) ;
124- } , [ intl , userInfo . approvalCount , saveUserInfo ] ) ;
119+ } , [ saveApprovalCount ] ) ;
120+
121+ useEffect ( ( ) => {
122+ if ( timer . current ) {
123+ clearInterval ( timer . current ) ;
124+ }
125+ initApprovalList ( ) ;
126+ timer . current = setInterval ( initApprovalList , 5000 ) ;
127+
128+ return ( ) => {
129+ clearInterval ( timer . current as NodeJS . Timeout ) ;
130+ } ;
131+ } , [ initApprovalList ] ) ;
125132
126133 useEffect ( ( ) => {
127134 const reg = new RegExp ( '[^/]+$' ) ;
@@ -192,7 +199,7 @@ const PageHeader = () => {
192199
193200 < div className = { approvalCls } onClick = { handleGotoApproval } >
194201 < FormattedMessage id = 'approvals.center' />
195- { userInfo . approvalCount !== 0 && < span className = { styles . count } > { userInfo . approvalCount > 99 ? '99+' : userInfo . approvalCount } </ span > }
202+ { approvalCount !== 0 && < span className = { styles . count } > { approvalCount > 99 ? '99+' : approvalCount } </ span > }
196203 </ div >
197204 </ div >
198205 < div className = { 'user' } >
0 commit comments