1 import {Component, AfterViewInit, ViewChild, Renderer2} from '@angular/core';
2 import {NgbPanelChangeEvent} from '@ng-bootstrap/ng-bootstrap';
3 import {HttpClient, HttpRequest, HttpEventType} from '@angular/common/http';
4 import {HttpResponse, HttpErrorResponse} from '@angular/common/http';
5 import {saveAs} from 'file-saver/FileSaver';
6 import {AuthService} from '@eg/core/auth.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {ProgressInlineComponent} from '@eg/share/dialog/progress-inline.component';
9 import {VandelayService, VANDELAY_EXPORT_PATH} from './vandelay.service';
13 templateUrl: 'export.component.html'
15 export class ExportComponent implements AfterViewInit {
24 recordEncoding: string;
25 includeHoldings: boolean;
28 @ViewChild('fileSelector') private fileSelector;
29 @ViewChild('exportProgress')
30 private exportProgress: ProgressInlineComponent;
33 private renderer: Renderer2,
34 private http: HttpClient,
35 private toast: ToastService,
36 private auth: AuthService
38 this.recordType = 'biblio';
39 this.recordFormat = 'USMARC';
40 this.recordEncoding = 'UTF-8';
41 this.includeHoldings = false;
45 this.renderer.selectRootElement('#csv-input').focus();
48 sourceChange($event: NgbPanelChangeEvent) {
49 this.recordSource = $event.panelId;
51 if ($event.nextState) { // panel opened
53 // give the panel a chance to render before focusing input
55 this.renderer.selectRootElement(
56 `#${this.recordSource}-input`).focus();
61 fileSelected($event) {
62 this.selectedFile = $event.target.files[0];
65 hasNeededData(): boolean {
67 this.selectedFile || this.recordId || this.bucketId
72 this.isExporting = true;
73 this.exportProgress.update({value: 0});
75 const formData: FormData = new FormData();
77 formData.append('ses', this.auth.token());
78 formData.append('rectype', this.recordType);
79 formData.append('encoding', this.recordEncoding);
80 formData.append('format', this.recordFormat);
82 if (this.includeHoldings) {
83 formData.append('holdings', '1');
86 switch (this.recordSource) {
89 formData.append('idcolumn', ''+this.fieldNumber);
90 formData.append('idfile',
91 this.selectedFile, this.selectedFile.name);
95 formData.append('id', ''+this.recordId);
99 formData.append('containerid', ''+this.bucketId);
103 this.sendExportRequest(formData);
106 sendExportRequest(formData: FormData) {
108 const fileName = `export.${this.recordType}.` +
109 `${this.recordEncoding}.${this.recordFormat}`;
111 const req = new HttpRequest('POST', VANDELAY_EXPORT_PATH,
112 formData, {reportProgress: true, responseType: 'text'});
114 this.http.request(req).subscribe(
117 if (evt.type === HttpEventType.DownloadProgress) {
118 // File size not reported by server in advance.
119 this.exportProgress.update({value: evt.loaded});
121 } else if (evt instanceof HttpResponse) {
123 saveAs(new Blob([evt.body],
124 {type: 'application/octet-stream'}), fileName);
126 this.isExporting = false;
130 (err: HttpErrorResponse) => {
132 this.toast.danger(err.error);
133 this.isExporting = false;