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

55.68% Statements 49/88
40% Branches 6/15
25.92% Functions 7/27
80.7% Lines 46/57

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 124 125 126 127 128 129 130 131 132 133 13411x 11x   11x 11x 11x     11x   11x   11x 11x 11x 11x 11x 11x 11x 11x                     11x 6x 6x 6x   6x 6x 6x     6x                 6x 6x 6x 6x       7x                                                                                 5x 5x   5x 5x 1x 1x     4x     5x       2x   2x 2x 2x 2x         3x 3x 3x         7x 7x    
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { JobService } from '@core/services/job.service';
import { Job } from '@core/model/job.interface';
import { AbstractControl, FormBuilder, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { EditorComponent, TINYMCE_SCRIPT_SRC } from '@tinymce/tinymce-angular'
import { NotificationService } from '@core/services/notification.service';
import { UpdateJobRequest } from '@core/model/update-job-request.interface';
import { CreateJobRequest } from '@core/model/create-job-request.interface';
import { catchError, Observable, take } from 'rxjs';
import { ApiError } from '@core/errors/api-error';
import { MatButton } from '@angular/material/button';
import { JobMetadata } from '@core/model/job-metadata.interface';
import { ErrorProcessorService } from '@core/services/error-processor.service';
import { CommentInputComponent } from '../inputs/comment-input.component';
import { SalaryInputComponent } from '../inputs/salary-input.component';
import { ProfileInputComponent } from '../inputs/profile-input.component';
import { DescriptionInputComponent } from '../inputs/description-input.component';
import { CompanyInputComponent } from '../inputs/company-input.component';
import { TitleInputComponent } from '../inputs/title-input.component';
import { UrlInputComponent } from '../inputs/url-input.component';
 
@Component({
  selector: 'app-job-form',
  imports: [ReactiveFormsModule, MatButton, CommentInputComponent, SalaryInputComponent, ProfileInputComponent, DescriptionInputComponent, CompanyInputComponent, TitleInputComponent, UrlInputComponent],
  providers: [
    { provide: TINYMCE_SCRIPT_SRC, useValue: 'tinymce/tinymce.min.js' }
  ],
  templateUrl: './job-form.component.html',
  styleUrl: './job-form.component.scss'
})
export class JobFormComponent implements OnInit {
  @Input() job: Job | null = null;
  @Input() jobMetadata: JobMetadata | null = null;
  @Output() jobSaved = new EventEmitter<Job>();
 
  protected valueSource: Job | JobMetadata | null = null;
  public loading = false;
  public error: string |null = null;
  public form: FormGroup | undefined;
 
  init: EditorComponent['init'] = {
    plugins: ['link', 'autolink', 'lists'],
    toolbar: 'undo redo | bold italic | link | bullist',
    promotion: false,
    menubar: 'null',
    statusbar: false
  };
 
  constructor(
    private readonly jobService: JobService, 
    private readonly notificationService: NotificationService, 
    private readonly fb: FormBuilder, 
    private readonly errorProcessorService: ErrorProcessorService
  ) {}
 
  ngOnInit(): void {
    this.initForm();
  }
 
  get url() {
    return this.form?.get('url');
  }
 
  get title() {
    return this.form?.get('title');
  }
 
  get company() {
    return this.form?.get('company');
  }
 
  get description() {
    return this.form?.get('description');
  }
 
  get profile() {
    return this.form?.get('profile');
  }
 
  get comment() {
    return this.form?.get('comment');
  }
 
  get salary() {
    return this.form?.get('salary');
  }
 
  updateRichText(event: any, field: string) :boolean {
    const control: AbstractControl<any>|null|undefined = this.form?.get(field);
    
    Iif(control !== null) {
      control?.setValue(event.editor.getContent());
    }
    return true;
  }
 
  submit() {
    this.error = null;
    this.loading = true;
    let observableResult: Observable<Job>;
    let term = $localize `:@@info.job.created:created`;
    if(this.job !== null && this.job.id !== null) {
      observableResult = this.jobService.updateJob(this.job.id, this.form?.value as UpdateJobRequest);
      term = $localize `:@@info.job.updated:updated`;
    }
    else {
      observableResult = this.jobService.createJob(this.form?.value as CreateJobRequest);
    }
 
    observableResult.pipe(
      take(1),
      catchError(
        (error: ApiError) => {
          this.loading = false;
          // set an explicit error message
          const errorMessage = $localize `:@@error.job.create_or_save:Job could not be ${term}.${error.message}`;
          error.message = errorMessage;
          this.error = errorMessage;
          return this.errorProcessorService.processError(error);
        }
      )
    )
    .subscribe((job) => {
      this.loading = false;
      this.notificationService.confirmation($localize `:@@job.created_or_saved:Job ${term} successfully`);
      this.jobSaved.emit(job);
    });
  }
 
  private initForm(): void {
    this.valueSource = this.job ? this.job : this.jobMetadata;
    this.form = this.fb.group({});
  }  
}