1
- /* global tcUniNav */
2
- import React , { useEffect , useMemo , useRef } from 'react ' ;
1
+ import React , { useMemo } from 'react' ;
2
+ import PT from 'prop-types ' ;
3
3
import { connect } from 'react-redux' ;
4
+ import { withRouter } from 'react-router-dom' ;
4
5
import { config } from 'topcoder-react-utils' ;
5
6
import LoadingIndicator from 'components/LoadingIndicator' ;
7
+ import _ from 'lodash' ;
8
+ import { MarketingNavigation , ToolNavigation } from 'uninav-react' ;
6
9
import { getSubPageConfiguration } from '../../utils/url' ;
7
- import { SSRPlaceholder } from '../../utils/SSR' ;
8
10
import './styles.scss' ;
9
11
10
- let counter = 0 ;
11
- const headerElIdTmpl = 'uninav-headerNav' ;
12
-
13
- const TopcoderHeader = ( ) => {
14
- const uniNavInitialized = useRef ( false ) ;
12
+ const TopcoderHeader = ( { auth, location } ) => {
13
+ const user = _ . get ( auth , 'profile' ) || { } ;
14
+ const authToken = _ . get ( auth , 'tokenV3' ) ;
15
+ const isAuthenticated = ! ! authToken ;
15
16
const authURLs = config . HEADER_AUTH_URLS ;
16
- const headerRef = useRef ( ) ;
17
- const headerElId = useRef ( `${ headerElIdTmpl } -${ counter } ` ) ;
18
-
19
- const navType = useMemo ( ( ) => {
20
- if ( typeof window === 'undefined' ) {
21
- return '' ;
22
- }
23
-
24
- let { type } = getSubPageConfiguration ( ) ;
25
-
26
- // if there's a stored nav type in session storage, retrieve it and overwrite type
27
- const sessionNavType = sessionStorage . getItem ( 'uni-nav[navType]' ) ;
28
- const url = new URL ( window . location . href ) ;
29
-
30
- // Only use the set sessionStorage value for navType on the /thrive paths, for now.
31
- // Probably will change in the future...
32
- if ( window . location . href . indexOf ( '/thrive' ) > - 1 && sessionNavType && ( sessionNavType === 'tool' || sessionNavType === 'marketing' ) ) {
33
- type = sessionNavType ;
17
+ const pageCfg = useMemo ( ( ) => getSubPageConfiguration ( location ) , [ location ] ) ;
18
+ const isSSR = typeof window === 'undefined' ;
19
+
20
+ const NavComponent = useMemo ( ( ) => {
21
+ let { type } = pageCfg ;
22
+
23
+ if ( ! isSSR ) {
24
+ // if there's a stored nav type in session storage, retrieve it and overwrite type
25
+ const sessionNavType = sessionStorage . getItem ( 'uni-nav[navType]' ) ;
26
+ if ( location . pathname . includes ( ) && sessionNavType && ( sessionNavType === 'tool' || sessionNavType === 'marketing' ) ) {
27
+ type = sessionNavType ;
28
+ }
34
29
}
35
30
36
31
// If url contains navTool url parameter. Overwrite settings with parameter.
37
- const urlParams = new URLSearchParams ( url . search ) ;
38
- if ( urlParams . get ( ' navTool' ) ) {
39
- type = urlParams . get ( ' navTool' ) ;
32
+ const navTool = ( location . search . match ( / n a v T o o l = ( t o o l | m a r k e t i n g ) / ) || [ ] ) [ 1 ] ;
33
+ if ( navTool ) {
34
+ type = navTool ;
40
35
}
41
36
42
- // store nav type for current session
43
- sessionStorage . setItem ( 'uni-nav[navType]' , type ) ;
44
- return type ;
45
- } , [ ] ) ;
46
-
47
- useEffect ( ( ) => {
48
- if ( uniNavInitialized . current ) {
49
- return ;
37
+ if ( ! isSSR ) {
38
+ // store nav type for current session
39
+ sessionStorage . setItem ( 'uni-nav[navType]' , type ) ;
50
40
}
51
- headerRef . current . id = headerElId . current ;
41
+ return type === 'marketing' ? MarketingNavigation : ToolNavigation ;
42
+ } , [ pageCfg , location ] ) ;
52
43
53
- uniNavInitialized . current = true ;
54
- counter += 1 ;
44
+ const { signOut, signIn, signUp } = useMemo ( ( ) => {
45
+ const regSource = ( location . pathname || '' ) . split ( '/' ) [ 1 ] ;
46
+ const retUrl = encodeURIComponent ( isSSR ? location . href : window . location . href ) ;
55
47
56
- const regSource = window . location . pathname . split ( '/' ) [ 1 ] ;
57
- const retUrl = encodeURIComponent ( window . location . href ) ;
58
-
59
- tcUniNav ( 'init' , headerElId . current , {
60
- type : navType ,
61
- toolName : getSubPageConfiguration ( ) . toolName ,
62
- toolRoot : getSubPageConfiguration ( ) . toolRoot ,
63
- user : 'auto' ,
48
+ return {
64
49
signOut : ( ) => {
65
50
window . location = `${ config . URL . BASE } /logout?ref=nav` ;
66
51
} ,
@@ -70,27 +55,39 @@ const TopcoderHeader = () => {
70
55
signUp : ( ) => {
71
56
window . location = `${ authURLs . location . replace ( '%S' , retUrl ) . replace ( 'member?' , '#!/member?' ) } &mode=signUp®Source=${ regSource } ` ;
72
57
} ,
73
- } ) ;
74
- } , [ navType ] ) ;
58
+ } ;
59
+ } , [ location ] ) ;
75
60
76
61
return (
77
- < div styleName = "header-container" id = { headerElId . current } ref = { headerRef } />
62
+ < div styleName = "header-container" >
63
+ < NavComponent
64
+ toolName = { pageCfg . toolName }
65
+ toolRoot = { pageCfg . toolRoot }
66
+ user = { isAuthenticated ? user : undefined }
67
+ signIn = { signIn }
68
+ signOut = { signOut }
69
+ signUp = { signUp }
70
+ currentLocation = { location . pathname }
71
+ >
72
+ < div styleName = "loader" >
73
+ < LoadingIndicator />
74
+ </ div >
75
+ </ NavComponent >
76
+ </ div >
78
77
) ;
79
78
} ;
79
+ TopcoderHeader . defaultProps = {
80
+ auth : { } ,
81
+ location : typeof window === 'undefined' ? { } : window . location ,
82
+ } ;
83
+
84
+ TopcoderHeader . propTypes = {
85
+ auth : PT . shape ( ) ,
86
+ location : PT . shape ( ) , // sent from withRouter
87
+ } ;
80
88
81
89
const mapStateToProps = state => ( {
82
90
auth : state . auth ,
83
91
} ) ;
84
92
85
- const TopcoderHeaderConnect = connect ( mapStateToProps , null ) ( TopcoderHeader ) ;
86
-
87
- const TopcoderHeaderPlaceholder = ( ) => (
88
- < div styleName = "header-container header-container-placeholder" >
89
- < LoadingIndicator />
90
- </ div >
91
- ) ;
92
-
93
- export default SSRPlaceholder ( ) (
94
- TopcoderHeaderConnect ,
95
- TopcoderHeaderPlaceholder ,
96
- ) ;
93
+ export default connect ( mapStateToProps , null ) ( withRouter ( TopcoderHeader ) ) ;
0 commit comments