1
- import { AfterContentChecked , AfterContentInit , Component , Input , OnDestroy } from '@angular/core' ;
2
- import { ActivatedRoute } from '@angular/router' ;
3
- import { Subscription } from 'rxjs' ;
1
+ import {
2
+ AfterContentChecked ,
3
+ AfterContentInit ,
4
+ Component ,
5
+ EventEmitter ,
6
+ Input ,
7
+ OnDestroy ,
8
+ OnInit ,
9
+ Output
10
+ } from '@angular/core' ;
11
+ import { Store } from '@ngrx/store' ;
12
+ import { ActivatedRoute , NavigationEnd , Router } from '@angular/router' ;
13
+ import { Subscription , filter } from 'rxjs' ;
14
+
15
+ import { GlobalActions } from '@mm-actions/global' ;
16
+ import { Selectors } from '@mm-selectors/index' ;
17
+ import { AuthService } from '@mm-services/auth.service' ;
18
+ import { SessionService } from '@mm-services/session.service' ;
19
+ import { TelemetryService } from '@mm-services/telemetry.service' ;
20
+ import { TargetAggregatesService } from '@mm-services/target-aggregates.service' ;
21
+ import { UserSettingsService } from '@mm-services/user-settings.service' ;
22
+ import { OLD_REPORTS_FILTER_PERMISSION } from '@mm-modules/reports/reports-filters.component' ;
23
+ import { OLD_ACTION_BAR_PERMISSION } from '@mm-components/actionbar/actionbar.component' ;
24
+ import { AGGREGATE_TARGETS_ID } from '@mm-services/analytics-modules.service' ;
4
25
5
26
@Component ( {
6
27
selector : 'mm-analytics-filters' ,
7
28
templateUrl : './analytics-filter.component.html'
8
29
} )
9
- export class AnalyticsFilterComponent implements AfterContentInit , AfterContentChecked , OnDestroy {
30
+ export class AnalyticsFilterComponent implements AfterContentInit , AfterContentChecked , OnInit , OnDestroy {
10
31
@Input ( ) analyticsModules : any [ ] = [ ] ;
32
+ @Output ( ) toggleFilter : EventEmitter < any > = new EventEmitter ( ) ;
33
+
34
+ private globalActions ;
11
35
activeModule ;
12
36
subscriptions : Subscription = new Subscription ( ) ;
37
+ showFilterButton ;
38
+ isOpen = false ;
13
39
14
40
constructor (
41
+ private store : Store ,
15
42
private route : ActivatedRoute ,
16
- ) { }
43
+ private router : Router ,
44
+ private authService : AuthService ,
45
+ private sessionService : SessionService ,
46
+ private telemetryService : TelemetryService ,
47
+ private targetAggregatesService : TargetAggregatesService ,
48
+ private userSettingsService : UserSettingsService
49
+ ) {
50
+ this . globalActions = new GlobalActions ( store ) ;
51
+ }
52
+
53
+ ngOnInit ( ) {
54
+ this . canDisplayFilterButton ( ) ;
55
+ this . subscribeToRouteChanges ( ) ;
56
+ this . subscribeToStore ( ) ;
57
+ }
17
58
18
59
ngAfterContentInit ( ) {
19
60
const subscription = this . route . url . subscribe ( ( ) => this . setActiveModule ( ) ) ;
@@ -28,11 +69,72 @@ export class AnalyticsFilterComponent implements AfterContentInit, AfterContentC
28
69
29
70
ngOnDestroy ( ) : void {
30
71
this . subscriptions . unsubscribe ( ) ;
72
+ this . globalActions . clearSidebarFilter ( ) ;
73
+ }
74
+
75
+ private subscribeToStore ( ) {
76
+ const subscription = this . store
77
+ . select ( Selectors . getSidebarFilter )
78
+ . subscribe ( ( filterState ) => this . isOpen = filterState ?. isOpen ?? false ) ;
79
+ this . subscriptions . add ( subscription ) ;
80
+ }
81
+
82
+ private getCurrentModuleId ( ) {
83
+ return this . route . snapshot ?. firstChild ?. data ?. moduleId ;
31
84
}
32
85
33
86
private setActiveModule ( ) {
34
- this . activeModule = this . analyticsModules ?. find ( module => {
35
- return module . id === this . route . snapshot ?. firstChild ?. data ?. moduleId ;
36
- } ) ;
87
+ const currentModuleId = this . getCurrentModuleId ( ) ;
88
+ this . activeModule = this . analyticsModules ?. find ( module => module . id === currentModuleId ) ;
89
+ }
90
+
91
+ private subscribeToRouteChanges ( ) {
92
+ const routeSubscription = this . router . events
93
+ . pipe ( filter ( event => event instanceof NavigationEnd ) )
94
+ . subscribe ( ( ) => this . canDisplayFilterButton ( ) ) ;
95
+ this . subscriptions . add ( routeSubscription ) ;
96
+ }
97
+
98
+ private checkPermissions ( ) {
99
+ const permissions = [
100
+ OLD_REPORTS_FILTER_PERMISSION ,
101
+ OLD_ACTION_BAR_PERMISSION
102
+ ] ;
103
+
104
+ return this . authService
105
+ . has ( permissions )
106
+ . then ( ( permissions ) => permissions === false ) ;
107
+ }
108
+
109
+ private isTargetAggregates ( ) {
110
+ return this . getCurrentModuleId ( ) === AGGREGATE_TARGETS_ID ;
111
+ }
112
+
113
+ private isTargetAggregateEnabled ( ) {
114
+ return this . targetAggregatesService . isEnabled ( ) ;
115
+ }
116
+
117
+ private async canDisplayFilterButton ( ) {
118
+ const isAdmin = this . sessionService . isAdmin ( ) ;
119
+ const [ hasMultipleFacilities , checkPermissions , isTargetAggregateEnabled ] = await Promise . all ( [
120
+ this . userSettingsService . hasMultipleFacilities ( ) ,
121
+ this . checkPermissions ( ) ,
122
+ this . isTargetAggregateEnabled ( ) ,
123
+ ] ) ;
124
+
125
+ this . showFilterButton = ! isAdmin &&
126
+ hasMultipleFacilities &&
127
+ checkPermissions &&
128
+ this . isTargetAggregates ( ) &&
129
+ isTargetAggregateEnabled ;
130
+ }
131
+
132
+ openSidebar ( ) {
133
+ this . isOpen = ! this . isOpen ;
134
+ this . globalActions . setSidebarFilter ( { isOpen : this . isOpen } ) ;
135
+ if ( this . isOpen ) {
136
+ // Counting every time the user opens the sidebar filter in analytics_targets_aggregrate tab.
137
+ this . telemetryService . record ( 'sidebar_filter:analytics_target_aggregates:open' ) ;
138
+ }
37
139
}
38
140
}
0 commit comments