]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holds/transfer-dialog.component.ts
LP1823041: Converting new dialogs to observables
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holds / transfer-dialog.component.ts
1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {Observable, throwError} from 'rxjs';
3 import {NetService} from '@eg/core/net.service';
4 import {StoreService} from '@eg/core/store.service';
5 import {EventService} from '@eg/core/event.service';
6 import {ToastService} from '@eg/share/toast/toast.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
12
13 /**
14  * Dialog for transferring holds.
15  */
16
17 @Component({
18   selector: 'eg-hold-transfer-dialog',
19   templateUrl: 'transfer-dialog.component.html'
20 })
21
22 export class HoldTransferDialogComponent
23     extends DialogComponent implements OnInit {
24
25     @Input() holdIds: number | number[];
26
27     @ViewChild('successMsg') private successMsg: StringComponent;
28     @ViewChild('errorMsg') private errorMsg: StringComponent;
29     @ViewChild('targetNeeded') private targetNeeded: StringComponent;
30
31     transferTarget: number;
32     changesApplied: boolean;
33     numSucceeded: number;
34     numFailed: number;
35
36     constructor(
37         private modal: NgbModal, // required for passing to parent
38         private toast: ToastService,
39         private store: StoreService,
40         private net: NetService,
41         private evt: EventService,
42         private auth: AuthService) {
43         super(modal); // required for subclassing
44     }
45
46     ngOnInit() {}
47
48     open(args: NgbModalOptions): Observable<boolean> {
49         this.holdIds = [].concat(this.holdIds); // array-ify ints
50
51         this.transferTarget =
52             this.store.getLocalItem('eg.circ.hold.title_transfer_target');
53
54         if (!this.transferTarget) {
55             this.targetNeeded.current()
56             .then((msg) => this.toast.warning(msg))
57             .then(() => throwError('Transfer Target Required'));
58         }
59
60         return super.open(args);
61     }
62
63     async transferHolds(): Promise<any> {
64         return this.net.request(
65             'open-ils.circ',
66             'open-ils.circ.hold.change_title.specific_holds',
67             this.auth.token(), this.transferTarget, this.holdIds
68         ).toPromise().then(async(result) => {
69             if (Number(result) === 1) {
70                 this.numSucceeded++;
71                 this.toast.success(await this.successMsg.current());
72             } else {
73                 this.numFailed++;
74                 console.error('Retarget Failed', this.evt.parse(result));
75                 this.toast.warning(await this.errorMsg.current());
76             }
77         });
78     }
79
80     async transferBatch(): Promise<any> {
81         this.numSucceeded = 0;
82         this.numFailed = 0;
83         await this.transferHolds();
84         this.close(this.numSucceeded > 0);
85     }
86 }
87
88
89