import {Component, OnInit} from '@angular/core'; import {FieldType} from '@ngx-formly/core'; import {AllService} from '../../../all.service'; import {ActivatedRoute} from '@angular/router'; @Component({ selector: 'jhi-file-upload', templateUrl: './file-upload.component.html', styleUrls: ['./file-upload.component.scss'] }) export class FileUploadComponent extends FieldType implements OnInit { fileToUpload = null; label = ''; fieldName = ''; inUpload = false; uploadOk = false; uploadError = false; contractid = ''; messageErrorFile = ''; required = null; constructor(private allService: AllService, private route: ActivatedRoute) { super(); } ngOnInit() { const fieldName = 'fieldName'; const required = 'required'; this.fieldName = this.field.templateOptions[fieldName]; this.required = this.field.templateOptions[required]; this.setLabel(); this.contractid = this.route.snapshot.paramMap.get('contractid'); } handleFileInput(target: any) { this.messageErrorFile = ''; this.fileToUpload = target.files.item(0); if (this.fileToUpload.size > this.allService.configurationPreview.maxUploadSize) { this.messageErrorFile = 'Le fichier dépasse la taille maximum (5Mo)'; return; } this.inUpload = true; this.uploadOk = false; this.uploadError = false; this.uploadFileToActivity(); } uploadFileToActivity() { this.allService.currentStepCustomError = true; this.contractid = this.route.snapshot.paramMap.get('contractid'); this.allService.contractDocumentService.storeCNI(this.fileToUpload, this.fieldName, this.contractid).subscribe(response => { this.allService.currentStepCustomError = false; if (this.fieldName === 'frontCNI') { this.model['insured_identity_CNI_front'] = response.path; } else { this.model['insured_identity_CNI_back'] = response.path; } this.setLabel(); this.inUpload = false; this.uploadOk = true; }, error => { this.allService.currentStepCustomError = false; if (this.fieldName === 'frontCNI') { this.model['insured_identity_CNI_front'] = ''; } else { this.model['insured_identity_CNI_back'] = ''; } this.setLabel(); this.inUpload = false; this.uploadError = true; console.log(error); }); } setLabel(): void { if (this.fieldName === 'frontCNI' && this.model['insured_identity_CNI_front'] && this.model['insured_identity_CNI_front'] != '') { this.label = this.model['insured_identity_CNI_front'].substring(this.model['insured_identity_CNI_front'].lastIndexOf('/') + 1); this.field.templateOptions.required = false; } else if (this.fieldName !== 'frontCNI' && this.model['insured_identity_CNI_back'] && this.model['insured_identity_CNI_back'] != '') { this.label = this.model['insured_identity_CNI_back'].substring(this.model['insured_identity_CNI_back'].lastIndexOf('/') + 1); this.field.templateOptions.required = false; } else { this.field.templateOptions.required = this.required; this.field.formControl.setValue(''); } } }