]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holds/retarget-dialog.component.ts
LP1851882 Angular catalog recall/force/part holds
[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.numSucceeded = 0;
45         this.numFailed = 0;
46         this.holdIds = [].concat(this.holdIds); // array-ify ints
47         return super.open(args);
48     }
49
50     async retargetNext(ids: number[]): Promise<any> {
51         if (ids.length === 0) {
52             return Promise.resolve();
53         }
54
55         return this.net.request(
56             'open-ils.circ', 'open-ils.circ.hold.reset',
57             this.auth.token(), ids.pop()
58         ).toPromise().then(
59             async(result) => {
60                 if (Number(result) === 1) {
61                     this.numSucceeded++;
62                     this.toast.success(await this.successMsg.current());
63                 } else {
64                     this.numFailed++;
65                     console.error(this.evt.parse(result));
66                     this.toast.warning(await this.errorMsg.current());
67                 }
68                 this.retargetNext(ids);
69             }
70         );
71     }
72
73     async retargetBatch(): Promise<any> {
74         this.numSucceeded = 0;
75         this.numFailed = 0;
76         const ids = [].concat(this.holdIds);
77         await this.retargetNext(ids);
78         this.close(this.numSucceeded > 0);
79     }
80 }
81
82
83