1 import {Component, OnInit, OnDestroy, Input, ViewChild, Renderer2} from '@angular/core';
2 import {Subscription} from 'rxjs';
3 import {IdlService, IdlObject} 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[];
27 // If true, ignore the provided copyIds array and fetch all of
28 // the linked copies to work on.
29 @Input() modifyAll: boolean;
31 // If peerRecord is not set, the localStorage value will be used.
32 @Input() peerRecord: number;
37 peerTypes: ComboboxEntry[];
39 onOpenSub: Subscription;
41 @ViewChild('successMsg', { static: true }) private successMsg: StringComponent;
42 @ViewChild('errorMsg', { static: true }) private errorMsg: StringComponent;
45 private modal: NgbModal, // required for passing to parent
46 private toast: ToastService,
47 private idl: IdlService,
48 private pcrud: PcrudService,
49 private localStore: StoreService) {
50 super(modal); // required for subclassing
56 this.onOpenSub = this.onOpen$.subscribe(() => {
58 // This will be set once the list of copies to
59 // modify has been fetched.
62 this.numSucceeded = 0;
65 if (!this.peerRecord) {
67 this.localStore.getLocalItem('eg.cat.marked_conjoined_record');
69 if (!this.peerRecord) {
74 if (this.peerTypes.length === 0) {
78 this.fetchExistingMaps();
83 this.onOpenSub.unsubscribe();
87 this.existingMaps = {};
89 peer_record: this.peerRecord
92 if (!this.modifyAll) {
93 search.target_copy = this.copyIds;
96 this.pcrud.search('bpbcm', search)
98 this.existingMaps[map.target_copy()] = map;
100 this.copyIds.push(map.target_copy());
105 // Fetch and map peer types to combobox entries
106 getPeerTypes(): Promise<any> {
107 return this.pcrud.retrieveAll('bpt', {}, {atomic: true}).toPromise()
109 this.peerTypes = types.map(t => ({id: t.id(), label: t.name()}))
113 peerTypeChanged(entry: ComboboxEntry) {
115 this.peerType = entry.id;
117 this.peerType = null;
121 // Create or update peer copy links.
124 const maps: IdlObject[] = [];
125 this.copyIds.forEach(id => {
127 if (this.existingMaps[id]) {
128 map = this.existingMaps[id];
131 map = this.idl.create('bpbcm');
135 map.peer_record(this.peerRecord);
137 map.peer_type(this.peerType);
141 return this.pcrud.autoApply(maps).subscribe(
142 ok => this.numSucceeded++,
146 this.errorMsg.current().then(msg => this.toast.warning(msg));
149 this.successMsg.current().then(msg => this.toast.success(msg));
150 this.close(this.numSucceeded > 0);