1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {Observable, Observer, of} from 'rxjs';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {NetService} from '@eg/core/net.service';
5 import {OrgService} from '@eg/core/org.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {Pager} from '@eg/share/util/pager';
8 import {ServerStoreService} from '@eg/core/server-store.service';
9 import {GridDataSource} from '@eg/share/grid/grid';
10 import {GridComponent} from '@eg/share/grid/grid.component';
11 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
12 import {MarkDamagedDialogComponent
13 } from '@eg/staff/share/holdings/mark-damaged-dialog.component';
14 import {MarkMissingDialogComponent
15 } from '@eg/staff/share/holdings/mark-missing-dialog.component';
16 import {HoldRetargetDialogComponent
17 } from '@eg/staff/share/holds/retarget-dialog.component';
18 import {HoldTransferDialogComponent} from './transfer-dialog.component';
19 import {HoldCancelDialogComponent} from './cancel-dialog.component';
20 import {HoldManageDialogComponent} from './manage-dialog.component';
21 import {PrintService} from '@eg/share/print/print.service';
23 /** Holds grid with access to detail page and other actions */
26 selector: 'eg-holds-grid',
27 templateUrl: 'grid.component.html'
29 export class HoldsGridComponent implements OnInit {
31 // If either are set/true, the pickup lib selector will display
32 @Input() initialPickupLib: number | IdlObject;
33 @Input() hidePickupLibFilter: boolean;
36 @Input() persistKey: string;
38 @Input() preFetchSetting: string;
40 @Input() printTemplate: string;
42 // If set, all holds are fetched on grid load and sorting/paging all
43 // happens in the client. If false, sorting and paging occur on
45 enablePreFetch: boolean;
47 // How to sort when no sort parameters have been applied
48 // via grid controls. This uses the eg-grid sort format:
49 // [{name: fname, dir: 'asc'}, {name: fname2, dir: 'desc'}]
50 @Input() defaultSort: any[];
52 mode: 'list' | 'detail' | 'manage' = 'list';
56 gridDataSource: GridDataSource;
59 transferTarget: number;
61 @ViewChild('holdsGrid') private holdsGrid: GridComponent;
62 @ViewChild('progressDialog')
63 private progressDialog: ProgressDialogComponent;
64 @ViewChild('transferDialog')
65 private transferDialog: HoldTransferDialogComponent;
66 @ViewChild('markDamagedDialog')
67 private markDamagedDialog: MarkDamagedDialogComponent;
68 @ViewChild('markMissingDialog')
69 private markMissingDialog: MarkMissingDialogComponent;
70 @ViewChild('retargetDialog')
71 private retargetDialog: HoldRetargetDialogComponent;
72 @ViewChild('cancelDialog')
73 private cancelDialog: HoldCancelDialogComponent;
74 @ViewChild('manageDialog')
75 private manageDialog: HoldManageDialogComponent;
79 @Input() set recordId(id: number) {
81 if (this.initDone) { // reload on update
82 this.holdsGrid.reload();
87 @Input() set userId(id: number) {
90 this.holdsGrid.reload();
94 // Include holds canceled on or after the provided date.
95 // If no value is passed, canceled holds are not displayed.
96 _showCanceledSince: Date;
97 @Input() set showCanceledSince(show: Date) {
98 this._showCanceledSince = show;
99 if (this.initDone) { // reload on update
100 this.holdsGrid.reload();
104 // Include holds fulfilled on or after hte provided date.
105 // If no value is passed, fulfilled holds are not displayed.
106 _showFulfilledSince: Date;
107 @Input() set showFulfilledSince(show: Date) {
108 this._showFulfilledSince = show;
109 if (this.initDone) { // reload on update
110 this.holdsGrid.reload();
115 private net: NetService,
116 private org: OrgService,
117 private store: ServerStoreService,
118 private auth: AuthService,
119 private printer: PrintService
121 this.gridDataSource = new GridDataSource();
122 this.enablePreFetch = null;
126 this.initDone = true;
127 this.pickupLib = this.org.get(this.initialPickupLib);
129 if (this.preFetchSetting) {
131 this.store.getItem(this.preFetchSetting).then(
132 applied => this.enablePreFetch = Boolean(applied)
137 if (!this.defaultSort) {
138 this.defaultSort = [{name: 'request_time', dir: 'asc'}];
141 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
142 sort = sort.length > 0 ? sort : this.defaultSort;
143 return this.fetchHolds(pager, sort);
147 // Returns true after all data/settings/etc required to render the
148 // grid have been fetched.
149 initComplete(): boolean {
150 return this.enablePreFetch !== null;
153 pickupLibChanged(org: IdlObject) {
154 this.pickupLib = org;
155 this.holdsGrid.reload();
158 preFetchHolds(apply: boolean) {
159 this.enablePreFetch = apply;
162 setTimeout(() => this.holdsGrid.reload());
165 if (this.preFetchSetting) {
167 this.store.setItem(this.preFetchSetting, apply);
171 applyFilters(): any {
172 const filters: any = {
173 is_staff_request: true,
174 fulfillment_time: this._showFulfilledSince ?
175 this._showFulfilledSince.toISOString() : null,
176 cancel_time: this._showCanceledSince ?
177 this._showCanceledSince.toISOString() : null,
180 if (this.pickupLib) {
182 this.org.descendants(this.pickupLib, true);
185 if (this._recordId) {
186 filters.record_id = this._recordId;
190 filters.usr_id = this._userId;
196 fetchHolds(pager: Pager, sort: any[]): Observable<any> {
198 // We need at least one filter.
199 if (!this._recordId && !this.pickupLib && !this._userId) {
203 const filters = this.applyFilters();
205 const orderBy: any = [];
206 if (sort.length > 0) {
207 sort.forEach(obj => {
208 const subObj: any = {};
209 subObj[obj.name] = {dir: obj.dir, nulls: 'last'};
210 orderBy.push(subObj);
214 const limit = this.enablePreFetch ? null : pager.limit;
215 const offset = this.enablePreFetch ? 0 : pager.offset;
217 let observer: Observer<any>;
218 const observable = new Observable(obs => observer = obs);
220 this.progressDialog.open();
221 this.progressDialog.update({value: 0, max: 1});
226 'open-ils.circ.hold.wide_hash.stream',
227 this.auth.token(), filters, orderBy, limit, offset
231 if (first) { // First response is the hold count.
232 this.holdsCount = Number(holdData);
235 } else { // Subsequent responses are hold data blobs
237 this.progressDialog.update(
238 {value: ++loadCount, max: this.holdsCount});
240 observer.next(holdData);
244 this.progressDialog.close();
248 this.progressDialog.close();
256 showDetails(rows: any[]) {
257 this.showDetail(rows[0]);
260 showDetail(row: any) {
262 this.mode = 'detail';
263 this.detailHold = row;
267 showManager(rows: any[]) {
269 this.mode = 'manage';
270 this.editHolds = rows.map(r => r.id);
274 handleModify(rowsModified: boolean) {
278 // give the grid a chance to render then ask it to reload
279 setTimeout(() => this.holdsGrid.reload());
285 showRecentCircs(rows: any[]) {
288 '/eg/staff/cat/item/' + rows[0].cp_id + '/circ_list';
289 window.open(url, '_blank');
293 showPatron(rows: any[]) {
296 '/eg/staff/circ/patron/' + rows[0].usr_id + '/checkout';
297 window.open(url, '_blank');
301 showManageDialog(rows: any[]) {
302 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
303 if (holdIds.length > 0) {
304 this.manageDialog.holdIds = holdIds;
305 this.manageDialog.open({size: 'lg'}).subscribe(
308 this.holdsGrid.reload();
315 showTransferDialog(rows: any[]) {
316 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
317 if (holdIds.length > 0) {
318 this.transferDialog.holdIds = holdIds;
319 this.transferDialog.open({}).subscribe(
322 this.holdsGrid.reload();
329 async showMarkDamagedDialog(rows: any[]) {
330 const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
331 if (copyIds.length === 0) { return; }
333 let rowsModified = false;
335 const markNext = async(ids: number[]) => {
336 if (ids.length === 0) {
337 return Promise.resolve();
340 this.markDamagedDialog.copyId = ids.pop();
341 return this.markDamagedDialog.open({size: 'lg'}).subscribe(
343 if (ok) { rowsModified = true; }
344 return markNext(ids);
346 dismiss => markNext(ids)
350 await markNext(copyIds);
352 this.holdsGrid.reload();
356 showMarkMissingDialog(rows: any[]) {
357 const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
358 if (copyIds.length > 0) {
359 this.markMissingDialog.copyIds = copyIds;
360 this.markMissingDialog.open({}).subscribe(
363 this.holdsGrid.reload();
370 showRetargetDialog(rows: any[]) {
371 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
372 if (holdIds.length > 0) {
373 this.retargetDialog.holdIds = holdIds;
374 this.retargetDialog.open({}).subscribe(
377 this.holdsGrid.reload();
384 showCancelDialog(rows: any[]) {
385 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
386 if (holdIds.length > 0) {
387 this.cancelDialog.holdIds = holdIds;
388 this.cancelDialog.open({}).subscribe(
391 this.holdsGrid.reload();
399 // Request a page with no limit to get all of the wide holds for
400 // printing. Call requestPage() directly instead of grid.reload()
401 // since we may already have the data.
403 const pager = new Pager();
407 if (this.gridDataSource.sort.length === 0) {
408 this.gridDataSource.sort = this.defaultSort;
411 this.gridDataSource.requestPage(pager).then(() => {
412 if (this.gridDataSource.data.length > 0) {
414 templateName: this.printTemplate || 'holds_for_bib',
415 contextData: this.gridDataSource.data,
416 printContext: 'default'