]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holds/retarget-dialog.component.ts
LP1830973 Angular 8 updates
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holds / retarget-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 {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
11
12 /**
13  * Dialog for retargeting holds.
14  */
15
16 @Component({
17   selector: 'eg-hold-retarget-dialog',
18   templateUrl: 'retarget-dialog.component.html'
19 })
20
21 export class HoldRetargetDialogComponent
22     extends DialogComponent implements OnInit {
23
24     @Input() holdIds: number | number[];
25     @ViewChild('successMsg', { static: true }) private successMsg: StringComponent;
26     @ViewChild('errorMsg', { static: true }) private errorMsg: StringComponent;
27
28     changesApplied: boolean;
29     numSucceeded: number;
30     numFailed: number;
31
32     constructor(
33         private modal: NgbModal, // required for passing to parent
34         private toast: ToastService,
35         private net: NetService,
36         private evt: EventService,
37         private auth: AuthService) {
38         super(modal); // required for subclassing
39     }
40
41     ngOnInit() {}
42
43     open(args: NgbModalOptions): Observable<boolean> {
44         this.holdIds = [].concat(this.holdIds); // array-ify ints
45         return super.open(args);
46     }
47
48     async retargetNext(ids: number[]): Promise<any> {
49         if (ids.length === 0) {
50             return Promise.resolve();
51         }
52
53         return this.net.request(
54             'open-ils.circ', 'open-ils.circ.hold.reset',
55             this.auth.token(), ids.pop()
56         ).toPromise().then(
57             async(result) => {
58                 if (Number(result) === 1) {
59                     this.numSucceeded++;
60                     this.toast.success(await this.successMsg.current());
61                 } else {
62                     this.numFailed++;
63                     console.error(this.evt.parse(result));
64                     this.toast.warning(await this.errorMsg.current());
65                 }
66                 this.retargetNext(ids);
67             }
68         );
69     }
70
71     async retargetBatch(): Promise<any> {
72         this.numSucceeded = 0;
73         this.numFailed = 0;
74         const ids = [].concat(this.holdIds);
75         await this.retargetNext(ids);
76         this.close(this.numSucceeded > 0);
77     }
78 }
79
80
81