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';
11 * Dialog for adding bib records to new and existing record buckets.
15 selector: 'eg-record-bucket-dialog',
16 templateUrl: 'record-bucket-dialog.component.html'
19 export class RecordBucketDialogComponent
20 extends DialogComponent implements OnInit {
22 selectedBucket: number;
23 newBucketName: string;
24 newBucketDesc: string;
27 @Input() bucketType: string;
29 // Add one or more bib records to bucket by ID.
31 @Input() set recordId(id: number | number[]) {
32 this.recIds = [].concat(id);
35 // Add items from a (vandelay) bib queue to a bucket
37 @Input() set queueId(id: number) {
42 private modal: NgbModal, // required for passing to parent
43 private renderer: Renderer2,
44 private toast: ToastService,
45 private idl: IdlService,
46 private net: NetService,
47 private evt: EventService,
48 private auth: AuthService) {
49 super(modal); // required for subclassing
56 this.bucketType = 'vandelay_queue';
58 this.bucketType = 'staff_client';
61 this.onOpen$.subscribe(ok => {
62 // Reset data on dialog open
64 this.selectedBucket = null;
65 this.newBucketName = '';
66 this.newBucketDesc = '';
70 'open-ils.actor.container.retrieve_by_class.authoritative',
71 this.auth.token(), this.auth.user().id(),
72 'biblio', this.bucketType
73 ).subscribe(buckets => this.buckets = buckets);
78 this.addToBucket(this.selectedBucket);
81 // Create a new bucket then add the record
83 const bucket = this.idl.create('cbreb');
85 bucket.owner(this.auth.user().id());
86 bucket.name(this.newBucketName);
87 bucket.description(this.newBucketDesc);
88 bucket.btype(this.bucketType);
92 'open-ils.actor.container.create',
93 this.auth.token(), 'biblio', bucket
94 ).subscribe(bktId => {
95 const evt = this.evt.parse(bktId);
97 this.toast.danger(evt.desc);
99 // make it find-able to the queue-add method which
100 // requires the bucket name.
102 this.buckets.push(bucket);
103 this.addToBucket(bktId);
108 addToBucket(id: number) {
109 if (this.recIds.length > 0) {
110 this.addRecordToBucket(id);
111 } else if (this.qId) {
112 this.addQueueToBucket(id);
116 // Add the record(s) to the bucket with provided ID.
117 addRecordToBucket(bucketId: number) {
119 this.recIds.forEach(recId => {
120 const item = this.idl.create('cbrebi');
121 item.bucket(bucketId);
122 item.target_biblio_record_entry(recId);
128 'open-ils.actor.container.item.create',
129 this.auth.token(), 'biblio', items
130 ).subscribe(resp => {
131 const evt = this.evt.parse(resp);
133 this.toast.danger(evt.toString());
140 addQueueToBucket(bucketId: number) {
141 const bucket = this.buckets.filter(b => b.id() === bucketId)[0];
142 if (!bucket) { return; }
146 'open-ils.vandelay.bib_queue.to_bucket',
147 this.auth.token(), this.qId, bucket.name()
148 ).toPromise().then(resp => {
149 const evt = this.evt.parse(resp);
151 this.toast.danger(evt.toString());