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