1 import {Component, OnInit, OnDestroy, Input, ViewChild, Renderer2} from '@angular/core';
2 import {Subscription} from 'rxjs';
3 import {IdlService} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {StoreService} from '@eg/core/store.service';
7 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
8 import {DialogComponent} from '@eg/share/dialog/dialog.component';
9 import {StringComponent} from '@eg/share/string/string.component';
10 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
14 * Dialog for linking conjoined items.
18 selector: 'eg-conjoined-items-dialog',
19 templateUrl: 'conjoined-items-dialog.component.html'
22 export class ConjoinedItemsDialogComponent
23 extends DialogComponent implements OnInit, OnDestroy {
25 @Input() copyIds: number[];
26 ids: number[]; // copy of list so we can pop()
31 peerTypes: ComboboxEntry[];
34 onOpenSub: Subscription;
36 @ViewChild('successMsg')
37 private successMsg: StringComponent;
39 @ViewChild('errorMsg')
40 private errorMsg: StringComponent;
43 private modal: NgbModal, // required for passing to parent
44 private toast: ToastService,
45 private idl: IdlService,
46 private pcrud: PcrudService,
47 private localStore: StoreService) {
48 super(modal); // required for subclassing
53 this.onOpenSub = this.onOpen$.subscribe(() => {
54 this.ids = [].concat(this.copyIds);
55 this.numSucceeded = 0;
58 this.localStore.getLocalItem('eg.cat.marked_conjoined_record');
60 if (!this.peerRecord) {
64 if (this.peerTypes.length === 0) {
71 this.onOpenSub.unsubscribe();
74 getPeerTypes(): Promise<any> {
75 return this.pcrud.retrieveAll('bpt', {}, {atomic: true}).toPromise()
77 // Map types to ComboboxEntry's
78 this.peerTypes = types.map(t => ({id: t.id(), label: t.name()}))
82 peerTypeChanged(entry: ComboboxEntry) {
84 this.peerType = entry.id;
90 linkCopies(): Promise<any> {
92 if (this.ids.length === 0) {
93 this.close(this.numSucceeded > 0);
94 return Promise.resolve();
97 const id = this.ids.pop();
98 const map = this.idl.create('bpbcm');
99 map.peer_record(this.peerRecord);
101 map.peer_type(this.peerType);
103 return this.pcrud.create(map).toPromise().then(
105 this.successMsg.current().then(msg => this.toast.success(msg));
107 return this.linkCopies();
112 this.errorMsg.current().then(msg => this.toast.warning(msg));
113 return this.linkCopies();