11import angular from 'angular'
22import _ from 'lodash'
3+ import { loadUser } from '../../services/userv3.service.js'
34
45( function ( ) {
56 'use strict'
67
78 angular . module ( 'tc.layout' ) . controller ( 'HeaderController' , HeaderController )
89
9- HeaderController . $inject = [ '$state' , 'TcAuthService' , 'CONSTANTS' , 'logger' , '$rootScope' , 'UserService' , 'ProfileService' , 'NavService' ]
10+ HeaderController . $inject = [ '$scope' , '$ state', 'TcAuthService' , 'CONSTANTS' , 'logger' , '$rootScope' , 'UserService' , 'ProfileService' , 'NavService' ]
1011
11- function HeaderController ( $state , TcAuthService , CONSTANTS , logger , $rootScope , UserService , ProfileService , NavService ) {
12+ function HeaderController ( $scope , $ state, TcAuthService , CONSTANTS , logger , $rootScope , UserService , ProfileService , NavService ) {
1213 var vm = this
1314
1415 vm . constants = CONSTANTS
@@ -31,7 +32,7 @@ import _ from 'lodash'
3132 activate ( )
3233
3334 function activate ( ) {
34- initHeaderProps ( 'default' )
35+ validateAndInitHeader ( 'default' )
3536
3637 // List of events that might force header update
3738 angular . forEach ( [
@@ -40,37 +41,54 @@ import _ from 'lodash'
4041 CONSTANTS . EVENT_PROFILE_UPDATED
4142 ] , function ( event ) {
4243 $rootScope . $on ( event , function ( ) {
43- initHeaderProps ( event )
44+ validateAndInitHeader ( event )
4445 } )
4546 } )
4647 }
4748
48- function initHeaderProps ( event ) {
49- logger . debug ( event + ' triggered header update.' )
50-
49+ /**
50+ * Validates login state and then initiate header update. If user is not loaded yet, i.e.,
51+ * page is reloaded or page is loaded for the first time, currentUser is not set yet. In this
52+ * case we try to load user first and if we are able to load user, update header properties
53+ * otherwise it assumes the page to be loaded without login.
54+ */
55+ function validateAndInitHeader ( event ) {
5156 vm . isAuth = TcAuthService . isAuthenticated ( )
52-
5357 if ( vm . isAuth ) {
54- vm . userHandle = UserService . getUserIdentity ( ) . handle
55-
56- vm . userMenu = [
57- { 'sref' : 'dashboard' , 'text' : 'DASHBOARD' , 'icon' : require ( '../../../assets/images/nav/dashboard.svg' ) } ,
58- { 'sref' : 'profile.about' , 'srefParams' : { 'userHandle' : vm . userHandle } , 'text' : 'MY PROFILE' , 'icon' : require ( '../../../assets/images/nav/profile.svg' ) } ,
59- { 'href' : vm . constants . COMMUNITY_URL + '/PactsMemberServlet?module=PaymentHistory&full_list=false' , 'text' : 'PAYMENTS' , 'icon' : require ( '../../../assets/images/nav/wallet.svg' ) } ,
60- { 'sref' : 'settings.profile' , 'text' : 'SETTINGS' , 'icon' : require ( '../../../assets/images/nav/settings.svg' ) }
61- ]
62-
63- ProfileService . getUserProfile ( vm . userHandle )
64- . then ( function ( data ) {
65- vm . profile = data
66- vm . userHandleColor = ProfileService . getUserHandleColor ( vm . profile )
67- } )
68- . catch ( function ( err ) {
69- logger . error ( 'Unable to get user profile data' , err )
58+ initHeaderProps ( event )
59+ } else {
60+ loadUser ( ) . then ( function ( token ) {
61+ // update auth flag
62+ vm . isAuth = TcAuthService . isAuthenticated ( )
63+ initHeaderProps ( event )
64+ } , function ( error ) {
65+ // do nothing, just show non logged in state of navigation bar
7066 } )
7167 }
7268 }
7369
70+ function initHeaderProps ( event ) {
71+ logger . debug ( event + ' triggered header update.' )
72+
73+ vm . userHandle = UserService . getUserIdentity ( ) . handle
74+
75+ vm . userMenu = [
76+ { 'sref' : 'dashboard' , 'text' : 'DASHBOARD' , 'icon' : require ( '../../../assets/images/nav/dashboard.svg' ) } ,
77+ { 'sref' : 'profile.about' , 'srefParams' : { 'userHandle' : vm . userHandle } , 'text' : 'MY PROFILE' , 'icon' : require ( '../../../assets/images/nav/profile.svg' ) } ,
78+ { 'href' : vm . constants . COMMUNITY_URL + '/PactsMemberServlet?module=PaymentHistory&full_list=false' , 'text' : 'PAYMENTS' , 'icon' : require ( '../../../assets/images/nav/wallet.svg' ) } ,
79+ { 'sref' : 'settings.profile' , 'text' : 'SETTINGS' , 'icon' : require ( '../../../assets/images/nav/settings.svg' ) }
80+ ]
81+
82+ return ProfileService . getUserProfile ( vm . userHandle )
83+ . then ( function ( data ) {
84+ vm . profile = data
85+ vm . userHandleColor = ProfileService . getUserHandleColor ( vm . profile )
86+ } )
87+ . catch ( function ( err ) {
88+ logger . error ( 'Unable to get user profile data' , err )
89+ } )
90+ }
91+
7492 function selectedGroup ( ) {
7593 return _ . get ( NavService , 'selectedTopLevelItem' , null )
7694 }
0 commit comments