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 {GridDataSource} from '@eg/share/grid/grid';
9 import {GridComponent} from '@eg/share/grid/grid.component';
10 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
11 import {MarkDamagedDialogComponent
12 } from '@eg/staff/share/holdings/mark-damaged-dialog.component';
13 import {MarkMissingDialogComponent
14 } from '@eg/staff/share/holdings/mark-missing-dialog.component';
15 import {HoldRetargetDialogComponent
16 } from '@eg/staff/share/holds/retarget-dialog.component';
17 import {HoldTransferDialogComponent} from './transfer-dialog.component';
18 import {HoldCancelDialogComponent} from './cancel-dialog.component';
19 import {HoldManageDialogComponent} from './manage-dialog.component';
21 /** Holds grid with access to detail page and other actions */
24 selector: 'eg-holds-grid',
25 templateUrl: 'grid.component.html'
27 export class HoldsGridComponent implements OnInit {
29 // If either are set/true, the pickup lib selector will display
30 @Input() initialPickupLib: number | IdlObject;
31 @Input() hidePickupLibFilter: boolean;
34 @Input() persistKey: string;
36 // How to sort when no sort parameters have been applied
37 // via grid controls. This uses the eg-grid sort format:
38 // [{name: fname, dir: 'asc'}, {name: fname2, dir: 'desc'}]
39 @Input() defaultSort: any[];
41 mode: 'list' | 'detail' | 'manage' = 'list';
45 gridDataSource: GridDataSource;
48 transferTarget: number;
49 copyStatuses: {[id: string]: IdlObject};
51 @ViewChild('holdsGrid') private holdsGrid: GridComponent;
52 @ViewChild('progressDialog')
53 private progressDialog: ProgressDialogComponent;
54 @ViewChild('transferDialog')
55 private transferDialog: HoldTransferDialogComponent;
56 @ViewChild('markDamagedDialog')
57 private markDamagedDialog: MarkDamagedDialogComponent;
58 @ViewChild('markMissingDialog')
59 private markMissingDialog: MarkMissingDialogComponent;
60 @ViewChild('retargetDialog')
61 private retargetDialog: HoldRetargetDialogComponent;
62 @ViewChild('cancelDialog')
63 private cancelDialog: HoldCancelDialogComponent;
64 @ViewChild('manageDialog')
65 private manageDialog: HoldManageDialogComponent;
69 @Input() set recordId(id: number) {
71 if (this.initDone) { // reload on update
72 this.holdsGrid.reload();
77 @Input() set userId(id: number) {
80 this.holdsGrid.reload();
84 // Include holds canceled on or after the provided date.
85 // If no value is passed, canceled holds are not displayed.
86 _showCanceledSince: Date;
87 @Input() set showCanceledSince(show: Date) {
88 this._showCanceledSince = show;
89 if (this.initDone) { // reload on update
90 this.holdsGrid.reload();
94 // Include holds fulfilled on or after hte provided date.
95 // If no value is passed, fulfilled holds are not displayed.
96 _showFulfilledSince: Date;
97 @Input() set showFulfilledSince(show: Date) {
98 this._showFulfilledSince = show;
99 if (this.initDone) { // reload on update
100 this.holdsGrid.reload();
105 private net: NetService,
106 private org: OrgService,
107 private auth: AuthService
109 this.gridDataSource = new GridDataSource();
110 this.copyStatuses = {};
114 this.initDone = true;
115 this.pickupLib = this.org.get(this.initialPickupLib);
117 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
119 if (this.defaultSort && sort.length === 0) {
120 // Only use initial sort if sorting has not been modified
121 // by the grid's own sort controls.
122 sort = this.defaultSort;
125 // sorting not currently supported
126 return this.fetchHolds(pager, sort);
130 pickupLibChanged(org: IdlObject) {
131 this.pickupLib = org;
132 this.holdsGrid.reload();
135 applyFilters(): any {
136 const filters: any = {
137 is_staff_request: true,
138 fulfillment_time: this._showFulfilledSince ?
139 this._showFulfilledSince.toISOString() : null,
140 cancel_time: this._showCanceledSince ?
141 this._showCanceledSince.toISOString() : null,
144 if (this.pickupLib) {
146 this.org.descendants(this.pickupLib, true);
149 if (this._recordId) {
150 filters.record_id = this._recordId;
154 filters.usr_id = this._userId;
160 fetchHolds(pager: Pager, sort: any[]): Observable<any> {
162 // We need at least one filter.
163 if (!this._recordId && !this.pickupLib && !this._userId) {
167 const filters = this.applyFilters();
169 const orderBy: any = [];
170 sort.forEach(obj => {
171 const subObj: any = {};
172 subObj[obj.name] = {dir: obj.dir, nulls: 'last'};
173 orderBy.push(subObj);
176 let observer: Observer<any>;
177 const observable = new Observable(obs => observer = obs);
179 this.progressDialog.open();
180 this.progressDialog.update({value: 0, max: 1});
185 'open-ils.circ.hold.wide_hash.stream',
186 // Pre-fetch all holds consistent with AngJS version
187 this.auth.token(), filters, orderBy
188 // Alternatively, fetch holds in pages.
189 // this.auth.token(), filters, orderBy, pager.limit, pager.offset
193 if (first) { // First response is the hold count.
194 this.holdsCount = Number(holdData);
197 } else { // Subsequent responses are hold data blobs
199 this.progressDialog.update(
200 {value: ++loadCount, max: this.holdsCount});
202 observer.next(holdData);
206 this.progressDialog.close();
210 this.progressDialog.close();
218 showDetails(rows: any[]) {
219 this.showDetail(rows[0]);
222 showDetail(row: any) {
224 this.mode = 'detail';
225 this.detailHold = row;
229 showManager(rows: any[]) {
231 this.mode = 'manage';
232 this.editHolds = rows.map(r => r.id);
236 handleModify(rowsModified: boolean) {
240 // give the grid a chance to render then ask it to reload
241 setTimeout(() => this.holdsGrid.reload());
247 showRecentCircs(rows: any[]) {
250 '/eg/staff/cat/item/' + rows[0].cp_id + '/circ_list';
251 window.open(url, '_blank');
255 showPatron(rows: any[]) {
258 '/eg/staff/circ/patron/' + rows[0].usr_id + '/checkout';
259 window.open(url, '_blank');
263 showManageDialog(rows: any[]) {
264 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
265 if (holdIds.length > 0) {
266 this.manageDialog.holdIds = holdIds;
267 this.manageDialog.open({size: 'lg'}).then(
270 this.holdsGrid.reload();
278 showTransferDialog(rows: any[]) {
279 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
280 if (holdIds.length > 0) {
281 this.transferDialog.holdIds = holdIds;
282 this.transferDialog.open({}).then(
285 this.holdsGrid.reload();
293 async showMarkDamagedDialog(rows: any[]) {
294 const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
295 if (copyIds.length === 0) { return; }
297 let rowsModified = false;
299 const markNext = async(ids: number[]) => {
300 if (ids.length === 0) {
301 return Promise.resolve();
304 this.markDamagedDialog.copyId = ids.pop();
305 this.markDamagedDialog.open({size: 'lg'}).then(
307 if (ok) { rowsModified = true; }
308 return markNext(ids);
310 dismiss => markNext(ids)
314 await markNext(copyIds);
316 this.holdsGrid.reload();
320 showMarkMissingDialog(rows: any[]) {
321 const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
322 if (copyIds.length > 0) {
323 this.markMissingDialog.copyIds = copyIds;
324 this.markMissingDialog.open({}).then(
327 this.holdsGrid.reload();
330 dismissed => {} // avoid console errors
335 showRetargetDialog(rows: any[]) {
336 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
337 if (holdIds.length > 0) {
338 this.retargetDialog.holdIds = holdIds;
339 this.retargetDialog.open({}).then(
342 this.holdsGrid.reload();
350 showCancelDialog(rows: any[]) {
351 const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
352 if (holdIds.length > 0) {
353 this.cancelDialog.holdIds = holdIds;
354 this.cancelDialog.open({}).then(
357 this.holdsGrid.reload();