eb99aec3be4fe2fcb75f5f5872fbbb73f48a799c
[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     recId: number;
30     @Input() set recordId(id: number) {
31         this.recId = id;
32     }
33
34     // Add items from a (vandelay) bib queue to a bucket
35     qId: number;
36     @Input() set queueId(id: number) {
37         this.qId = id;
38     }
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     }
50
51     ngOnInit() {
52         
53         if (this.qId) {
54             this.bucketType = 'vandelay_queue';
55         } else {
56             this.bucketType = 'staff_client';
57         }
58
59         this.onOpen$.subscribe(ok => {
60             // Reset data on dialog open
61
62             this.selectedBucket = null;
63             this.newBucketName = '';
64             this.newBucketDesc = '';
65
66             this.net.request(
67                 'open-ils.actor',
68                 'open-ils.actor.container.retrieve_by_class.authoritative',
69                 this.auth.token(), this.auth.user().id(),
70                 'biblio', this.bucketType
71             ).subscribe(buckets => this.buckets = buckets);
72         });
73     }
74
75     addToSelected() {
76         this.addToBucket(this.selectedBucket);
77     }
78
79     // Create a new bucket then add the record
80     addToNew() {
81         const bucket = this.idl.create('cbreb');
82
83         bucket.owner(this.auth.user().id());
84         bucket.name(this.newBucketName);
85         bucket.description(this.newBucketDesc);
86         bucket.btype(this.bucketType);
87
88         this.net.request(
89             'open-ils.actor',
90             'open-ils.actor.container.create',
91             this.auth.token(), 'biblio', bucket
92         ).subscribe(bktId => {
93             const evt = this.evt.parse(bktId);
94             if (evt) {
95                 this.toast.danger(evt.desc);
96             } else {
97                 // make it find-able to the queue-add method which
98                 // requires the bucket name.
99                 bucket.id(bktId);
100                 this.buckets.push(bucket);
101
102                 this.addToBucket(bktId);
103             }
104         });
105     }
106
107     // Add the record to the selected existing bucket
108     addToBucket(id: number) {
109         if (this.recId) {
110             this.addRecordToBucket(id);
111         } else if (this.qId) {
112             this.addQueueToBucket(id);
113         }
114     }
115
116     addRecordToBucket(bucketId: number) {
117         const item = this.idl.create('cbrebi');
118         item.bucket(bucketId);
119         item.target_biblio_record_entry(this.recId);
120         this.net.request(
121             'open-ils.actor',
122             'open-ils.actor.container.item.create',
123             this.auth.token(), 'biblio', item
124         ).subscribe(resp => {
125             const evt = this.evt.parse(resp);
126             if (evt) {
127                 this.toast.danger(evt.toString());
128             } else {
129                 this.close();
130             }
131         });
132     }
133
134     addQueueToBucket(bucketId: number) {
135         const bucket = this.buckets.filter(b => b.id() === bucketId)[0];
136         if (!bucket) { return; }
137
138         this.net.request(
139             'open-ils.vandelay',
140             'open-ils.vandelay.bib_queue.to_bucket',
141             this.auth.token(), this.qId, bucket.name()
142         ).toPromise().then(resp => {
143             const evt = this.evt.parse(resp);
144             if (evt) {
145                 this.toast.danger(evt.toString());
146             } else {
147                 this.close();
148             }
149         });
150     }
151 }
152
153
154