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';
12 * Dialog for adding bib records to new and existing record buckets.
16 selector: 'eg-bucket-dialog',
17 templateUrl: 'bucket-dialog.component.html'
20 export class BucketDialogComponent extends DialogComponent implements OnInit {
22 selectedBucket: number;
23 newBucketName: string;
24 newBucketDesc: string;
27 @Input() bucketClass: 'biblio' | 'user' | 'callnumber' | 'copy';
28 @Input() bucketType: string; // e.g. staff_client
30 // ID's of items to add to the bucket
31 @Input() itemIds: number[];
33 // If set, itemIds will be derived from the records in a bib queue
34 @Input() fromBibQueue: number;
36 // bucket item classes are these plus a following 'i'.
37 bucketFmClass: 'ccb' | 'ccnb' | 'cbreb' | 'cub';
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
51 this.fromBibQueue = null;
55 this.onOpen$.subscribe(ok => {
56 this.reset(); // Reset data on dialog open
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);
67 this.selectedBucket = null;
68 this.newBucketName = '';
69 this.newBucketDesc = '';
71 if (!this.bucketClass) {
72 this.bucketClass = 'biblio';
75 switch (this.bucketClass) {
77 if (this.fromBibQueue) {
78 this.bucketType = 'vandelay_queue';
80 this.bucketFmClass = 'cbreb';
81 this.targetField = 'target_biblio_record_entry';
84 this.bucketFmClass = 'ccb';
85 this.targetField = 'target_copy';
88 this.bucketFmClass = 'ccnb';
89 this.targetField = 'target_call_number';
92 this.bucketFmClass = 'cub';
93 this.targetField = 'target_user';
96 if (!this.bucketType) {
97 this.bucketType = 'staff_client';
102 this.addToBucket(this.selectedBucket);
105 bucketChanged(entry: ComboboxEntry) {
107 this.selectedBucket = entry.id;
109 this.selectedBucket = null;
113 formatBucketEntries(): ComboboxEntry[] {
114 return this.buckets.map(b => ({id: b.id(), label: b.name()}));
117 // Create a new bucket then add the record
119 const bucket = this.idl.create(this.bucketFmClass);
121 bucket.owner(this.auth.user().id());
122 bucket.name(this.newBucketName);
123 bucket.description(this.newBucketDesc);
124 bucket.btype(this.bucketType);
128 'open-ils.actor.container.create',
129 this.auth.token(), this.bucketClass, bucket
130 ).subscribe(bktId => {
131 const evt = this.evt.parse(bktId);
133 this.toast.danger(evt.desc);
135 // make it find-able to the queue-add method which
136 // requires the bucket name.
138 this.buckets.push(bucket);
139 this.addToBucket(bktId);
144 addToBucket(id: number) {
145 if (this.itemIds.length > 0) {
146 this.addRecordToBucket(id);
147 } else if (this.fromBibQueue) {
148 this.addBibQueueToBucket(id);
152 // Add the record(s) to the bucket with provided ID.
153 addRecordToBucket(bucketId: number) {
155 this.itemIds.forEach(itemId => {
156 const item = this.idl.create(this.bucketFmClass + 'i');
157 item.bucket(bucketId);
158 item[this.targetField](itemId);
164 'open-ils.actor.container.item.create',
165 this.auth.token(), this.bucketClass, items
166 ).subscribe(resp => {
167 const evt = this.evt.parse(resp);
169 this.toast.danger(evt.toString());
176 addBibQueueToBucket(bucketId: number) {
177 const bucket = this.buckets.filter(b => b.id() === bucketId)[0];
178 if (!bucket) { return; }
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);
187 this.toast.danger(evt.toString());