1 import {Component, OnInit, OnDestroy, Input, ViewChild} from '@angular/core';
2 import {Subscription} from 'rxjs';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {NetService} from '@eg/core/net.service';
5 import {EventService} from '@eg/core/event.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {AuthService} from '@eg/core/auth.service';
9 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
10 import {DialogComponent} from '@eg/share/dialog/dialog.component';
11 import {StringComponent} from '@eg/share/string/string.component';
14 * Dialog for making items bookable
18 selector: 'eg-make-bookable-dialog',
19 templateUrl: 'make-bookable-dialog.component.html'
21 export class MakeBookableDialogComponent
22 extends DialogComponent implements OnInit, OnDestroy {
24 // Note copyIds must refer to copies that belong to a single
26 @Input() copyIds: number[];
31 updateComplete: boolean;
32 newResourceType: number;
33 newResourceOrg: number;
35 onOpenSub: Subscription;
37 @ViewChild('successMsg') private successMsg: StringComponent;
38 @ViewChild('errorMsg') private errorMsg: StringComponent;
41 private modal: NgbModal, // required for passing to parent
42 private toast: ToastService,
43 private net: NetService,
44 private pcrud: PcrudService,
45 private evt: EventService,
46 private auth: AuthService) {
47 super(modal); // required for subclassing
51 this.onOpenSub = this.onOpen$.subscribe(async () => {
52 this.numSucceeded = 0;
54 this.updateComplete = false;
59 this.onOpenSub.unsubscribe();
62 manageUrlParams(): any {
63 if (this.newResourceOrg) {
65 gridFilters: JSON.stringify({type: this.newResourceType}),
66 contextOrg: this.newResourceOrg
72 this.newResourceType = null;
76 'open-ils.booking.resources.create_from_copies',
77 this.auth.token(), this.copyIds
80 // resp.brsrc = [[brsrc.id, acp.id, existed], ...]
81 // resp.brt = [[brt.id, brt.peer_record, existed], ...]
82 const evt = this.evt.parse(resp);
83 if (evt) { return Promise.reject(evt); }
84 this.numSucceeded = resp.brsrc.length;
85 this.newResourceType = resp.brt[0][0]; // new resource ID
86 this.updateComplete = true;
87 this.successMsg.current().then(msg => this.toast.success(msg));
89 err => Promise.reject(err)
92 // Once resource creation is complete, grab the call number
93 // for the first copy to get the owning library
94 this.pcrud.retrieve('acp', this.copyIds[0],
95 {flesh: 1, flesh_fields: {acp: ['call_number']}})
96 .toPromise().then(copy => {
97 this.newResourceOrg = copy.call_number().owning_lib();
98 this.updateComplete = true;
104 this.errorMsg.current().then(msg => this.toast.danger(msg));
105 this.updateComplete = true;