All files / src/app/features/jobs/job-attachments job-attachments.component.ts

69.35% Statements 43/62
25% Branches 2/8
60% Functions 15/25
100% Lines 39/39

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 959x   9x   9x 9x 9x 9x 9x 9x 9x   9x               9x   9x 9x   9x     9x 9x 9x 9x 9x     10x       2x     1x   1x 1x 1x         1x 1x             1x 1x     1x 1x 1x     1x         1x       1x       2x 1x     1x         1x        
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Job } from '@core/model/job.interface';
import { JobService } from '@core/services/job.service';
import { Attachment } from '@core/model/attachment.interface';
import { catchError, EMPTY, of, switchMap, take, tap } from 'rxjs';
import { ConfirmDialogService } from '@core/services/confirm-dialog.service';
import { FileService } from '@core/services/file.service';
import { MatButton } from '@angular/material/button';
import { JobAttachmentsFormComponent } from '@app/features/jobs/forms/job-attachments-form/job-attachments-form.component';
import { ModalService } from '@core/services/modal.service';
import { DatePipe } from '@angular/common';
import { ProtectedFile } from '@app/core/model/protected-file.interface';
import { NotificationService } from '@app/core/services/notification.service';
 
@Component({
  selector: 'app-job-attachments',
  imports: [MatButton, JobAttachmentsFormComponent, DatePipe],
  templateUrl: './job-attachments.component.html',
  styleUrl: './job-attachments.component.scss'
})
export class JobAttachmentsComponent implements OnInit {
  @Input({ required: true }) job!: Job;
  @Input() formMode = 'inline';
  @Output() attachmentsSaved = new EventEmitter<Job>();
 
  protected displayForm = this.formMode === 'inline';
 
  constructor(
    private jobService: JobService, 
    private confirmDialogService: ConfirmDialogService, 
    private fileService: FileService, 
    private modalService: ModalService, 
    private notificationService: NotificationService){}
 
  ngOnInit(): void {
    this.displayForm = this.formMode === 'inline';
  }
 
  downloadAttachement(job: Job, attachment: Attachment) :void {
    this.jobService.getProtectedFile(job.id, attachment.id)
      .pipe(
        switchMap((p: ProtectedFile) =>
          this.fileService.downloadFile(p.url, true).pipe(
            tap((blob) => {
              const objectUrl = URL.createObjectURL(blob);
              window.open(objectUrl, '_blank');
              URL.revokeObjectURL(objectUrl);
            })
          )
        ),
      catchError((err) => {
        this.notificationService.error($localize `:download file|message indicating file download has failed@@error.file.download:File download failed`, err);
        return EMPTY;
      })
  )
  .subscribe();
  }
 
  confirmDeleteAttachment(job: Job, attachment: Attachment) :void {
    console.log('deleting attachment');
    this.jobService.deleteAttachment(job.id, attachment.id).pipe(
      take(1),
      tap(() => {
        this.notificationService.confirmation($localize `:@@message.attachment.deleted:Attachment deleted successfully`);
        console.log('filtering attachments');
        job.attachments = job.attachments.filter((a) => a.id != attachment.id )
    })
    ).subscribe(() => {
      console.log('something happened');
    });
  }
 
 deleteAttachment(job: Job, attachment: Attachment) :void {
    this.confirmDialogService.openConfirmDialog($localize `:@@info.attachment.delete.confirmation_required:Delete attachment "${attachment.name}" ?`, () => this.confirmDeleteAttachment(job, attachment));
  }
 
  cancelForm() :void {
    this.displayForm = false;
  } 
 
 addAttachment(job: Job) :void {
  if(this.formMode === 'inline') {
    this.displayForm = true;
  }
  else {
    this.modalService.openJobModal('attachments-form', job, () => this.onAttachmentsSaved(job), {defaultAttachments: 1});
  }
 }
 
 onAttachmentsSaved(job: Job): void {
  this.attachmentsSaved.emit(job);
 }
 
}