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';
12 // Slim version of the WS that's stored in the cache.
13 interface Workstation {
20 templateUrl: 'workstations.component.html'
22 export class WorkstationsComponent implements OnInit {
25 workstations: Workstation[] = [];
26 removeWorkstation: string;
31 @ViewChild('workstationExistsDialog', { static: true })
32 private wsExistsDialog: ConfirmDialogComponent;
34 // Org selector options.
36 disableOrgs: number[];
37 orgOnChange = (org: IdlObject): void => {
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
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');
58 this.removeSelected(this.removeWorkstation = rm);
61 // TODO: use the org selector limitPerm option
62 this.perm.hasWorkPermAt(['REGISTER_WORKSTATION'], true)
64 // Disable org units that cannot have users and any
65 // that this user does not have work perms for.
67 this.org.filterList({canHaveUsers : false}, true)
68 .concat(this.org.filterList(
69 {notInList : perms.REGISTER_WORKSTATION}, true));
73 selected(): Workstation {
74 return this.workstations.filter(
75 ws => ws.name === this.selectedName)[0];
79 if (this.selected()) {
80 this.router.navigate(['/staff/login'],
81 {queryParams: {workstation: this.selected().name}});
86 if (this.selected()) {
87 this.defaultName = this.selected().name;
88 this.store.setLocalItem('eg.workstation.default', this.defaultName);
92 removeSelected(name?: string): void {
94 name = this.selected().name;
97 this.workstations = this.workstations.filter(w => w.name !== name);
98 this.store.setLocalItem('eg.workstation.all', this.workstations);
100 if (this.defaultName === name) {
101 this.defaultName = null;
102 this.store.removeLocalItem('eg.workstation.default');
106 canDeleteSelected(): boolean {
110 registerWorkstation(): void {
111 console.log(`Registering new workstation ` +
112 `"${this.newName}" at ${this.newOwner.shortname()}`);
114 this.newName = this.newOwner.shortname() + '-' + this.newName;
116 this.registerWorkstationApi().then(
117 wsId => this.registerWorkstationLocal(wsId),
118 notOk => console.log('Workstation registration canceled/failed')
122 private handleCollision(): Promise<number> {
123 return new Promise((resolve, reject) => {
124 this.wsExistsDialog.open().subscribe(override => {
126 this.registerWorkstationApi(true).then(
127 wsId => resolve(wsId),
128 notOk => reject(notOk)
136 private registerWorkstationApi(override?: boolean): Promise<number> {
137 let method = 'open-ils.actor.workstation.register';
139 method += '.override';
142 return new Promise((resolve, reject) => {
144 'open-ils.actor', method,
145 this.auth.token(), this.newName, this.newOwner.id()
146 ).subscribe(wsId => {
147 const evt = this.evt.parse(wsId);
149 if (evt.textcode === 'WORKSTATION_NAME_EXISTS') {
150 this.handleCollision().then(
152 notOk => reject(notOk)
155 console.error(`Registration failed ${evt}`);
165 private registerWorkstationLocal(wsId: number) {
166 const ws: Workstation = {
169 owning_lib: this.newOwner.id()
172 this.workstations.push(ws);
173 this.store.setLocalItem('eg.workstation.all', this.workstations);
175 // when registering our first workstation, mark it as the
176 // default and show it as selected in the ws selector.
177 if (this.workstations.length === 1) {
178 this.selectedName = ws.name;