1
1
/* global tcUniNav */
2
- import React , { useEffect , useRef } from 'react' ;
2
+ import React , { useEffect , useMemo , useRef } from 'react' ;
3
3
import PT from 'prop-types' ;
4
4
import { connect } from 'react-redux' ;
5
5
import { config } from 'topcoder-react-utils' ;
6
6
import _ from 'lodash' ;
7
7
import { getInitials , getSubPageConfiguration } from '../utils/url' ;
8
8
9
- const headerElId = 'uninav-headerNav' ;
9
+ let counter = 0 ;
10
+ const headerElIdTmpl = 'uninav-headerNav' ;
10
11
11
12
const TopcoderHeader = ( { auth } ) => {
12
13
const uniNavInitialized = useRef ( false ) ;
@@ -15,6 +16,32 @@ const TopcoderHeader = ({ auth }) => {
15
16
const isAuthenticated = ! ! authToken ;
16
17
const authURLs = config . HEADER_AUTH_URLS ;
17
18
const headerRef = useRef ( ) ;
19
+ const headerElId = useRef ( `${ headerElIdTmpl } -${ counter } ` ) ;
20
+
21
+ const navType = useMemo ( ( ) => {
22
+ if ( typeof window === 'undefined' ) {
23
+ return '' ;
24
+ }
25
+
26
+ let { type } = getSubPageConfiguration ( ) ;
27
+
28
+ // if there's a stored nav type in session storage, retrieve it and overwrite type
29
+ const sessionNavType = sessionStorage . getItem ( 'uni-nav[navType]' ) ;
30
+ if ( sessionNavType && ( sessionNavType === 'tool' || sessionNavType === 'marketing' ) ) {
31
+ type = sessionNavType ;
32
+ }
33
+
34
+ // If url contains navTool url parameter. Overwrite settings with parameter.
35
+ const url = new URL ( window . location . href ) ;
36
+ const urlParams = new URLSearchParams ( url . search ) ;
37
+ if ( urlParams . get ( 'navTool' ) ) {
38
+ type = urlParams . get ( 'navTool' ) ;
39
+ }
40
+
41
+ // store nav type for current session
42
+ sessionStorage . setItem ( 'uni-nav[navType]' , type ) ;
43
+ return type ;
44
+ } , [ ] ) ;
18
45
19
46
const navigationUserInfo = {
20
47
...user ,
@@ -27,21 +54,13 @@ const TopcoderHeader = ({ auth }) => {
27
54
}
28
55
29
56
uniNavInitialized . current = true ;
57
+ counter += 1 ;
30
58
31
59
const regSource = window . location . pathname . split ( '/' ) [ 1 ] ;
32
60
const retUrl = encodeURIComponent ( window . location . href ) ;
33
61
34
- let { type } = getSubPageConfiguration ( ) ;
35
-
36
- // If url contains navTool url parameter. Overwrite settings with parameter.
37
- const url = new URL ( window . location . href ) ;
38
- const urlParams = new URLSearchParams ( url . search ) ;
39
- if ( urlParams . get ( 'navTool' ) ) {
40
- type = urlParams . get ( 'navTool' ) ;
41
- }
42
-
43
- tcUniNav ( 'init' , headerElId , {
44
- type,
62
+ tcUniNav ( 'init' , headerElId . current , {
63
+ type : navType ,
45
64
toolName : getSubPageConfiguration ( ) . toolName ,
46
65
toolRoot : getSubPageConfiguration ( ) . toolRoot ,
47
66
signOut : ( ) => {
@@ -54,15 +73,15 @@ const TopcoderHeader = ({ auth }) => {
54
73
window . location = `${ authURLs . location . replace ( '%S' , retUrl ) . replace ( 'member?' , '#!/member?' ) } &mode=signUp®Source=${ regSource } ` ;
55
74
} ,
56
75
} ) ;
57
- } , [ ] ) ;
76
+ } , [ navType ] ) ;
58
77
59
78
useEffect ( ( ) => {
60
- tcUniNav ( 'update' , headerElId , {
79
+ tcUniNav ( 'update' , headerElId . current , {
61
80
user : isAuthenticated ? navigationUserInfo : null ,
62
81
} ) ;
63
82
} , [ isAuthenticated , navigationUserInfo ] ) ;
64
83
65
- return < div id = { headerElId } ref = { headerRef } /> ;
84
+ return < div id = { headerElId . current } ref = { headerRef } /> ;
66
85
} ;
67
86
68
87
TopcoderHeader . defaultProps = {
0 commit comments