LP1823041 Angular dialogs return observables
[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().subscribe(override => {
125                 if (override) {
126                     this.registerWorkstationApi(true).then(
127                         wsId => resolve(wsId),
128                         notOk => reject(notOk)
129                     );
130                 }
131             });
132         });
133     }
134
135
136     private registerWorkstationApi(override?: boolean): Promise<number> {
137         let method = 'open-ils.actor.workstation.register';
138         if (override) {
139             method += '.override';
140         }
141
142         return new Promise((resolve, reject) => {
143             this.net.request(
144                 'open-ils.actor', method,
145                 this.auth.token(), this.newName, this.newOwner.id()
146             ).subscribe(wsId => {
147                 const evt = this.evt.parse(wsId);
148                 if (evt) {
149                     if (evt.textcode === 'WORKSTATION_NAME_EXISTS') {
150                         this.handleCollision().then(
151                             id => resolve(id),
152                             notOk => reject(notOk)
153                         );
154                     } else {
155                         console.error(`Registration failed ${evt}`);
156                         reject();
157                     }
158                 } else {
159                    resolve(wsId);
160                 }
161             });
162         });
163     }
164
165     private registerWorkstationLocal(wsId: number) {
166         const ws: Workstation = {
167             id: wsId,
168             name: this.newName,
169             owning_lib: this.newOwner.id()
170         };
171
172         this.workstations.push(ws);
173         this.store.setLocalItem('eg.workstation.all', this.workstations);
174         this.newName = '';
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;
179             this.setDefault();
180         }
181     }
182 }
183
184