]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/record/add-to-carousel-dialog.component.ts
LP1922120: Add to carousel action in angular catalog
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / record / add-to-carousel-dialog.component.ts
1 import {Component, Input, OnInit, ViewChild} from '@angular/core';
2 import {FormControl} from '@angular/forms';
3 import {takeLast} from 'rxjs/operators';
4 import {DialogComponent} from '@eg/share/dialog/dialog.component';
5 import {AuthService} from '@eg/core/auth.service';
6 import {NetService} from '@eg/core/net.service';
7 import {EventService} from '@eg/core/event.service';
8 import {ToastService} from '@eg/share/toast/toast.service';
9 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
10 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
11 import {StringComponent} from '@eg/share/string/string.component';
12
13 @Component({
14   selector: 'eg-add-to-carousel-dialog',
15   templateUrl: './add-to-carousel-dialog.component.html'
16 })
17
18
19 export class AddToCarouselDialogComponent extends DialogComponent implements OnInit {
20
21     // IDs of records to add to the carousel
22     @Input() recordIds: number[];
23
24
25     @ViewChild('successMsg', { static: true }) private successMsg: StringComponent;
26     @ViewChild('errorMsg', { static: true }) private errorMsg: StringComponent;
27
28     selectedCarousel = new FormControl('');
29
30     private carousels = [];
31
32     public addToCarousel: () => void;
33     private reset: () => void;
34
35     constructor(
36         private modal: NgbModal,
37         private auth: AuthService,
38         private evt: EventService,
39         private net: NetService,
40         private toast: ToastService
41     ) {
42         super(modal);
43     }
44
45     ngOnInit() {
46         this.onOpen$.subscribe(ok => {
47             this.reset();
48             this.net.request(
49                 'open-ils.actor',
50                 'open-ils.actor.carousel.retrieve_manual_by_staff',
51                 this.auth.token()
52             ).subscribe(carousels => this.carousels = carousels);
53         });
54
55         this.reset = () => {
56             this.carousels = [];
57         };
58
59         this.addToCarousel = () => {
60             this.net.request(
61                 'open-ils.actor',
62                 'open-ils.actor.container.item.create.batch',
63                 this.auth.token(),
64                 'biblio_record_entry',
65                 this.selectedCarousel.value['id'],
66                 this.recordIds
67             ).pipe(takeLast(1))
68             .subscribe(
69                 result => {
70                     const evt = this.evt.parse(result);
71                     if (evt) {
72                         this.errorMsg.current().then(m => this.toast.danger(m));
73                     } else {
74                         this.successMsg.current().then(m => this.toast.success(m));
75                         this.close(true);
76                     }
77                 }
78             );
79         };
80     }
81
82     formatCarouselEntries(): ComboboxEntry[] {
83         return this.carousels.map(carousel => ({id: carousel['bucket'], label: carousel['name']}));
84     }
85
86 }