1 import {Component, ViewChild} from '@angular/core';
2 import {Observable, of} from 'rxjs';
3 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
4 import {Pager} from '@eg/share/util/pager';
5 import {IdlObject} from '@eg/core/idl.service';
6 import {NetService} from '@eg/core/net.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {GridComponent} from '@eg/share/grid/grid.component';
9 import {GridDataSource} from '@eg/share/grid/grid';
10 import {VandelayService} from './vandelay.service';
13 templateUrl: 'queue-list.component.html'
15 export class QueueListComponent {
17 queueType: string; // bib / auth / bib-acq
18 queueSource: GridDataSource;
19 deleteSelected: (rows: IdlObject[]) => void;
21 // points to the currently active grid.
22 queueGrid: GridComponent;
24 @ViewChild('bibQueueGrid', { static: false }) bibQueueGrid: GridComponent;
25 @ViewChild('authQueueGrid', { static: false }) authQueueGrid: GridComponent;
28 private router: Router,
29 private route: ActivatedRoute,
30 private net: NetService,
31 private auth: AuthService,
32 private vandelay: VandelayService) {
34 this.queueType = 'bib';
35 this.queueSource = new GridDataSource();
37 // Reset queue grid offset
38 this.vandelay.queuePageOffset = 0;
40 // queue API does not support sorting
41 this.queueSource.getRows = (pager: Pager) => {
42 return this.loadQueues(pager);
45 this.deleteSelected = (queues: IdlObject[]) => {
47 // Serialize the deletes, especially if there are many of them
48 // because they can be bulky calls
49 const qtype = this.queueType;
50 const method = `open-ils.vandelay.${qtype}_queue.delete`;
51 const selected = queues.slice(0); // clone to be nice
53 const deleteNext = (idx: number) => {
54 const queue = selected[idx];
56 this.currentGrid().reload();
57 return Promise.resolve();
60 return this.net.request('open-ils.vandelay',
61 method, this.auth.token(), queue.id()
62 ).toPromise().then(() => deleteNext(++idx));
69 currentGrid(): GridComponent {
70 // The active grid changes along with the queue type.
71 // The inactive grid will be set to null.
72 return this.bibQueueGrid || this.authQueueGrid;
76 const url = `/staff/cat/vandelay/queue/${this.queueType}/${queue.id()}`;
77 this.router.navigate([url]);
80 queueTypeChanged($event) {
81 if ($event.id === this.queueType) { return; }
82 this.queueType = $event.id;
83 this.queueSource.reset();
87 loadQueues(pager: Pager): Observable<any> {
89 if (!this.queueType) {
93 const qtype = this.queueType.match(/bib/) ? 'bib' : 'authority';
94 const method = `open-ils.vandelay.${qtype}_queue.owner.retrieve`;
96 return this.net.request('open-ils.vandelay',
97 method, this.auth.token(), null, null,
98 {offset: pager.offset, limit: pager.limit}