LP1779158 Angular7 and ng-lint updates
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / vandelay / queue-list.component.ts
1 import {Component, OnInit, ViewChild} from '@angular/core';
2 import {Observable, of} from 'rxjs';
3 import {map} from 'rxjs/operators';
4 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
5 import {Pager} from '@eg/share/util/pager';
6 import {IdlObject} from '@eg/core/idl.service';
7 import {NetService} from '@eg/core/net.service';
8 import {AuthService} from '@eg/core/auth.service';
9 import {GridComponent} from '@eg/share/grid/grid.component';
10 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
11 import {VandelayService} from './vandelay.service';
12
13 @Component({
14   templateUrl: 'queue-list.component.html'
15 })
16 export class QueueListComponent {
17
18     queueType: string; // bib / auth / bib-acq
19     queueSource: GridDataSource;
20     deleteSelected: (rows: IdlObject[]) => void;
21
22     // points to the currently active grid.
23     queueGrid: GridComponent;
24
25     @ViewChild('bibQueueGrid') bibQueueGrid: GridComponent;
26     @ViewChild('authQueueGrid') authQueueGrid: GridComponent;
27
28     constructor(
29         private router: Router,
30         private route: ActivatedRoute,
31         private net: NetService,
32         private auth: AuthService,
33         private vandelay: VandelayService) {
34
35         this.queueType = 'bib';
36         this.queueSource = new GridDataSource();
37
38         // Reset queue grid offset
39         this.vandelay.queuePageOffset = 0;
40
41         // queue API does not support sorting
42         this.queueSource.getRows = (pager: Pager) => {
43             return this.loadQueues(pager);
44         };
45
46         this.deleteSelected = (queues: IdlObject[]) => {
47
48             // Serialize the deletes, especially if there are many of them
49             // because they can be bulky calls
50             const qtype = this.queueType;
51             const method = `open-ils.vandelay.${qtype}_queue.delete`;
52             const selected = queues.slice(0); // clone to be nice
53
54             const deleteNext = (idx: number) => {
55                 const queue = selected[idx];
56                 if (!queue) {
57                     this.currentGrid().reload();
58                     return Promise.resolve();
59                 }
60
61                 return this.net.request('open-ils.vandelay',
62                     method, this.auth.token(), queue.id()
63                 ).toPromise().then(() => deleteNext(++idx));
64             };
65
66             deleteNext(0);
67         };
68     }
69
70     currentGrid(): GridComponent {
71         // The active grid changes along with the queue type.
72         // The inactive grid will be set to null.
73         return this.bibQueueGrid || this.authQueueGrid;
74     }
75
76     rowActivated(queue) {
77         const url = `/staff/cat/vandelay/queue/${this.queueType}/${queue.id()}`;
78         this.router.navigate([url]);
79     }
80
81     queueTypeChanged($event) {
82         this.queueType = $event.id;
83         this.queueSource.reset();
84     }
85
86
87     loadQueues(pager: Pager): Observable<any> {
88
89         if (!this.queueType) {
90             return of();
91         }
92
93         const qtype = this.queueType.match(/bib/) ? 'bib' : 'authority';
94         const method = `open-ils.vandelay.${qtype}_queue.owner.retrieve`;
95
96         return this.net.request('open-ils.vandelay',
97             method, this.auth.token(), null, null,
98             {offset: pager.offset, limit: pager.limit}
99         );
100     }
101 }
102