1 import {Component, Input, ViewChild} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {NetService} from '@eg/core/net.service';
4 import {EventService} from '@eg/core/event.service';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {DialogComponent} from '@eg/share/dialog/dialog.component';
9 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
10 import {StringComponent} from '@eg/share/string/string.component';
11 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
14 * Dialog for canceling hold requests.
18 selector: 'eg-hold-cancel-dialog',
19 templateUrl: 'cancel-dialog.component.html'
22 export class HoldCancelDialogComponent
23 extends DialogComponent {
25 @Input() holdIds: number[];
26 @ViewChild('successMsg', { static: true }) private successMsg: StringComponent;
27 @ViewChild('errorMsg', { static: true }) private errorMsg: StringComponent;
29 changesApplied: boolean;
33 cancelReasons: ComboboxEntry[];
37 private modal: NgbModal, // required for passing to parent
38 private toast: ToastService,
39 private net: NetService,
40 private evt: EventService,
41 private pcrud: PcrudService,
42 private auth: AuthService) {
43 super(modal); // required for subclassing
44 this.cancelReasons = [];
47 // Avoid fetching cancel reasons in ngOnInit becaues that causes
48 // them to load regardless of whether the dialog is ever used.
49 open(args: NgbModalOptions): Observable<boolean> {
50 this.numSucceeded = 0;
53 if (this.cancelReasons.length === 0) {
54 this.pcrud.retrieveAll('ahrcc', {}, {atomic: true}).toPromise()
57 reasons.map(r => ({id: r.id(), label: r.label()}));
61 return super.open(args);
64 async cancelNext(ids: number[]): Promise<any> {
65 if (ids.length === 0) {
66 return Promise.resolve();
69 return this.net.request(
70 'open-ils.circ', 'open-ils.circ.hold.cancel',
71 this.auth.token(), ids.pop(),
72 this.cancelReason, this.cancelNote
75 if (Number(result) === 1) {
77 this.toast.success(await this.successMsg.current());
80 console.error(this.evt.parse(result));
81 this.toast.warning(await this.errorMsg.current());
83 return this.cancelNext(ids);
88 async cancelBatch(): Promise<any> {
89 this.numSucceeded = 0;
91 const ids = [].concat(this.holdIds);
92 await this.cancelNext(ids);
93 this.close(this.numSucceeded > 0);