]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/buckets/record-bucket-dialog.component.ts
LP1806087 Angular staff catalog phase II.
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / buckets / record-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
10 /**
11  * Dialog for adding bib records to new and existing record buckets.
12  */
13
14 @Component({
15   selector: 'eg-record-bucket-dialog',
16   templateUrl: 'record-bucket-dialog.component.html'
17 })
18
19 export class RecordBucketDialogComponent
20     extends DialogComponent implements OnInit {
21
22     selectedBucket: number;
23     newBucketName: string;
24     newBucketDesc: string;
25     buckets: any[];
26
27     @Input() bucketType: string;
28
29     // Add one or more bib records to bucket by ID.
30     recIds: number[];
31     @Input() set recordId(id: number | number[]) {
32         this.recIds = [].concat(id);
33     }
34
35     // Add items from a (vandelay) bib queue to a bucket
36     qId: number;
37     @Input() set queueId(id: number) {
38         this.qId = id;
39     }
40
41     constructor(
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
50         this.recIds = [];
51     }
52
53     ngOnInit() {
54
55         if (this.qId) {
56             this.bucketType = 'vandelay_queue';
57         } else {
58             this.bucketType = 'staff_client';
59         }
60
61         this.onOpen$.subscribe(ok => {
62             // Reset data on dialog open
63
64             this.selectedBucket = null;
65             this.newBucketName = '';
66             this.newBucketDesc = '';
67
68             this.net.request(
69                 'open-ils.actor',
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);
74         });
75     }
76
77     addToSelected() {
78         this.addToBucket(this.selectedBucket);
79     }
80
81     // Create a new bucket then add the record
82     addToNew() {
83         const bucket = this.idl.create('cbreb');
84
85         bucket.owner(this.auth.user().id());
86         bucket.name(this.newBucketName);
87         bucket.description(this.newBucketDesc);
88         bucket.btype(this.bucketType);
89
90         this.net.request(
91             'open-ils.actor',
92             'open-ils.actor.container.create',
93             this.auth.token(), 'biblio', bucket
94         ).subscribe(bktId => {
95             const evt = this.evt.parse(bktId);
96             if (evt) {
97                 this.toast.danger(evt.desc);
98             } else {
99                 // make it find-able to the queue-add method which
100                 // requires the bucket name.
101                 bucket.id(bktId);
102                 this.buckets.push(bucket);
103                 this.addToBucket(bktId);
104             }
105         });
106     }
107
108     addToBucket(id: number) {
109         if (this.recIds.length > 0) {
110             this.addRecordToBucket(id);
111         } else if (this.qId) {
112             this.addQueueToBucket(id);
113         }
114     }
115
116     // Add the record(s) to the bucket with provided ID.
117     addRecordToBucket(bucketId: number) {
118         const items = [];
119         this.recIds.forEach(recId => {
120             const item = this.idl.create('cbrebi');
121             item.bucket(bucketId);
122             item.target_biblio_record_entry(recId);
123             items.push(item);
124         });
125
126         this.net.request(
127             'open-ils.actor',
128             'open-ils.actor.container.item.create',
129             this.auth.token(), 'biblio', items
130         ).subscribe(resp => {
131             const evt = this.evt.parse(resp);
132             if (evt) {
133                 this.toast.danger(evt.toString());
134             } else {
135                 this.close();
136             }
137         });
138     }
139
140     addQueueToBucket(bucketId: number) {
141         const bucket = this.buckets.filter(b => b.id() === bucketId)[0];
142         if (!bucket) { return; }
143
144         this.net.request(
145             'open-ils.vandelay',
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);
150             if (evt) {
151                 this.toast.danger(evt.toString());
152             } else {
153                 this.close();
154             }
155         });
156     }
157 }
158
159
160