]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/prefs.component.ts
LP 2061136 follow-up: ng lint --fix
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / prefs.component.ts
1 import {Component, OnInit, ViewChild} from '@angular/core';
2 import {IdlObject} from '@eg/core/idl.service';
3 import {StaffCatalogService} from './catalog.service';
4 import {ServerStoreService} from '@eg/core/server-store.service';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {StringComponent} from '@eg/share/string/string.component';
7 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
8
9 /* Component for managing catalog preferences */
10
11 const CATALOG_PREFS = [
12     'eg.search.search_lib',
13     'eg.search.pref_lib',
14     'eg.search.adv_pane',
15     'eg.catalog.results.count',
16     'eg.staffcat.exclude_electronic',
17     'eg.staffcat.course_materials_selector',
18     'circ.course_materials_opt_in'
19 ];
20
21 @Component({
22     templateUrl: 'prefs.component.html'
23 })
24 export class PreferencesComponent implements OnInit {
25
26     settings: Object = {};
27
28     @ViewChild('successMsg', {static: false}) successMsg: StringComponent;
29     @ViewChild('failMsg', {static: false}) failMsg: StringComponent;
30
31     constructor(
32         private store: ServerStoreService,
33         private toast: ToastService,
34         private staffCat: StaffCatalogService,
35     ) {}
36
37     ngOnInit() {
38         this.staffCat.createContext();
39
40         // Pre-fetched by the resolver.
41         return this.store.getItemBatch(CATALOG_PREFS)
42             .then(settings => this.settings = settings);
43     }
44
45     showCoursePreferences() {
46         return this.settings['circ.course_materials_opt_in'];
47     }
48
49     orgChanged(org: IdlObject, setting: string) {
50         const localVar = setting === 'eg.search.search_lib' ?
51             'defaultSearchOrg' : 'prefOrg';
52
53         if (org.id()) {
54             this.updateValue(setting, org ? org.id() : null)
55                 .then(val => this.staffCat[localVar] = val);
56         }
57     }
58
59     paneChanged(entry: ComboboxEntry) {
60         this.updateValue('eg.search.adv_pane', entry ? entry.id : null)
61             .then(value => this.staffCat.defaultTab = value);
62     }
63
64     countChanged() {
65         this.updateValue('eg.catalog.results.count',
66             this.settings['eg.catalog.results.count'] || null)
67             .then(value => {
68                 // eslint-disable-next-line no-magic-numbers
69                 this.staffCat.searchContext.pager.limit = value || 20;
70             });
71     }
72
73     checkboxChanged(setting: string) {
74         const value = this.settings[setting];
75         this.updateValue(setting, value || null);
76
77         if (setting === 'eg.staffcat.exclude_electronic') {
78             this.staffCat.showExcludeElectronic = value;
79         }
80     }
81
82     updateValue(setting: string, value: any): Promise<any> {
83         const promise = (value === null) ?
84             this.store.removeItem(setting) :
85             this.store.setItem(setting, value);
86
87         return promise
88             .then(_ => this.toast.success(this.successMsg.text))
89             .then(_ => value);
90     }
91
92     hasNoHistory(): boolean {
93         return history.length === 0;
94     }
95
96     goBack() {
97         history.back();
98     }
99 }
100