]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holds/transfer-dialog.component.ts
LP1823041 Observable dialogs repairs and cleanup
[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
58             return throwError('Transfer Target Required');
59         }
60
61         return super.open(args);
62     }
63
64     async transferHolds(): Promise<any> {
65         return this.net.request(
66             'open-ils.circ',
67             'open-ils.circ.hold.change_title.specific_holds',
68             this.auth.token(), this.transferTarget, this.holdIds
69         ).toPromise().then(async(result) => {
70             if (Number(result) === 1) {
71                 this.numSucceeded++;
72                 this.toast.success(await this.successMsg.current());
73             } else {
74                 this.numFailed++;
75                 console.error('Retarget Failed', this.evt.parse(result));
76                 this.toast.warning(await this.errorMsg.current());
77             }
78         });
79     }
80
81     async transferBatch(): Promise<any> {
82         this.numSucceeded = 0;
83         this.numFailed = 0;
84         await this.transferHolds();
85         this.close(this.numSucceeded > 0);
86     }
87 }
88
89
90