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