Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 7x 81x 8x 8x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { FormArray, FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms'; import { MatFormField, MatLabel } from '@angular/material/form-field'; import { MatInput } from '@angular/material/input'; import { JobService } from '@core/services/job.service'; import { CreateJobAttachmentsRequest } from '@core/model/create-job-attachments-request.interface'; import { Job } from '@core/model/job.interface'; import { catchError, take } from 'rxjs'; import { ApiError } from '@core/errors/api-error'; import { CreateJobAttachmentRequest } from '@core/model/create-job-attachment-request.interface'; import { MatButton, MatIconButton } from '@angular/material/button'; import { NotificationService } from '@core/services/notification.service'; import { MatIcon } from '@angular/material/icon'; import { ErrorProcessorService } from '@core/services/error-processor.service'; @Component({ selector: 'app-job-attachments-form', imports: [ReactiveFormsModule, MatFormField, MatInput, MatLabel, MatButton, MatIcon, MatIconButton], templateUrl: './job-attachments-form.component.html', styleUrl: './job-attachments-form.component.scss' }) export class JobAttachmentsFormComponent implements OnInit { @Input({ required: true }) job!: Job; @Input() defaultAttachements = 0; @Output() attachmentsSaved = new EventEmitter<Job>(); loading = false; attachmentForm!: FormGroup; maxFileSize = 2*1024*1024; constructor( private readonly fb: FormBuilder, private readonly jobService: JobService, private readonly notificationService: NotificationService, private errorProcessorService: ErrorProcessorService) { } ngOnInit(): void { this.attachmentForm = this.fb.group({ attachments: this.fb.array([]) }); for(let a=0; a<this.defaultAttachements; a++) { this.addAttachment(); } } get attachments(): FormArray { return this.attachmentForm.get('attachments') as FormArray; } addAttachment(): void { const attachmentGroup = this.fb.group({ name: ['', Validators.required], file: [null, Validators.required], filename: [], content: [], fileU: ['', Validators.required] }); this.attachments.push(attachmentGroup); } removeAttachment(index: number): void { this.attachments.removeAt(index); } onFileChange(event: Event, index: number): void { const input = event.target as HTMLInputElement; if (input.files && input.files.length > 0) { const file = input.files[0]; if(file.size > this.maxFileSize) { input.value = ''; alert($localize`:@@alert.file_too_big:File too big`); } else { // Lire le fichier et le convertir en Base64 const reader = new FileReader(); reader.onload = () => { const base64String = reader.result as string; this.attachments.at(index).patchValue({ filename: file.name, content: base64String, fileU: 'chosen' }); }; reader.readAsDataURL(file); } } } submit(): void { if (this.attachmentForm.valid) { this.loading = true; const attachments: {}[] = new Array(); this.attachments.controls.forEach((attachment, index) => { attachments[index] = {'name': attachment.value.name, 'content': attachment.value.content, 'filename': attachment.value.filename} as CreateJobAttachmentRequest; }); // send `formData` to backend this.jobService.createAttachments(this.job.id, {attachments: attachments} as CreateJobAttachmentsRequest).pipe( take(1), catchError( (error: ApiError) => { this.loading = false; // set an explicit error message error.message = `Attachments could not be created.${error.message}`; return this.errorProcessorService.processError(error); } ) ) .subscribe((job) => { this.loading = false; this.notificationService.confirmation( attachments.length > 1 ? $localize`:@@message.attachements.created:${attachments.length} attachments created successfully` : $localize`:@@message.attachement.created:Attachment created successfully` ); this.attachmentsSaved.emit(job); }); } } } |