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