11require ( './ProjectsToolBar.scss' )
2-
2+ /* global tcUniNav */
33import React , { Component } from 'react'
44import PropTypes from 'prop-types'
55import querystring from 'query-string'
66import { withRouter , Prompt } from 'react-router-dom'
77import { connect } from 'react-redux'
88import _ from 'lodash'
99import SearchBar from 'appirio-tech-react-components/components/SearchBar/SearchBar'
10- import NotificationsDropdown from '../NotificationsDropdown/NotificationsDropdownContainer'
1110import NewProjectNavLink from './NewProjectNavLink'
1211import MobileMenu from '../MobileMenu/MobileMenu'
1312import MobileMenuToggle from '../MobileMenu/MobileMenuToggle'
@@ -16,21 +15,34 @@ import { loadProjectsMetadata } from '../../actions/templates'
1615import { getNewProjectLink } from '../../helpers/projectHelper'
1716import { hasPermission } from '../../helpers/permissions'
1817import { PERMISSIONS } from '../../config/permissions'
18+ import { HEADER_AUTH_URLS_HREF , HEADER_AUTH_URLS_LOCATION , DOMAIN } from '../../config/constants'
19+ import { getInitials } from '../../helpers/format'
20+
21+ const HEADER_AUTH_URLS = {
22+ href : HEADER_AUTH_URLS_HREF ,
23+ location : HEADER_AUTH_URLS_LOCATION
24+ }
25+ const BASE = `https://www.${ DOMAIN } `
26+
27+ let uniqueId = 0
1928
2029class ProjectsToolBar extends Component {
2130
2231 constructor ( props ) {
2332 super ( props )
33+ uniqueId += 1
2434 this . state = {
2535 errorCreatingProject : false ,
2636 isMobileMenuOpen : false ,
27- isMobileSearchVisible : false
37+ isMobileSearchVisible : false ,
38+ headerId : uniqueId ,
2839 }
2940 this . applyFilters = this . applyFilters . bind ( this )
3041 this . handleTermChange = this . handleTermChange . bind ( this )
3142 this . handleSearch = this . handleSearch . bind ( this )
3243 this . toggleMobileMenu = this . toggleMobileMenu . bind ( this )
3344 this . onLeave = this . onLeave . bind ( this )
45+ this . uniNavInitialized = false
3446 }
3547
3648 componentWillMount ( ) {
@@ -71,6 +83,43 @@ class ProjectsToolBar extends Component {
7183 window . addEventListener ( 'beforeunload' , this . onLeave )
7284 }
7385
86+ componentDidUpdate ( ) {
87+ if ( ! ! this . state . headerId && ! this . uniNavInitialized ) {
88+ const user = this . props . user
89+ const navigationUserInfo = {
90+ ...user ,
91+ initials : getInitials ( user . firstName , user . lastName )
92+ }
93+ const authToken = user ? user . token : null
94+ this . uniNavInitialized = true
95+ const headerId = this . state . headerId
96+ const isAuthenticated = ! ! authToken
97+ const authURLs = HEADER_AUTH_URLS
98+
99+ const regSource = window . location . pathname . split ( '/' ) [ 1 ]
100+ const retUrl = encodeURIComponent ( window . location . href )
101+ tcUniNav ( 'init' , `headerNav-${ headerId } ` , {
102+ type : 'tool' ,
103+ toolName : 'Connect' ,
104+ toolRoot : '/' ,
105+ user : isAuthenticated ? navigationUserInfo : null ,
106+ signOut : ( ) => {
107+ window . location = `${ BASE } /logout?ref=nav`
108+ } ,
109+ signIn : ( ) => {
110+ window . location = `${ authURLs . location
111+ . replace ( '%S' , retUrl )
112+ . replace ( 'member?' , '#!/member?' ) } ®Source=${ regSource } `
113+ } ,
114+ signUp : ( ) => {
115+ window . location = `${ authURLs . location
116+ . replace ( '%S' , retUrl )
117+ . replace ( 'member?' , '#!/member?' ) } &mode=signUp®Source=${ regSource } `
118+ }
119+ } )
120+ }
121+ }
122+
74123 componentWillUnmount ( ) {
75124 window . removeEventListener ( 'beforeunload' , this . onLeave )
76125 const contentDiv = document . getElementById ( 'wrapper-main' )
@@ -146,8 +195,8 @@ class ProjectsToolBar extends Component {
146195 }
147196
148197 render ( ) {
149- const { renderLogoSection , userMenu , userRoles, user, mobileMenu, location , orgConfig } = this . props
150- const { isMobileMenuOpen, isMobileSearchVisible } = this . state
198+ const { userRoles, user, mobileMenu, orgConfig } = this . props
199+ const { isMobileMenuOpen, isMobileSearchVisible, headerId } = this . state
151200 const isLoggedIn = ! ! ( userRoles && userRoles . length )
152201
153202 const onLeaveMessage = this . onLeave ( ) || ''
@@ -158,8 +207,8 @@ class ProjectsToolBar extends Component {
158207 when = { ! ! onLeaveMessage }
159208 message = { onLeaveMessage }
160209 />
210+ < div id = { `headerNav-${ headerId } ` } />
161211 < div className = "primary-toolbar" >
162- { renderLogoSection ( ) }
163212 { isLoggedIn && ! hasPermission ( PERMISSIONS . SEARCH_PROJECTS ) && < div className = "projects-title-mobile" > MY PROJECTS</ div > }
164213 {
165214 isLoggedIn && hasPermission ( PERMISSIONS . SEARCH_PROJECTS ) &&
@@ -176,10 +225,6 @@ class ProjectsToolBar extends Component {
176225 }
177226 < div className = "actions" >
178227 { isLoggedIn && < NewProjectNavLink link = { getNewProjectLink ( orgConfig ) } /> }
179- { userMenu }
180- { /* pass location, to make sure that component is re-rendered when location is changed
181- it's necessary to hide notification dropdown on mobile when users uses browser history back/forward buttons */ }
182- { isLoggedIn && < NotificationsDropdown location = { location } /> }
183228 { isLoggedIn && < MobileMenuToggle onToggle = { this . toggleMobileMenu } /> }
184229 </ div >
185230 </ div >
0 commit comments