888c8a57f40412ee4cbb7038927dae653261b369
[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} from 'rxjs/Observable';
3 import 'rxjs/add/observable/of';
4 import {map} from 'rxjs/operators/map';
5 import {Router, ActivatedRoute, ParamMap} from '@angular/router';              
6 import {Pager} from '@eg/share/util/pager';                                    
7 import {IdlObject} from '@eg/core/idl.service';
8 import {NetService} from '@eg/core/net.service';
9 import {AuthService} from '@eg/core/auth.service';
10 import {GridComponent} from '@eg/share/grid/grid.component';
11 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
12 import {VandelayService} from './vandelay.service';
13
14 @Component({
15   templateUrl: 'queue-list.component.html'
16 })
17 export class QueueListComponent {
18
19     queueType: string; // bib / auth / bib-acq
20     queueSource: GridDataSource;
21     deleteSelected: (rows: IdlObject[]) => void;
22
23     // points to the currently active grid.
24     queueGrid: GridComponent;
25
26     @ViewChild('bibQueueGrid') bibQueueGrid: GridComponent;
27     @ViewChild('authQueueGrid') authQueueGrid: GridComponent;
28
29     constructor(
30         private router: Router,
31         private route: ActivatedRoute,
32         private net: NetService,
33         private auth: AuthService,
34         private vandelay: VandelayService) {
35
36         this.queueType = 'bib';
37         this.queueSource = new GridDataSource();
38
39         // Reset queue grid offset
40         this.vandelay.queuePageOffset = 0;
41
42         // queue API does not support sorting
43         this.queueSource.getRows = (pager: Pager) => {
44             return this.loadQueues(pager);
45         }
46
47         this.deleteSelected = (queues: IdlObject[]) => {
48
49             // Serialize the deletes, especially if there are many of them
50             // because they can be bulky calls
51             const qtype = this.queueType;
52             const method = `open-ils.vandelay.${qtype}_queue.delete`;
53
54             const deleteNext = (queues: IdlObject[], idx: number) => {
55                 const queue = queues[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(queues, ++idx));
64             }
65
66             deleteNext(queues, 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 Observable.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