]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/buckets/bucket-dialog.component.ts
LP1821382 Add Items to Bucket menu action
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / buckets / bucket-dialog.component.ts
1 import {Component, OnInit, Input, Renderer2} from '@angular/core';
2 import {NetService} from '@eg/core/net.service';
3 import {IdlService} from '@eg/core/idl.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} from '@ng-bootstrap/ng-bootstrap';
9 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
10
11 /**
12  * Dialog for adding bib records to new and existing record buckets.
13  */
14
15 @Component({
16   selector: 'eg-bucket-dialog',
17   templateUrl: 'bucket-dialog.component.html'
18 })
19
20 export class BucketDialogComponent extends DialogComponent implements OnInit {
21
22     selectedBucket: number;
23     newBucketName: string;
24     newBucketDesc: string;
25     buckets: any[];
26
27     @Input() bucketClass: 'biblio' | 'user' | 'callnumber' | 'copy';
28     @Input() bucketType: string; // e.g. staff_client
29
30     // ID's of items to add to the bucket
31     @Input() itemIds: number[];
32
33     // If set, itemIds will be derived from the records in a bib queue
34     @Input() fromBibQueue: number;
35
36     // bucket item classes are these plus a following 'i'.
37     bucketFmClass: 'ccb' | 'ccnb' | 'cbreb' | 'cub';
38     targetField: string;
39
40     constructor(
41         private modal: NgbModal, // required for passing to parent
42         private renderer: Renderer2,
43         private toast: ToastService,
44         private idl: IdlService,
45         private net: NetService,
46         private evt: EventService,
47         private auth: AuthService) {
48         super(modal); // required for subclassing
49         this.buckets = [];
50         this.itemIds = [];
51         this.fromBibQueue = null;
52     }
53
54     ngOnInit() {
55         this.onOpen$.subscribe(ok => {
56             this.reset(); // Reset data on dialog open
57             this.net.request(
58                 'open-ils.actor',
59                 'open-ils.actor.container.retrieve_by_class.authoritative',
60                 this.auth.token(), this.auth.user().id(),
61                 this.bucketClass, this.bucketType
62             ).subscribe(buckets => this.buckets = buckets);
63         });
64     }
65
66     reset() {
67         this.selectedBucket = null;
68         this.newBucketName = '';
69         this.newBucketDesc = '';
70
71         if (!this.bucketClass) {
72             this.bucketClass = 'biblio';
73         }
74
75         switch (this.bucketClass) {
76             case 'biblio':
77                 if (this.fromBibQueue) {
78                     this.bucketType = 'vandelay_queue';
79                 }
80                 this.bucketFmClass = 'cbreb';
81                 this.targetField = 'target_biblio_record_entry';
82                 break;
83             case 'copy':
84                 this.bucketFmClass = 'ccb';
85                 this.targetField = 'target_copy';
86                 break;
87             case 'callnumber':
88                 this.bucketFmClass = 'ccnb';
89                 this.targetField = 'target_call_number';
90                 break;
91             case 'user':
92                 this.bucketFmClass = 'cub';
93                 this.targetField = 'target_user';
94         }
95
96         if (!this.bucketType) {
97             this.bucketType = 'staff_client';
98         }
99     }
100
101     addToSelected() {
102         this.addToBucket(this.selectedBucket);
103     }
104
105     bucketChanged(entry: ComboboxEntry) {
106         if (entry) {
107             this.selectedBucket = entry.id;
108         } else {
109             this.selectedBucket = null;
110         }
111     }
112
113     formatBucketEntries(): ComboboxEntry[] {
114         return this.buckets.map(b => ({id: b.id(), label: b.name()}));
115     }
116
117     // Create a new bucket then add the record
118     addToNew() {
119         const bucket = this.idl.create(this.bucketFmClass);
120
121         bucket.owner(this.auth.user().id());
122         bucket.name(this.newBucketName);
123         bucket.description(this.newBucketDesc);
124         bucket.btype(this.bucketType);
125
126         this.net.request(
127             'open-ils.actor',
128             'open-ils.actor.container.create',
129             this.auth.token(), this.bucketClass, bucket
130         ).subscribe(bktId => {
131             const evt = this.evt.parse(bktId);
132             if (evt) {
133                 this.toast.danger(evt.desc);
134             } else {
135                 // make it find-able to the queue-add method which
136                 // requires the bucket name.
137                 bucket.id(bktId);
138                 this.buckets.push(bucket);
139                 this.addToBucket(bktId);
140             }
141         });
142     }
143
144     addToBucket(id: number) {
145         if (this.itemIds.length > 0) {
146             this.addRecordToBucket(id);
147         } else if (this.fromBibQueue) {
148             this.addBibQueueToBucket(id);
149         }
150     }
151
152     // Add the record(s) to the bucket with provided ID.
153     addRecordToBucket(bucketId: number) {
154         const items = [];
155         this.itemIds.forEach(itemId => {
156             const item = this.idl.create(this.bucketFmClass + 'i');
157             item.bucket(bucketId);
158             item[this.targetField](itemId);
159             items.push(item);
160         });
161
162         this.net.request(
163             'open-ils.actor',
164             'open-ils.actor.container.item.create',
165             this.auth.token(), this.bucketClass, items
166         ).subscribe(resp => {
167             const evt = this.evt.parse(resp);
168             if (evt) {
169                 this.toast.danger(evt.toString());
170             } else {
171                 this.close();
172             }
173         });
174     }
175
176     addBibQueueToBucket(bucketId: number) {
177         const bucket = this.buckets.filter(b => b.id() === bucketId)[0];
178         if (!bucket) { return; }
179
180         this.net.request(
181             'open-ils.vandelay',
182             'open-ils.vandelay.bib_queue.to_bucket',
183             this.auth.token(), this.fromBibQueue, bucket.name()
184         ).toPromise().then(resp => {
185             const evt = this.evt.parse(resp);
186             if (evt) {
187                 this.toast.danger(evt.toString());
188             } else {
189                 this.close();
190             }
191         });
192     }
193 }
194
195
196