import {Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core'; import {FieldType} from '@ngx-formly/core'; import {AllService} from '../../../all.service'; @Component({ selector: 'jhi-file-custom', templateUrl: './file-custom.component.html', styleUrls: ['./file-custom.component.scss'] }) export class FileCustomComponent extends FieldType implements OnInit, OnDestroy { fileName = ''; subcriberSepa = null; messageErrorFile = ''; @ViewChild('fileField') fileField: ElementRef; constructor(public allService: AllService) { super(); } ngOnInit(): void { this.subcriberSepa = this.allService.getFileSepaRequiredAsObserVable().subscribe((state) => { if (state) { this.fileField.nativeElement.value = ''; this.fileName = ''; this.model['insured_sepa_path'] = null; } else { this.field.templateOptions.required = false; } }); if (this.model['insured_sepa_path'] && this.model['insured_sepa_path'] != null) { this.fileName = this.model['insured_sepa_path'].substring(this.model['insured_sepa_path'].lastIndexOf('/') + 1); this.field.templateOptions.required = false; } } ngOnDestroy(): void { this.subcriberSepa.unsubscribe(); } handleFileInput(target: any) { this.messageErrorFile = ''; const fileToUpload = target.files.item(0); if (!fileToUpload || !this.checkFileSizeIsValid(fileToUpload)) { this.fileName = ''; this.messageErrorFile = 'Le fichier dépasse la taille maximum (5Mo)'; return; } this.fileName = fileToUpload.name; } checkFileSizeIsValid(fileToUpload): boolean { return !(fileToUpload && fileToUpload.size > this.allService.configurationPreview.maxUploadSize); } }