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';
14 templateUrl: 'queue-list.component.html'
16 export class QueueListComponent {
18 queueType: string; // bib / auth / bib-acq
19 queueSource: GridDataSource;
20 deleteSelected: (rows: IdlObject[]) => void;
22 // points to the currently active grid.
23 queueGrid: GridComponent;
25 @ViewChild('bibQueueGrid') bibQueueGrid: GridComponent;
26 @ViewChild('authQueueGrid') authQueueGrid: GridComponent;
29 private router: Router,
30 private route: ActivatedRoute,
31 private net: NetService,
32 private auth: AuthService,
33 private vandelay: VandelayService) {
35 this.queueType = 'bib';
36 this.queueSource = new GridDataSource();
38 // Reset queue grid offset
39 this.vandelay.queuePageOffset = 0;
41 // queue API does not support sorting
42 this.queueSource.getRows = (pager: Pager) => {
43 return this.loadQueues(pager);
46 this.deleteSelected = (queues: IdlObject[]) => {
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
54 const deleteNext = (idx: number) => {
55 const queue = selected[idx];
57 this.currentGrid().reload();
58 return Promise.resolve();
61 return this.net.request('open-ils.vandelay',
62 method, this.auth.token(), queue.id()
63 ).toPromise().then(() => deleteNext(++idx));
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;
77 const url = `/staff/cat/vandelay/queue/${this.queueType}/${queue.id()}`;
78 this.router.navigate([url]);
81 queueTypeChanged($event) {
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}