LP#1901072 Menus Don't Recognize Max Recent Patrons Setting
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / staff / nav.component.ts
1 import {Component, OnInit, OnDestroy, ViewChild} from '@angular/core';
2 import {ActivatedRoute, Router} from '@angular/router';
3 import {Location} from '@angular/common';
4 import {Subscription} from 'rxjs';
5 import {OrgService} from '@eg/core/org.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {LocaleService} from '@eg/core/locale.service';
9 import {PrintService} from '@eg/share/print/print.service';
10 import {StoreService} from '@eg/core/store.service';
11 import {NetRequest, NetService} from '@eg/core/net.service';
12 import {OpChangeComponent} from '@eg/staff/share/op-change/op-change.component';
13 import {PermService} from '@eg/core/perm.service';
14
15 @Component({
16     selector: 'eg-staff-nav-bar',
17     styleUrls: ['nav.component.css'],
18     templateUrl: 'nav.component.html'
19 })
20
21 export class StaffNavComponent implements OnInit, OnDestroy {
22
23     // Locales that have Angular staff translations
24     locales: any[];
25     currentLocale: any;
26
27     // When active, show a link to the traditional staff catalog
28     showTraditionalCatalog = true;
29     showAngularAcq: boolean;
30     curbsideEnabled: boolean;
31     showAngularCirc = false;
32     maxRecentPatrons: number = 1;
33
34     @ViewChild('navOpChange', {static: false}) opChange: OpChangeComponent;
35     permFailedSub: Subscription;
36
37     constructor(
38         private router: Router,
39         private store: StoreService,
40         private net: NetService,
41         private org: OrgService,
42         private auth: AuthService,
43         private perm: PermService,
44         private pcrud: PcrudService,
45         private locale: LocaleService,
46         private printer: PrintService
47     ) {
48         this.locales = [];
49     }
50
51     ngOnInit() {
52
53         this.locale.supportedLocales().subscribe(
54             l => this.locales.push(l),
55             err => {},
56             () => {
57                 this.currentLocale = this.locales.filter(
58                     l => l.code() === this.locale.currentLocaleCode())[0];
59             }
60         );
61
62         // NOTE: this can eventually go away.
63         // Avoid attempts to fetch org settings if the user has not yet
64         // logged in (e.g. this is the login page).
65         if (this.user()) {
66             // Note these are all pre-cached by our resolver.
67             // Batching not required.
68             this.org.settings('ui.staff.traditional_catalog.enabled')
69             .then(settings => this.showTraditionalCatalog =
70                 Boolean(settings['ui.staff.traditional_catalog.enabled']));
71
72             this.org.settings('circ.curbside')
73             .then(settings => this.curbsideEnabled =
74                 Boolean(settings['circ.curbside']));
75
76             this.org.settings('ui.staff.max_recent_patrons')
77             .then(settings => this.maxRecentPatrons =
78                 settings['ui.staff.max_recent_patrons'] ?? 1)
79
80             // Do we show the angular circ menu?
81             // TODO remove these once Angular Circ takes over.
82             const angSet = 'ui.staff.angular_circ.enabled';
83             const angPerm = 'ACCESS_ANGULAR_CIRC';
84
85             this.org.settings(angSet).then(s => {
86                 if (s[angSet]) {
87                     return this.perm.hasWorkPermHere([angPerm])
88                     .then(perms => perms[angPerm]);
89                 } else {
90                     return false;
91                 }
92             }).then(enable => this.showAngularCirc = enable);
93         }
94
95         // Wire up our op-change component as the general purpose
96         // permission failed handler.
97         this.net.permFailedHasHandler = true;
98         this.permFailedSub =
99             this.net.permFailed$.subscribe(
100                 (req: NetRequest) => this.opChange.escalateRequest(req));
101     }
102
103     ngOnDestroy() {
104         if (this.permFailedSub) {
105             this.permFailedSub.unsubscribe();
106         }
107     }
108
109     user() {
110         return this.auth.user() ? this.auth.user().usrname() : '';
111     }
112
113     user_id() {
114         return this.auth.user() ? this.auth.user().id() : '';
115     }
116
117     workstation() {
118         return this.auth.user() ? this.auth.workstation() : '';
119     }
120
121     ws_ou() {
122         return this.auth.user() ? this.auth.user().ws_ou() : '';
123     }
124
125     setLocale(locale: any) {
126         this.locale.setLocale(locale.code());
127     }
128
129     opChangeActive(): boolean {
130         return this.auth.opChangeIsActive();
131     }
132
133     // Broadcast to all tabs that we're logging out.
134     // Redirect to the login page, which performs the remaining
135     // logout duties.
136     logout(): void {
137         this.auth.broadcastLogout();
138         this.router.navigate(['/staff/login']);
139     }
140
141     reprintLast() {
142         this.printer.reprintLast();
143     }
144
145     retrieveLastRecord() {
146         const recId = this.store.getLocalItem('eg.cat.last_record_retrieved');
147         if (recId) {
148             this.router.navigate(['/staff/catalog/record/' + recId]);
149         }
150     }
151 }
152
153