1- import { AzureAdB2CService } from 'src/app/modules/login/services/azure.ad.b2c.service' ;
2- import { Component , OnInit } from '@angular/core' ;
3- import { ItemSidebar } from './models/item-sidebar.model' ;
4- import { NavigationStart , Router } from '@angular/router' ;
5- import { Observable } from 'rxjs' ;
6- import { filter } from 'rxjs/operators' ;
1+ import { AzureAdB2CService } from 'src/app/modules/login/services/azure.ad.b2c.service' ;
2+ import { Component , OnDestroy , OnInit } from '@angular/core' ;
3+ import { ItemSidebar } from './models/item-sidebar.model' ;
4+ import { NavigationStart , Router } from '@angular/router' ;
5+ import { Observable , of , Subscription } from 'rxjs' ;
6+ import { filter , map , mergeMap } from 'rxjs/operators' ;
77import { FeatureManagerService } from '../../feature-toggles/feature-toggle-manager.service' ;
8+ import { UserInfoService } from 'src/app/modules/user/services/user-info.service' ;
9+ import { FeatureSwitchGroupService } from '../../feature-toggles/switch-group/feature-switch-group.service' ;
810
911@Component ( {
1012 selector : 'app-sidebar' ,
1113 templateUrl : './sidebar.component.html' ,
1214 styleUrls : [ './sidebar.component.scss' ] ,
1315} )
14- export class SidebarComponent implements OnInit {
15-
16+ export class SidebarComponent implements OnInit , OnDestroy {
1617 itemsSidebar : ItemSidebar [ ] = [ ] ;
1718 navStart ;
19+ FTSwitchGroup$ : Subscription ;
1820
1921 constructor (
20- private azureAdB2CService : AzureAdB2CService ,
2122 private router : Router ,
23+ private userInfoService : UserInfoService ,
24+ private azureAdB2CService : AzureAdB2CService ,
2225 private featureManagerService : FeatureManagerService ,
26+ private featureSwitchGroup : FeatureSwitchGroupService
2327 ) {
2428 this . navStart = this . router . events . pipe (
25- filter ( evt => evt instanceof NavigationStart )
29+ filter ( ( evt ) => evt instanceof NavigationStart )
2630 ) as Observable < NavigationStart > ;
2731 }
2832
2933 ngOnInit ( ) : void {
3034 this . toggleSideBar ( ) ;
31- this . getSidebarItems ( ) ;
35+ this . FTSwitchGroup$ = this . getSidebarItems ( ) . subscribe ( ) ;
3236 this . toggleListTechnologies ( this . itemsSidebar ) ;
3337 this . highlightMenuOption ( this . router . routerState . snapshot . url ) ;
34- this . navStart . subscribe ( evt => {
38+ this . navStart . subscribe ( ( evt ) => {
3539 this . highlightMenuOption ( evt . url ) ;
3640 } ) ;
3741 }
42+ ngOnDestroy ( ) : void {
43+ this . FTSwitchGroup$ . unsubscribe ( ) ;
44+ }
3845
3946 toggleSideBar ( ) {
4047 $ ( '#menu-toggle' ) . on ( 'click' , ( e ) => {
@@ -43,42 +50,51 @@ export class SidebarComponent implements OnInit {
4350 } ) ;
4451 }
4552
46- getSidebarItems ( ) {
47- if ( this . azureAdB2CService . isAdmin ( ) ) {
48- this . itemsSidebar = [
49- { route : '/time-clock' , icon : 'fas fa-clock' , text : 'Time Clock' , active : false } ,
50- { route : '/time-entries' , icon : 'fas fa-list-alt' , text : 'Time Entries' , active : false } ,
51- { route : '/reports' , icon : 'fas fa-chart-pie' , text : 'Reports' , active : false } ,
52- { route : '/activities-management' , icon : 'fas fa-file-alt' , text : 'Activities' , active : false } ,
53- { route : '/customers-management' , icon : 'fas fa-user' , text : 'Customers' , active : false } ,
54- { route : '/users' , icon : 'fas fa-user' , text : 'Users' , active : false } ,
55- ] ;
56- } else {
57- this . itemsSidebar = [
58- { route : '/time-clock' , icon : 'fas fa-clock' , text : 'Time Clock' , active : false } ,
59- { route : '/time-entries' , icon : 'fas fa-list-alt' , text : 'Time Entries' , active : false } ,
60- ] ;
61- }
53+ getSidebarItems ( ) : Observable < void > {
54+ const isAdminBasedInRole = of ( this . azureAdB2CService . isAdmin ( ) ) ;
55+ const isAdminBasedInGroup = this . userInfoService . isAdmin ( ) ;
56+ return this . featureSwitchGroup . isActivated ( ) . pipe (
57+ mergeMap ( ( enabled ) => {
58+ return enabled ? isAdminBasedInGroup : isAdminBasedInRole ;
59+ } ) ,
60+ map ( ( isAdmin ) => {
61+ if ( isAdmin ) {
62+ this . itemsSidebar = [
63+ { route : '/time-clock' , icon : 'fas fa-clock' , text : 'Time Clock' , active : false } ,
64+ { route : '/time-entries' , icon : 'fas fa-list-alt' , text : 'Time Entries' , active : false } ,
65+ { route : '/reports' , icon : 'fas fa-chart-pie' , text : 'Reports' , active : false } ,
66+ { route : '/activities-management' , icon : 'fas fa-file-alt' , text : 'Activities' , active : false } ,
67+ { route : '/customers-management' , icon : 'fas fa-user' , text : 'Customers' , active : false } ,
68+ { route : '/users' , icon : 'fas fa-user' , text : 'Users' , active : false } ,
69+ ] ;
70+ } else {
71+ this . itemsSidebar = [
72+ { route : '/time-clock' , icon : 'fas fa-clock' , text : 'Time Clock' , active : false } ,
73+ { route : '/time-entries' , icon : 'fas fa-list-alt' , text : 'Time Entries' , active : false } ,
74+ ] ;
75+ }
76+ } )
77+ ) ;
6278 }
6379
64- toggleListTechnologies ( itemsSidebar : ItemSidebar [ ] ) {
80+ toggleListTechnologies ( itemsSidebar : ItemSidebar [ ] ) {
6581 this . featureManagerService
66- . isToggleEnabledForUser ( 'ui-list-technologies' )
67- . subscribe ( ( enabled ) => {
68- if ( enabled === true ) {
69- const listTechnologiesItem = {
70- route : '/technology-report' ,
71- icon : 'fas fa-user' ,
72- text : 'Technology Report' ,
73- active : false
74- } ;
75- itemsSidebar . push ( listTechnologiesItem ) ;
76- }
77- } ) ;
82+ . isToggleEnabledForUser ( 'ui-list-technologies' )
83+ . subscribe ( ( enabled ) => {
84+ if ( enabled === true ) {
85+ const listTechnologiesItem = {
86+ route : '/technology-report' ,
87+ icon : 'fas fa-user' ,
88+ text : 'Technology Report' ,
89+ active : false ,
90+ } ;
91+ itemsSidebar . push ( listTechnologiesItem ) ;
92+ }
93+ } ) ;
7894 }
7995
8096 highlightMenuOption ( route ) {
81- this . itemsSidebar . map ( item => item . active = false ) ;
82- this . itemsSidebar . filter ( item => item . route === route ) . map ( item => item . active = true ) ;
97+ this . itemsSidebar . map ( ( item ) => ( item . active = false ) ) ;
98+ this . itemsSidebar . filter ( ( item ) => item . route === route ) . map ( ( item ) => ( item . active = true ) ) ;
8399 }
84100}
0 commit comments