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';
22 /** Holds grid with access to detail page and other actions */
25 selector: 'eg-holds-grid',
26 templateUrl: 'grid.component.html'
28 export class HoldsGridComponent implements OnInit {
30 // If either are set/true, the pickup lib selector will display
31 @Input() initialPickupLib: number | IdlObject;
32 @Input() hidePickupLibFilter: boolean;
35 @Input() persistKey: string;
37 @Input() preFetchSetting: string;
38 // If set, all holds are fetched on grid load and sorting/paging all
39 // happens in the client. If false, sorting and paging occur on
41 enablePreFetch: boolean;
43 // How to sort when no sort parameters have been applied
44 // via grid controls. This uses the eg-grid sort format:
45 // [{name: fname, dir: 'asc'}, {name: fname2, dir: 'desc'}]
46 @Input() defaultSort: any[];
48 mode: 'list' | 'detail' | 'manage' = 'list';
52 gridDataSource: GridDataSource;
55 transferTarget: number;
57 @ViewChild('holdsGrid') private holdsGrid: GridComponent;
58 @ViewChild('progressDialog')
59 private progressDialog: ProgressDialogComponent;
60 @ViewChild('transferDialog')
61 private transferDialog: HoldTransferDialogComponent;
62 @ViewChild('markDamagedDialog')
63 private markDamagedDialog: MarkDamagedDialogComponent;
64 @ViewChild('markMissingDialog')
65 private markMissingDialog: MarkMissingDialogComponent;
66 @ViewChild('retargetDialog')
67 private retargetDialog: HoldRetargetDialogComponent;
68 @ViewChild('cancelDialog')
69 private cancelDialog: HoldCancelDialogComponent;
70 @ViewChild('manageDialog')
71 private manageDialog: HoldManageDialogComponent;
75 @Input() set recordId(id: number) {
77 if (this.initDone) { // reload on update
78 this.holdsGrid.reload();
83 @Input() set userId(id: number) {
86 this.holdsGrid.reload();
90 // Include holds canceled on or after the provided date.
91 // If no value is passed, canceled holds are not displayed.
92 _showCanceledSince: Date;
93 @Input() set showCanceledSince(show: Date) {
94 this._showCanceledSince = show;
95 if (this.initDone) { // reload on update
96 this.holdsGrid.reload();
100 // Include holds fulfilled on or after hte provided date.
101 // If no value is passed, fulfilled holds are not displayed.
102 _showFulfilledSince: Date;
103 @Input() set showFulfilledSince(show: Date) {
104 this._showFulfilledSince = show;
105 if (this.initDone) { // reload on update
106 this.holdsGrid.reload();
111 private net: NetService,
112 private org: OrgService,
113 private store: ServerStoreService,
114 private auth: AuthService
116 this.gridDataSource = new GridDataSource();
117 this.enablePreFetch = null;
121 this.initDone = true;
122 this.pickupLib = this.org.get(this.initialPickupLib);
124 if (this.preFetchSetting) {
126 this.store.getItem(this.preFetchSetting).then(
127 applied => this.enablePreFetch = Boolean(applied)
132 if (!this.defaultSort) {
133 this.defaultSort = [{name: 'request_time', dir: 'asc'}];
136 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
137 sort = sort.length > 0 ? sort : this.defaultSort;
138 return this.fetchHolds(pager, sort);
142 // Returns true after all data/settings/etc required to render the
143 // grid have been fetched.
144 initComplete(): boolean {
145 return this.enablePreFetch !== null;
148 pickupLibChanged(org: IdlObject) {
149 this.pickupLib = org;
150 this.holdsGrid.reload();
153 preFetchHolds(apply: boolean) {
154 this.enablePreFetch = apply;
157 setTimeout(() => this.holdsGrid.reload());
160 if (this.preFetchSetting) {
162 this.store.setItem(this.preFetchSetting, apply);
166 applyFilters(): any {
167 const filters: any = {
168 is_staff_request: true,
169 fulfillment_time: this._showFulfilledSince ?
170 this._showFulfilledSince.toISOString() : null,
171 cancel_time: this._showCanceledSince ?
172 this._showCanceledSince.toISOString() : null,
175 if (this.pickupLib) {
177 this.org.descendants(this.pickupLib, true);
180 if (this._recordId) {
181 filters.record_id = this._recordId;
185 filters.usr_id = this._userId;
191 fetchHolds(pager: Pager, sort: any[]): Observable<any> {
193 // We need at least one filter.
194 if (!this._recordId && !this.pickupLib && !this._userId) {
198 const filters = this.applyFilters();
200 const orderBy: any = [];
201 if (sort.length > 0) {
202 sort.forEach(obj => {
203 const subObj: any = {};
204 subObj[obj.name] = {dir: obj.dir, nulls: 'last'};
205 orderBy.push(subObj);
209 const limit = this.enablePreFetch ? null : pager.limit;
210 const offset = this.enablePreFetch ? 0 : pager.offset;
212 let observer: Observer<any>;
213 const observable = new Observable(obs => observer = obs);
215 this.progressDialog.open();
216 this.progressDialog.update({value: 0, max: 1});
221 'open-ils.circ.hold.wide_hash.stream',
222 this.auth.token(), filters, orderBy, limit, offset
226 if (first) { // First response is the hold count.
227 this.holdsCount = Number(holdData);
230 } else { // Subsequent responses are hold data blobs
232 this.progressDialog.update(
233 {value: ++loadCount, max: this.holdsCount});
235 observer.next(holdData);
239 this.progressDialog.close();
243 this.progressDialog.close();
251 showDetails(rows: any[]) {
252 this.showDetail(rows[0]);
255 showDetail(row: any) {
257 this.mode = 'detail';
258 this.detailHold = row;
262 showManager(rows: any[]) {
264 this.mode = 'manage';
265 this.editHolds = rows.map(r => r.id);
269 handleModify(rowsModified: boolean) {
273 // give the grid a chance to render then ask it to reload
274 setTimeout(() => this.holdsGrid.reload());
280 showRecentCircs(rows: any[]) {
283 '/eg/staff/cat/item/' + rows[0].cp_id + '/circ_list';
284 window.open(url, '_blank');
288 showPatron(rows: any[]) {
291 '/eg/staff/circ/patron/' + rows[0].usr_id + '/checkout';
292 window.open(url, '_blank');
296 showManageDialog(rows: any[]) {
297 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
298 if (holdIds.length > 0) {
299 this.manageDialog.holdIds = holdIds;
300 this.manageDialog.open({size: 'lg'}).then(
303 this.holdsGrid.reload();
311 showTransferDialog(rows: any[]) {
312 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
313 if (holdIds.length > 0) {
314 this.transferDialog.holdIds = holdIds;
315 this.transferDialog.open({}).then(
318 this.holdsGrid.reload();
326 async showMarkDamagedDialog(rows: any[]) {
327 const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
328 if (copyIds.length === 0) { return; }
330 let rowsModified = false;
332 const markNext = async(ids: number[]) => {
333 if (ids.length === 0) {
334 return Promise.resolve();
337 this.markDamagedDialog.copyId = ids.pop();
338 this.markDamagedDialog.open({size: 'lg'}).then(
340 if (ok) { rowsModified = true; }
341 return markNext(ids);
343 dismiss => markNext(ids)
347 await markNext(copyIds);
349 this.holdsGrid.reload();
353 showMarkMissingDialog(rows: any[]) {
354 const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
355 if (copyIds.length > 0) {
356 this.markMissingDialog.copyIds = copyIds;
357 this.markMissingDialog.open({}).then(
360 this.holdsGrid.reload();
363 dismissed => {} // avoid console errors
368 showRetargetDialog(rows: any[]) {
369 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
370 if (holdIds.length > 0) {
371 this.retargetDialog.holdIds = holdIds;
372 this.retargetDialog.open({}).then(
375 this.holdsGrid.reload();
383 showCancelDialog(rows: any[]) {
384 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
385 if (holdIds.length > 0) {
386 this.cancelDialog.holdIds = holdIds;
387 this.cancelDialog.open({}).then(
390 this.holdsGrid.reload();