]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/marc-edit/editor.component.ts
LP1830973 Angular 8 updates
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / marc-edit / editor.component.ts
1 import {Component, Input, Output, OnInit, EventEmitter, ViewChild} from '@angular/core';
2 import {IdlService} from '@eg/core/idl.service';
3 import {EventService} from '@eg/core/event.service';
4 import {NetService} from '@eg/core/net.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {OrgService} from '@eg/core/org.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {ToastService} from '@eg/share/toast/toast.service';
9 import {StringComponent} from '@eg/share/string/string.component';
10 import {MarcRecord} from './marcrecord';
11 import {ComboboxEntry, ComboboxComponent
12   } from '@eg/share/combobox/combobox.component';
13 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
14
15
16 /**
17  * MARC Record editor main interface.
18  */
19
20 @Component({
21   selector: 'eg-marc-editor',
22   templateUrl: './editor.component.html'
23 })
24
25 export class MarcEditorComponent implements OnInit {
26
27     record: MarcRecord;
28     editorTab: 'rich' | 'flat';
29     sources: ComboboxEntry[];
30
31     @Input() set recordId(id: number) {
32         if (!id) { return; }
33         if (this.record && this.record.id === id) { return; }
34         this.fromId(id);
35     }
36
37     @Input() set recordXml(xml: string) {
38         if (xml) { this.fromXml(xml); }
39     }
40
41     // If true, saving records to the database is assumed to
42     // happen externally.  IOW, the record editor is just an
43     // in-place MARC modification interface.
44     inPlaceMode: boolean;
45
46     // In inPlaceMode, this is emitted in lieu of saving the record
47     // in th database.  When inPlaceMode is false, this is emitted after
48     // the record is successfully saved.
49     @Output() recordSaved: EventEmitter<string>;
50
51     @ViewChild('sourceSelector', { static: true }) sourceSelector: ComboboxComponent;
52     @ViewChild('confirmDelete', { static: true }) confirmDelete: ConfirmDialogComponent;
53     @ViewChild('confirmUndelete', { static: true }) confirmUndelete: ConfirmDialogComponent;
54     @ViewChild('cannotDelete', { static: true }) cannotDelete: ConfirmDialogComponent;
55     @ViewChild('successMsg', { static: true }) successMsg: StringComponent;
56     @ViewChild('failMsg', { static: true }) failMsg: StringComponent;
57
58     constructor(
59         private evt: EventService,
60         private idl: IdlService,
61         private net: NetService,
62         private auth: AuthService,
63         private org: OrgService,
64         private pcrud: PcrudService,
65         private toast: ToastService
66     ) {
67         this.sources = [];
68         this.recordSaved = new EventEmitter<string>();
69     }
70
71     ngOnInit() {
72         // Default to flat for now since it's all that's supported.
73         this.editorTab = 'flat';
74
75         this.pcrud.retrieveAll('cbs').subscribe(
76             src => this.sources.push({id: +src.id(), label: src.source()}),
77             _ => {},
78             () => {
79                 this.sources = this.sources.sort((a, b) =>
80                     a.label.toLowerCase() < b.label.toLowerCase() ? -1 : 1
81                 );
82             }
83         );
84     }
85
86     saveRecord(): Promise<any> {
87         const xml = this.record.toXml();
88
89         if (this.inPlaceMode) {
90             // Let the caller have the modified XML and move on.
91             this.recordSaved.emit(xml);
92             return Promise.resolve();
93         }
94
95         const source = this.sourceSelector.selected ?
96             this.sourceSelector.selected.label : null; // 'label' not a typo
97
98         if (this.record.id) { // Editing an existing record
99
100             const method = 'open-ils.cat.biblio.record.marc.replace';
101
102             return this.net.request('open-ils.cat', method,
103                 this.auth.token(), this.record.id, xml, source
104             ).toPromise().then(response => {
105
106                 const evt = this.evt.parse(response);
107                 if (evt) {
108                     console.error(evt);
109                     this.failMsg.current().then(msg => this.toast.warning(msg));
110                     return;
111                 }
112
113                 this.successMsg.current().then(msg => this.toast.success(msg));
114                 this.recordSaved.emit(xml);
115                 return response;
116             });
117
118         } else {
119             // TODO: create a new record
120         }
121     }
122
123     fromId(id: number): Promise<any> {
124         return this.pcrud.retrieve('bre', id)
125         .toPromise().then(bib => {
126             this.record = new MarcRecord(bib.marc());
127             this.record.id = id;
128             this.record.deleted = bib.deleted() === 't';
129             if (bib.source()) {
130                 this.sourceSelector.applyEntryId(+bib.source());
131             }
132         });
133     }
134
135     fromXml(xml: string) {
136         this.record = new MarcRecord(xml);
137         this.record.id = null;
138     }
139
140     deleteRecord(): Promise<any> {
141
142         return this.confirmDelete.open().toPromise()
143         .then(yes => {
144             if (!yes) { return; }
145
146             return this.net.request('open-ils.cat',
147                 'open-ils.cat.biblio.record_entry.delete',
148                 this.auth.token(), this.record.id).toPromise()
149
150             .then(resp => {
151
152                 const evt = this.evt.parse(resp);
153                 if (evt) {
154                     if (evt.textcode === 'RECORD_NOT_EMPTY') {
155                         return this.cannotDelete.open().toPromise();
156                     } else {
157                         console.error(evt);
158                         return alert(evt);
159                     }
160                 }
161                 return this.fromId(this.record.id)
162                 .then(_ => this.recordSaved.emit(this.record.toXml()));
163             });
164         });
165     }
166
167     undeleteRecord(): Promise<any> {
168
169         return this.confirmUndelete.open().toPromise()
170         .then(yes => {
171             if (!yes) { return; }
172
173             return this.net.request('open-ils.cat',
174                 'open-ils.cat.biblio.record_entry.undelete',
175                 this.auth.token(), this.record.id).toPromise()
176
177             .then(resp => {
178
179                 const evt = this.evt.parse(resp);
180                 if (evt) { console.error(evt); return alert(evt); }
181
182                 return this.fromId(this.record.id)
183                 .then(_ => this.recordSaved.emit(this.record.toXml()));
184             });
185         });
186     }
187 }
188