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.store.getWorkstations()
56 this.workstations = wsList || [];
57 return this.store.getDefaultWorkstation();
60 this.defaultName = def;
61 this.selectedName = this.auth.workstation() || this.defaultName;
62 const rm = this.route.snapshot.paramMap.get('remove');
63 if (rm) { this.removeSelected(this.removeWorkstation = rm); }
66 // TODO: use the org selector limitPerm option
67 this.perm.hasWorkPermAt(['REGISTER_WORKSTATION'], true)
69 // Disable org units that cannot have users and any
70 // that this user does not have work perms for.
72 this.org.filterList({canHaveUsers : false}, true)
73 .concat(this.org.filterList(
74 {notInList : perms.REGISTER_WORKSTATION}, true));
78 selected(): Workstation {
79 return this.workstations.filter(
80 ws => ws.name === this.selectedName)[0];
84 if (this.selected()) {
85 this.router.navigate(['/staff/login'],
86 {queryParams: {workstation: this.selected().name}});
91 if (this.selected()) {
92 this.defaultName = this.selected().name;
93 this.store.setDefaultWorkstation(this.defaultName);
97 removeSelected(name?: string): void {
99 name = this.selected().name;
102 this.workstations = this.workstations.filter(w => w.name !== name);
103 this.store.setWorkstations(this.workstations);
105 if (this.defaultName === name) {
106 this.defaultName = null;
107 this.store.removeWorkstations();
111 canDeleteSelected(): boolean {
115 registerWorkstation(): void {
116 console.log(`Registering new workstation ` +
117 `"${this.newName}" at ${this.newOwner.shortname()}`);
119 this.newName = this.newOwner.shortname() + '-' + this.newName;
121 this.registerWorkstationApi().then(
122 wsId => this.registerWorkstationLocal(wsId),
123 notOk => console.log('Workstation registration canceled/failed')
127 private handleCollision(): Promise<number> {
128 return new Promise((resolve, reject) => {
129 this.wsExistsDialog.open().subscribe(override => {
131 this.registerWorkstationApi(true).then(
132 wsId => resolve(wsId),
133 notOk => reject(notOk)
141 private registerWorkstationApi(override?: boolean): Promise<number> {
142 let method = 'open-ils.actor.workstation.register';
144 method += '.override';
147 return new Promise((resolve, reject) => {
149 'open-ils.actor', method,
150 this.auth.token(), this.newName, this.newOwner.id()
151 ).subscribe(wsId => {
152 const evt = this.evt.parse(wsId);
154 if (evt.textcode === 'WORKSTATION_NAME_EXISTS') {
155 this.handleCollision().then(
157 notOk => reject(notOk)
160 console.error(`Registration failed ${evt}`);
170 private registerWorkstationLocal(wsId: number) {
171 const ws: Workstation = {
174 owning_lib: this.newOwner.id()
177 this.workstations.push(ws);
178 this.store.setWorkstations(this.workstations);
180 // when registering our first workstation, mark it as the
181 // default and show it as selected in the ws selector.
182 if (this.workstations.length === 1) {
183 this.selectedName = ws.name;