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