]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holds/manage.component.ts
LP1818288 WS Option to pre-fetch record holds
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holds / manage.component.ts
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';
9
10 /** Edit holds in single or batch mode. */
11
12 @Component({
13   selector: 'eg-hold-manage',
14   templateUrl: 'manage.component.html'
15 })
16 export class HoldManageComponent implements OnInit {
17
18     // One holds ID means standard edit mode.
19     // >1 hold IDs means batch edit mode.
20     @Input() holdIds: number[];
21
22     hold: IdlObject;
23     smsEnabled: boolean;
24     smsCarriers: ComboboxEntry[];
25     activeFields: {[key: string]: boolean};
26
27     // Emits true if changes were applied to the hold.
28     @Output() onComplete: EventEmitter<boolean>;
29
30     constructor(
31         private idl: IdlService,
32         private org: OrgService,
33         private pcrud: PcrudService,
34         private holds: HoldsService
35     ) {
36         this.onComplete = new EventEmitter<boolean>();
37         this.smsCarriers = [];
38         this.holdIds = [];
39         this.activeFields = {};
40     }
41
42     ngOnInit() {
43         this.org.settings('sms.enable').then(sets => {
44             this.smsEnabled = sets['sms.enable'];
45             if (!this.smsEnabled) { return; }
46
47             this.pcrud.search('csc', {active: 't'}, {order_by: {csc: 'name'}})
48             .subscribe(carrier => {
49                 this.smsCarriers.push({
50                     id: carrier.id(),
51                     label: carrier.name()
52                 });
53             });
54         });
55
56         this.fetchHold();
57     }
58
59     fetchHold() {
60         this.hold = null;
61
62         if (this.holdIds.length === 0) {
63             return;
64
65         } else if (this.isBatch()) {
66             // Use a dummy hold to store form values.
67             this.hold = this.idl.create('ahr');
68
69         } else {
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);
73         }
74     }
75
76     toFormData() {
77
78     }
79
80     isBatch(): boolean {
81         return this.holdIds.length > 1;
82     }
83
84     pickupLibChanged(org: IdlObject) {
85         if (org) {
86             this.hold.pickup_lib(org.id());
87         }
88     }
89
90     save() {
91         if (this.isBatch()) {
92
93             // Fields with edit-active checkboxes
94             const fields = Object.keys(this.activeFields)
95                 .filter(field => this.activeFields[field]);
96
97             const holds: IdlObject[] = [];
98             this.pcrud.search('ahr', {id: this.holdIds})
99             .subscribe(
100                 hold => {
101                     // Copy form fields to each hold to update.
102                     fields.forEach(field => hold[field](this.hold[field]()));
103                     holds.push(hold);
104                 },
105                 err => {},
106                 ()  => {
107                     this.saveBatch(holds);
108                 }
109             );
110         } else {
111             this.saveBatch([this.hold]);
112         }
113     }
114
115     saveBatch(holds: IdlObject[]) {
116         let successCount = 0;
117         this.holds.updateHolds(holds)
118         .subscribe(
119             res  => {
120                 if (Number(res) > 0) {
121                     successCount++;
122                     console.debug('hold update succeeded with ', res);
123                 } else {
124                     // TODO: toast?
125                 }
126             },
127             err => console.error('hold update failed with ', err),
128             ()  => {
129                 if (successCount === holds.length) {
130                     this.onComplete.emit(true);
131                 } else {
132                     // TODO: toast?
133                     console.error('Some holds failed to update');
134                 }
135             }
136         );
137     }
138
139     exit() {
140         this.onComplete.emit(false);
141     }
142 }
143
144