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 import {HatchService} from '@eg/core/hatch.service';
13 // Slim version of the WS that's stored in the cache.
14 interface Workstation {
21 templateUrl: 'workstations.component.html'
23 export class WorkstationsComponent implements OnInit {
26 workstations: Workstation[] = [];
27 removeWorkstation: string;
32 @ViewChild('workstationExistsDialog', { static: true })
33 private wsExistsDialog: ConfirmDialogComponent;
35 // Org selector options.
37 disableOrgs: number[];
38 orgOnChange = (org: IdlObject): void => {
43 private router: Router,
44 private route: ActivatedRoute,
45 private evt: EventService,
46 private net: NetService,
47 private store: StoreService,
48 private auth: AuthService,
49 private org: OrgService,
50 private hatch: HatchService,
51 private perm: PermService
55 this.store.getWorkstations()
58 this.workstations = wsList || [];
60 // Populate the new WS name field with the hostname when available.
61 return this.setNewName();
64 ok => this.store.getDefaultWorkstation()
67 this.defaultName = def;
68 this.selectedName = this.auth.workstation() || this.defaultName;
69 const rm = this.route.snapshot.paramMap.get('remove');
70 if (rm) { this.removeSelected(this.removeWorkstation = rm); }
73 // TODO: use the org selector limitPerm option
74 this.perm.hasWorkPermAt(['REGISTER_WORKSTATION'], true)
76 // Disable org units that cannot have users and any
77 // that this user does not have work perms for.
79 this.org.filterList({canHaveUsers : false}, true)
80 .concat(this.org.filterList(
81 {notInList : perms.REGISTER_WORKSTATION}, true));
85 selected(): Workstation {
86 return this.workstations.filter(
87 ws => ws.name === this.selectedName)[0];
91 if (this.selected()) {
92 this.router.navigate(['/staff/login'],
93 {queryParams: {workstation: this.selected().name}});
98 if (this.selected()) {
99 this.defaultName = this.selected().name;
100 this.store.setDefaultWorkstation(this.defaultName);
104 removeSelected(name?: string): void {
106 name = this.selected().name;
109 this.workstations = this.workstations.filter(w => w.name !== name);
110 this.store.setWorkstations(this.workstations);
114 this.hatch.hostname().then(name => this.newName = name || '');
117 canDeleteSelected(): boolean {
121 registerWorkstation(): void {
122 console.log('Registering new workstation ' +
123 `"${this.newName}" at ${this.newOwner.shortname()}`);
125 this.newName = this.newOwner.shortname() + '-' + this.newName;
127 this.registerWorkstationApi().then(
128 wsId => this.registerWorkstationLocal(wsId),
129 notOk => console.log('Workstation registration canceled/failed')
133 private handleCollision(): Promise<number> {
134 return new Promise((resolve, reject) => {
135 this.wsExistsDialog.open().subscribe(override => {
137 this.registerWorkstationApi(true).then(
138 wsId => resolve(wsId),
139 notOk => reject(notOk)
147 private registerWorkstationApi(override?: boolean): Promise<number> {
148 let method = 'open-ils.actor.workstation.register';
150 method += '.override';
153 return new Promise((resolve, reject) => {
155 'open-ils.actor', method,
156 this.auth.token(), this.newName, this.newOwner.id()
157 ).subscribe(wsId => {
158 const evt = this.evt.parse(wsId);
160 if (evt.textcode === 'WORKSTATION_NAME_EXISTS') {
161 this.handleCollision().then(
163 notOk => reject(notOk)
166 console.error(`Registration failed ${evt}`);
176 private registerWorkstationLocal(wsId: number) {
177 const ws: Workstation = {
180 owning_lib: this.newOwner.id()
183 this.workstations.push(ws);
184 this.store.setWorkstations(this.workstations);
186 // when registering our first workstation, mark it as the
187 // default and show it as selected in the ws selector.
188 if (this.workstations.length === 1) {
189 this.selectedName = ws.name;