11/* global tcUniNav */
2- import React , { useEffect , useRef } from 'react' ;
2+ import React , { useEffect , useMemo , useRef } from 'react' ;
33import PT from 'prop-types' ;
44import { connect } from 'react-redux' ;
55import { config } from 'topcoder-react-utils' ;
66import _ from 'lodash' ;
77import { getInitials , getSubPageConfiguration } from '../utils/url' ;
88
9- const headerElId = 'uninav-headerNav' ;
9+ let counter = 0 ;
10+ const headerElIdTmpl = 'uninav-headerNav' ;
1011
1112const TopcoderHeader = ( { auth } ) => {
1213 const uniNavInitialized = useRef ( false ) ;
@@ -15,6 +16,32 @@ const TopcoderHeader = ({ auth }) => {
1516 const isAuthenticated = ! ! authToken ;
1617 const authURLs = config . HEADER_AUTH_URLS ;
1718 const headerRef = useRef ( ) ;
19+ const headerElId = useRef ( `${ headerElIdTmpl } -${ counter } ` ) ;
20+
21+ const navType = useMemo ( ( ) => {
22+ let { type } = getSubPageConfiguration ( ) ;
23+
24+ if ( typeof window === 'undefined' ) {
25+ return type ;
26+ }
27+
28+ // If url contains navTool url parameter. Overwrite settings with parameter.
29+ const url = new URL ( window . location . href ) ;
30+ const urlParams = new URLSearchParams ( url . search ) ;
31+ if ( urlParams . get ( 'navTool' ) ) {
32+ type = urlParams . get ( 'navTool' ) ;
33+ }
34+
35+ // if there's a stored nav type in session storage, retrieve it and overwrite type
36+ const sessionNavType = sessionStorage . getItem ( 'uni-nav[navType]' ) ;
37+ if ( sessionNavType && ( sessionNavType === 'tool' || sessionNavType === 'marketing' ) ) {
38+ type = sessionNavType ;
39+ }
40+
41+ // store nav type for current session
42+ sessionStorage . setItem ( 'uni-nav[navType]' , type ) ;
43+ return type ;
44+ } , [ ] ) ;
1845
1946 const navigationUserInfo = {
2047 ...user ,
@@ -27,21 +54,13 @@ const TopcoderHeader = ({ auth }) => {
2754 }
2855
2956 uniNavInitialized . current = true ;
57+ counter += 1 ;
3058
3159 const regSource = window . location . pathname . split ( '/' ) [ 1 ] ;
3260 const retUrl = encodeURIComponent ( window . location . href ) ;
3361
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 ,
4564 toolName : getSubPageConfiguration ( ) . toolName ,
4665 toolRoot : getSubPageConfiguration ( ) . toolRoot ,
4766 signOut : ( ) => {
@@ -54,15 +73,15 @@ const TopcoderHeader = ({ auth }) => {
5473 window . location = `${ authURLs . location . replace ( '%S' , retUrl ) . replace ( 'member?' , '#!/member?' ) } &mode=signUp®Source=${ regSource } ` ;
5574 } ,
5675 } ) ;
57- } , [ ] ) ;
76+ } , [ navType ] ) ;
5877
5978 useEffect ( ( ) => {
60- tcUniNav ( 'update' , headerElId , {
79+ tcUniNav ( 'update' , headerElId . current , {
6180 user : isAuthenticated ? navigationUserInfo : null ,
6281 } ) ;
6382 } , [ isAuthenticated , navigationUserInfo ] ) ;
6483
65- return < div id = { headerElId } ref = { headerRef } /> ;
84+ return < div id = { headerElId . current } ref = { headerRef } /> ;
6685} ;
6786
6887TopcoderHeader . defaultProps = {
0 commit comments