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';
14 * Dialog for transferring holds.
18 selector: 'eg-hold-transfer-dialog',
19 templateUrl: 'transfer-dialog.component.html'
22 export class HoldTransferDialogComponent
23 extends DialogComponent implements OnInit {
25 @Input() holdIds: number | number[];
27 @ViewChild('successMsg') private successMsg: StringComponent;
28 @ViewChild('errorMsg') private errorMsg: StringComponent;
29 @ViewChild('targetNeeded') private targetNeeded: StringComponent;
31 transferTarget: number;
32 changesApplied: boolean;
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
48 open(args: NgbModalOptions): Observable<boolean> {
49 this.holdIds = [].concat(this.holdIds); // array-ify ints
52 this.store.getLocalItem('eg.circ.hold.title_transfer_target');
54 if (!this.transferTarget) {
55 this.targetNeeded.current()
56 .then((msg) => this.toast.warning(msg))
57 .then(() => throwError('Transfer Target Required'));
60 return super.open(args);
63 async transferHolds(): Promise<any> {
64 return this.net.request(
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) {
71 this.toast.success(await this.successMsg.current());
74 console.error('Retarget Failed', this.evt.parse(result));
75 this.toast.warning(await this.errorMsg.current());
80 async transferBatch(): Promise<any> {
81 this.numSucceeded = 0;
83 await this.transferHolds();
84 this.close(this.numSucceeded > 0);