]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/cat/authority/merge-dialog.component.ts
LP1879335 Manage Authorities Angular Port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / authority / merge-dialog.component.ts
1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {NetService} from '@eg/core/net.service';
3 import {EventService} from '@eg/core/event.service';
4 import {ToastService} from '@eg/share/toast/toast.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {DialogComponent} from '@eg/share/dialog/dialog.component';
7 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
8 import {StringComponent} from '@eg/share/string/string.component';
9
10 /**
11  * Dialog for merging authority records.
12  */
13
14 @Component({
15   selector: 'eg-authority-merge-dialog',
16   templateUrl: 'merge-dialog.component.html'
17 })
18
19 export class AuthorityMergeDialogComponent
20     extends DialogComponent implements OnInit {
21
22     // Rows passed from the authority browse grid.
23     @Input() authData: any[] = [];
24
25     leadRecord: number;
26
27     @ViewChild('successMsg', {static: true})
28         private successMsg: StringComponent;
29
30     @ViewChild('errorMsg', {static: true})
31         private errorMsg: StringComponent;
32
33     constructor(
34         private modal: NgbModal, // required for passing to parent
35         private toast: ToastService,
36         private net: NetService,
37         private evt: EventService,
38         private auth: AuthService) {
39         super(modal); // required for subclassing
40     }
41
42     ngOnInit() {
43         this.onOpen$.subscribe(_ => {
44             if (this.authData.length > 0) {
45                 this.leadRecord = this.authData[0].authority.id();
46             }
47         });
48     }
49
50     merge() {
51
52         const list = this.authData
53             .map(data => data.authority.id())
54             .filter(id => id !== this.leadRecord);
55
56         this.net.request('open-ils.cat',
57             'open-ils.cat.authority.records.merge',
58             this.auth.token(), this.leadRecord, list)
59         .subscribe(resp => {
60             const evt = this.evt.parse(resp);
61
62             if (evt) {
63                 this.errorMsg.current().then(str => this.toast.warning(str));
64                 this.close(false);
65             } else {
66                 this.successMsg.current().then(str => this.toast.success(str));
67                 this.close(true);
68             }
69         });
70     }
71 }
72
73
74