Fix ng lint errors
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / staff / catalog / record / upload-jacket-image-dialog.component.ts
1 import {Component, Input, OnInit, ViewChild} from '@angular/core';
2 import {FormControl} from '@angular/forms';
3 import {takeLast, finalize} from 'rxjs/operators';
4 import {DialogComponent} from '@eg/share/dialog/dialog.component';
5 import {AuthService} from '@eg/core/auth.service';
6 import {NetService} from '@eg/core/net.service';
7 import {EventService} from '@eg/core/event.service';
8 import {ToastService} from '@eg/share/toast/toast.service';
9 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
10 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {HttpClient, HttpResponse, HttpErrorResponse, HttpEventType} from '@angular/common/http';
13
14 @Component({
15   selector: 'eg-upload-jacket-image-dialog',
16   templateUrl: './upload-jacket-image-dialog.component.html'
17 })
18
19
20 export class UploadJacketImageDialogComponent extends DialogComponent implements OnInit {
21
22     // ID of bib record for jacket image
23     @Input() recordId: number;
24
25     uploading: boolean;
26     noFile: boolean;
27     errorUploading: boolean;
28     errorAuthentication: boolean;
29     errorAuthorization: boolean;
30     errorCompressionConfig: boolean;
31     errorNotFound: boolean;
32     errorLocationConfig: boolean;
33     errorWritingFile: boolean;
34     errorSize: boolean;
35     errorParsing: boolean;
36     errorGeneric: boolean;
37
38     private fileEvent: any;
39
40     constructor(
41         private modal: NgbModal,
42         private auth: AuthService,
43         private evt: EventService,
44         private net: NetService,
45         private toast: ToastService,
46         private http: HttpClient
47     ) {
48         super(modal);
49     }
50
51     clearErrors() {
52         this.errorAuthentication = false;
53         this.errorAuthorization = false;
54         this.errorCompressionConfig = false;
55         this.errorNotFound = false;
56         this.errorLocationConfig = false;
57         this.errorWritingFile = false;
58         this.errorSize = false;
59         this.errorParsing = false;
60         this.errorGeneric = false;
61         this.errorUploading = false;
62     }
63
64     ngOnInit() {
65         this.uploading = false;
66         this.noFile = true;
67         this.clearErrors();
68     }
69
70     onFileSelected(event) {
71         console.debug('onFileSelected', event);
72         this.fileEvent = event;
73         const file: File = this.fileEvent.target.files[0];
74         if (file) {
75             this.noFile = false;
76         } else {
77             this.noFile = true;
78         }
79     }
80
81     uploadJacketImage() {
82         const file: File = this.fileEvent.target.files[0];
83         if (file) {
84             this.uploading = true;
85             this.clearErrors();
86             const formData = new FormData();
87             formData.append('jacket_upload', file);
88             formData.append('ses', this.auth.token());
89             formData.append('bib_record', this.recordId.toString());
90
91             const upload$ = this.http.post('/jacket-upload', formData, {
92                 reportProgress: true,
93                 observe: 'events'
94             });
95
96             upload$.subscribe(
97               x => {
98                 console.debug('Jacket upload: ' , x);
99                 if (x instanceof HttpResponse) {
100                     console.debug('yay', x.body);
101                     if (x.body !== '1') {
102                         this.uploading = false;
103                         this.errorUploading = true;
104                     }
105                     switch (x.body) {
106                         case 'session not found': this.errorAuthentication = true; break;
107                         case 'permission denied': this.errorAuthorization = true; break;
108                         case 'invalid compression level': this.errorCompressionConfig = true; break;
109                         case 'bib not found': this.errorNotFound = true; break;
110                         case 'jacket location not configured': this.errorLocationConfig = true; break;
111                         case 'unable to open file for writing': this.errorWritingFile = true; break;
112                         case 'file too large': this.errorSize = true; break;
113                         case 'parse error': this.errorParsing = true; break;
114                         case 'upload error': this.errorGeneric = true; break;
115                         default: this.errorGeneric = true; break;
116                     }
117                 }
118               },
119               err => {
120                 this.uploading = false;
121                 this.errorUploading = true;
122                 this.errorGeneric = true;
123                 console.error('jacket upload error: ' , err);
124               },
125               () => this.refreshPage()
126             );
127         }
128     }
129
130     refreshPage() {
131         if (this.errorUploading) {
132             console.debug('no refresh page due to error');
133         } else {
134             console.debug('refresh page');
135             location.href = location.href;
136         }
137     }
138 }