1 import {Component, OnInit, Input, Output, ViewChild, EventEmitter} from '@angular/core';
2 import {IdlObject, IdlService} from '@eg/core/idl.service';
3 import {NetService} from '@eg/core/net.service';
4 import {OrgService} from '@eg/core/org.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
8 import {HoldsService} from './holds.service';
10 /** Edit holds in single or batch mode. */
13 selector: 'eg-hold-manage',
14 templateUrl: 'manage.component.html'
16 export class HoldManageComponent implements OnInit {
18 // One holds ID means standard edit mode.
19 // >1 hold IDs means batch edit mode.
20 @Input() holdIds: number[];
24 smsCarriers: ComboboxEntry[];
25 activeFields: {[key: string]: boolean};
27 // Emits true if changes were applied to the hold.
28 @Output() onComplete: EventEmitter<boolean>;
31 private idl: IdlService,
32 private org: OrgService,
33 private pcrud: PcrudService,
34 private holds: HoldsService
36 this.onComplete = new EventEmitter<boolean>();
37 this.smsCarriers = [];
39 this.activeFields = {};
43 this.org.settings('sms.enable').then(sets => {
44 this.smsEnabled = sets['sms.enable'];
45 if (!this.smsEnabled) { return; }
47 this.pcrud.search('csc', {active: 't'}, {order_by: {csc: 'name'}})
48 .subscribe(carrier => {
49 this.smsCarriers.push({
62 if (this.holdIds.length === 0) {
65 } else if (this.isBatch()) {
66 // Use a dummy hold to store form values.
67 this.hold = this.idl.create('ahr');
70 // Form values are stored in the one hold we're editing.
71 this.pcrud.retrieve('ahr', this.holdIds[0])
72 .subscribe(hold => this.hold = hold);
81 return this.holdIds.length > 1;
84 pickupLibChanged(org: IdlObject) {
86 this.hold.pickup_lib(org.id());
93 // Fields with edit-active checkboxes
94 const fields = Object.keys(this.activeFields)
95 .filter(field => this.activeFields[field]);
97 const holds: IdlObject[] = [];
98 this.pcrud.search('ahr', {id: this.holdIds})
101 // Copy form fields to each hold to update.
102 fields.forEach(field => hold[field](this.hold[field]()));
107 this.saveBatch(holds);
111 this.saveBatch([this.hold]);
115 saveBatch(holds: IdlObject[]) {
116 let successCount = 0;
117 this.holds.updateHolds(holds)
120 if (Number(res) > 0) {
122 console.debug('hold update succeeded with ', res);
127 err => console.error('hold update failed with ', err),
129 if (successCount === holds.length) {
130 this.onComplete.emit(true);
133 console.error('Some holds failed to update');
140 this.onComplete.emit(false);