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' ;
7
7
import { 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' ;
8
10
9
11
@Component ( {
10
12
selector : 'app-sidebar' ,
11
13
templateUrl : './sidebar.component.html' ,
12
14
styleUrls : [ './sidebar.component.scss' ] ,
13
15
} )
14
- export class SidebarComponent implements OnInit {
15
-
16
+ export class SidebarComponent implements OnInit , OnDestroy {
16
17
itemsSidebar : ItemSidebar [ ] = [ ] ;
17
18
navStart ;
19
+ FTSwitchGroup$ : Subscription ;
18
20
19
21
constructor (
20
- private azureAdB2CService : AzureAdB2CService ,
21
22
private router : Router ,
23
+ private userInfoService : UserInfoService ,
24
+ private azureAdB2CService : AzureAdB2CService ,
22
25
private featureManagerService : FeatureManagerService ,
26
+ private featureSwitchGroup : FeatureSwitchGroupService
23
27
) {
24
28
this . navStart = this . router . events . pipe (
25
- filter ( evt => evt instanceof NavigationStart )
29
+ filter ( ( evt ) => evt instanceof NavigationStart )
26
30
) as Observable < NavigationStart > ;
27
31
}
28
32
29
33
ngOnInit ( ) : void {
30
34
this . toggleSideBar ( ) ;
31
- this . getSidebarItems ( ) ;
35
+ this . FTSwitchGroup$ = this . getSidebarItems ( ) . subscribe ( ) ;
32
36
this . toggleListTechnologies ( this . itemsSidebar ) ;
33
37
this . highlightMenuOption ( this . router . routerState . snapshot . url ) ;
34
- this . navStart . subscribe ( evt => {
38
+ this . navStart . subscribe ( ( evt ) => {
35
39
this . highlightMenuOption ( evt . url ) ;
36
40
} ) ;
37
41
}
42
+ ngOnDestroy ( ) : void {
43
+ this . FTSwitchGroup$ . unsubscribe ( ) ;
44
+ }
38
45
39
46
toggleSideBar ( ) {
40
47
$ ( '#menu-toggle' ) . on ( 'click' , ( e ) => {
@@ -43,42 +50,51 @@ export class SidebarComponent implements OnInit {
43
50
} ) ;
44
51
}
45
52
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
+ ) ;
62
78
}
63
79
64
- toggleListTechnologies ( itemsSidebar : ItemSidebar [ ] ) {
80
+ toggleListTechnologies ( itemsSidebar : ItemSidebar [ ] ) {
65
81
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
+ } ) ;
78
94
}
79
95
80
96
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 ) ) ;
83
99
}
84
100
}
0 commit comments