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[];
35 onOpenSub: Subscription;
37 @ViewChild('successMsg')
38 private successMsg: StringComponent;
40 @ViewChild('errorMsg')
41 private errorMsg: StringComponent;
44 private modal: NgbModal, // required for passing to parent
45 private toast: ToastService,
46 private idl: IdlService,
47 private pcrud: PcrudService,
48 private localStore: StoreService) {
49 super(modal); // required for subclassing
54 this.onOpenSub = this.onOpen$.subscribe(() => {
55 this.ids = [].concat(this.copyIds);
56 this.numSucceeded = 0;
59 this.localStore.getLocalItem('eg.cat.marked_conjoined_record');
61 if (!this.peerRecord) {
65 if (this.peerTypes.length === 0) {
74 this.onOpenSub.unsubscribe();
78 this.existingMaps = {};
79 this.pcrud.search('bpbcm',
80 {target_copy: this.copyIds, peer_record: this.peerRecord})
81 .subscribe(map => this.existingMaps[map.target_copy()] = map);
84 getPeerTypes(): Promise<any> {
85 return this.pcrud.retrieveAll('bpt', {}, {atomic: true}).toPromise()
87 // Map types to ComboboxEntry's
88 this.peerTypes = types.map(t => ({id: t.id(), label: t.name()}))
92 peerTypeChanged(entry: ComboboxEntry) {
94 this.peerType = entry.id;
100 linkCopies(): Promise<any> {
102 if (this.ids.length === 0) {
103 this.close(this.numSucceeded > 0);
104 return Promise.resolve();
107 const id = this.ids.pop();
108 const map = this.existingMaps[id] || this.idl.create('bpbcm');
109 map.peer_record(this.peerRecord);
111 map.peer_type(this.peerType);
113 let promise: Promise<any>;
114 if (this.existingMaps[id]) {
115 promise = this.pcrud.update(map).toPromise();
117 promise = this.pcrud.create(map).toPromise();
122 this.successMsg.current().then(msg => this.toast.success(msg));
124 return this.linkCopies();
129 this.errorMsg.current().then(msg => this.toast.warning(msg));
130 return this.linkCopies();