]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holds/cancel-dialog.component.ts
LP1851882 Angular catalog recall/force/part holds
[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', { static: true }) private successMsg: StringComponent;
27     @ViewChild('errorMsg', { static: true }) 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         this.numSucceeded = 0;
55         this.numFailed = 0;
56
57         if (this.cancelReasons.length === 0) {
58             this.pcrud.retrieveAll('ahrcc', {}, {atomic: true}).toPromise()
59             .then(reasons => {
60                 this.cancelReasons =
61                     reasons.map(r => ({id: r.id(), label: r.label()}));
62             });
63         }
64
65         return super.open(args);
66     }
67
68     async cancelNext(ids: number[]): Promise<any> {
69         if (ids.length === 0) {
70             return Promise.resolve();
71         }
72
73         return this.net.request(
74             'open-ils.circ', 'open-ils.circ.hold.cancel',
75             this.auth.token(), ids.pop(),
76             this.cancelReason, this.cancelNote
77         ).toPromise().then(
78             async(result) => {
79                 if (Number(result) === 1) {
80                     this.numSucceeded++;
81                     this.toast.success(await this.successMsg.current());
82                 } else {
83                     this.numFailed++;
84                     console.error(this.evt.parse(result));
85                     this.toast.warning(await this.errorMsg.current());
86                 }
87                 this.cancelNext(ids);
88             }
89         );
90     }
91
92     async cancelBatch(): Promise<any> {
93         this.numSucceeded = 0;
94         this.numFailed = 0;
95         const ids = [].concat(this.holdIds);
96         await this.cancelNext(ids);
97         this.close(this.numSucceeded > 0);
98     }
99 }
100
101
102