]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holds/cancel-dialog.component.ts
LP1823041: Converting new dialogs to observables
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holds / cancel-dialog.component.ts
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';
12
13 /**
14  * Dialog for canceling hold requests.
15  */
16
17 @Component({
18   selector: 'eg-hold-cancel-dialog',
19   templateUrl: 'cancel-dialog.component.html'
20 })
21
22 export class HoldCancelDialogComponent
23     extends DialogComponent implements OnInit {
24
25     @Input() holdIds: number[];
26     @ViewChild('successMsg') private successMsg: StringComponent;
27     @ViewChild('errorMsg') private errorMsg: StringComponent;
28
29     changesApplied: boolean;
30     numSucceeded: number;
31     numFailed: number;
32     cancelReason: number;
33     cancelReasons: ComboboxEntry[];
34     cancelNote: string;
35
36     constructor(
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 = [];
45     }
46
47     ngOnInit() {
48         // Avoid fetching cancel reasons in ngOnInit becaues that causes
49         // them to load regardless of whether the dialog is ever used.
50     }
51
52     open(args: NgbModalOptions): Observable<boolean> {
53
54         if (this.cancelReasons.length === 0) {
55             this.pcrud.retrieveAll('ahrcc', {}, {atomic: true}).toPromise()
56             .then(reasons => {
57                 this.cancelReasons =
58                     reasons.map(r => ({id: r.id(), label: r.label()}));
59             });
60         }
61
62         return super.open(args);
63     }
64
65     async cancelNext(ids: number[]): Promise<any> {
66         if (ids.length === 0) {
67             return Promise.resolve();
68         }
69
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
74         ).toPromise().then(
75             async(result) => {
76                 if (Number(result) === 1) {
77                     this.numSucceeded++;
78                     this.toast.success(await this.successMsg.current());
79                 } else {
80                     this.numFailed++;
81                     console.error(this.evt.parse(result));
82                     this.toast.warning(await this.errorMsg.current());
83                 }
84                 this.cancelNext(ids);
85             }
86         );
87     }
88
89     async cancelBatch(): Promise<any> {
90         this.numSucceeded = 0;
91         this.numFailed = 0;
92         const ids = [].concat(this.holdIds);
93         await this.cancelNext(ids);
94         this.close(this.numSucceeded > 0);
95     }
96 }
97
98
99