]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/workstation/workstations/workstations.component.ts
LP#1775466 Angular(6) base application
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / workstation / workstations / workstations.component.ts
1 import {Component, OnInit, ViewChild} from '@angular/core';
2 import {Router, ActivatedRoute} from '@angular/router';
3 import {StoreService} from '@eg/core/store.service';
4 import {IdlObject} from '@eg/core/idl.service';
5 import {NetService} from '@eg/core/net.service';
6 import {PermService} from '@eg/core/perm.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {OrgService} from '@eg/core/org.service';
9 import {EventService} from '@eg/core/event.service';
10 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
11
12 // Slim version of the WS that's stored in the cache.
13 interface Workstation {
14     id: number;
15     name: string;
16     owning_lib: number;
17 }
18
19 @Component({
20   templateUrl: 'workstations.component.html'
21 })
22 export class WorkstationsComponent implements OnInit {
23
24     selectedName: string;
25     workstations: Workstation[] = [];
26     removeWorkstation: string;
27     newOwner: IdlObject;
28     newName: string;
29     defaultName: string;
30
31     @ViewChild('workstationExistsDialog')
32     private wsExistsDialog: ConfirmDialogComponent;
33
34     // Org selector options.
35     hideOrgs: number[];
36     disableOrgs: number[];
37     orgOnChange = (org: IdlObject): void => {
38         this.newOwner = org;
39     }
40
41     constructor(
42         private router: Router,
43         private route: ActivatedRoute,
44         private evt: EventService,
45         private net: NetService,
46         private store: StoreService,
47         private auth: AuthService,
48         private org: OrgService,
49         private perm: PermService
50     ) {}
51
52     ngOnInit() {
53         this.workstations = this.store.getLocalItem('eg.workstation.all') || [];
54         this.defaultName = this.store.getLocalItem('eg.workstation.default');
55         this.selectedName = this.auth.workstation() || this.defaultName;
56         const rm = this.route.snapshot.paramMap.get('remove');
57         if (rm) {
58             this.removeSelected(this.removeWorkstation = rm);
59         }
60
61         // TODO: use the org selector limitPerm option
62         this.perm.hasWorkPermAt(['REGISTER_WORKSTATION'], true)
63         .then(perms => {
64             // Disable org units that cannot have users and any
65             // that this user does not have work perms for.
66             this.disableOrgs =
67                 this.org.filterList({canHaveUsers : false}, true)
68                 .concat(this.org.filterList(
69                     {notInList : perms.REGISTER_WORKSTATION}, true));
70         });
71     }
72
73     selected(): Workstation {
74         return this.workstations.filter(
75           ws => ws.name === this.selectedName)[0];
76     }
77
78     useNow(): void {
79         if (this.selected()) {
80             this.router.navigate(['/staff/login'],
81                 {queryParams: {workstation: this.selected().name}});
82         }
83     }
84
85     setDefault(): void {
86       if (this.selected()) {
87             this.defaultName = this.selected().name;
88             this.store.setLocalItem('eg.workstation.default', this.defaultName);
89         }
90     }
91
92     removeSelected(name?: string): void {
93         if (!name) {
94             name = this.selected().name;
95         }
96
97         this.workstations = this.workstations.filter(w => w.name !== name);
98         this.store.setLocalItem('eg.workstation.all', this.workstations);
99
100         if (this.defaultName === name) {
101             this.defaultName = null;
102             this.store.removeLocalItem('eg.workstation.default');
103         }
104     }
105
106     canDeleteSelected(): boolean {
107         return true;
108     }
109
110     registerWorkstation(): void {
111         console.log(`Registering new workstation ` +
112             `"${this.newName}" at ${this.newOwner.shortname()}`);
113
114         this.newName = this.newOwner.shortname() + '-' + this.newName;
115
116         this.registerWorkstationApi().then(
117             wsId => this.registerWorkstationLocal(wsId),
118             notOk => console.log('Workstation registration canceled/failed')
119         );
120     }
121
122     private handleCollision(): Promise<number> {
123         return new Promise((resolve, reject) => {
124             this.wsExistsDialog.open()
125             .then(
126                 confirmed => {
127                     this.registerWorkstationApi(true).then(
128                         wsId => resolve(wsId),
129                         notOk => reject(notOk)
130                     );
131                 },
132                 dismissed => reject(dismissed)
133             );
134         });
135     }
136
137
138     private registerWorkstationApi(override?: boolean): Promise<number> {
139         let method = 'open-ils.actor.workstation.register';
140         if (override) {
141             method += '.override';
142         }
143
144         return new Promise((resolve, reject) => {
145             this.net.request(
146                 'open-ils.actor', method,
147                 this.auth.token(), this.newName, this.newOwner.id()
148             ).subscribe(wsId => {
149                 const evt = this.evt.parse(wsId);
150                 if (evt) {
151                     if (evt.textcode === 'WORKSTATION_NAME_EXISTS') {
152                         this.handleCollision().then(
153                             id => resolve(id),
154                             notOk => reject(notOk)
155                         );
156                     } else {
157                         console.error(`Registration failed ${evt}`);
158                         reject();
159                     }
160                 } else {
161                    resolve(wsId);
162                 }
163             });
164         });
165     }
166
167     private registerWorkstationLocal(wsId: number) {
168         const ws: Workstation = {
169             id: wsId,
170             name: this.newName,
171             owning_lib: this.newOwner.id()
172         };
173
174         this.workstations.push(ws);
175         this.store.setLocalItem('eg.workstation.all', this.workstations);
176         this.newName = '';
177         // when registering our first workstation, mark it as the
178         // default and show it as selected in the ws selector.
179         if (this.workstations.length === 1) {
180             this.selectedName = ws.name;
181             this.setDefault();
182         }
183     }
184 }
185
186