1 import {Component, OnInit, 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 implements OnInit {
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 = [];
48 // Avoid fetching cancel reasons in ngOnInit becaues that causes
49 // them to load regardless of whether the dialog is ever used.
52 open(args: NgbModalOptions): Observable<boolean> {
54 if (this.cancelReasons.length === 0) {
55 this.pcrud.retrieveAll('ahrcc', {}, {atomic: true}).toPromise()
58 reasons.map(r => ({id: r.id(), label: r.label()}));
62 return super.open(args);
65 async cancelNext(ids: number[]): Promise<any> {
66 if (ids.length === 0) {
67 return Promise.resolve();
70 return this.net.request(
71 'open-ils.circ', 'open-ils.circ.hold.cancel',
72 this.auth.token(), ids.pop(),
73 this.cancelReason, this.cancelNote
76 if (Number(result) === 1) {
78 this.toast.success(await this.successMsg.current());
81 console.error(this.evt.parse(result));
82 this.toast.warning(await this.errorMsg.current());
89 async cancelBatch(): Promise<any> {
90 this.numSucceeded = 0;
92 const ids = [].concat(this.holdIds);
93 await this.cancelNext(ids);
94 this.close(this.numSucceeded > 0);