Skip to content
Snippets Groups Projects
jobspec.component.ts 2.31 KiB
Newer Older
import {Component, Input, OnDestroy, OnInit} from '@angular/core';
import {Job, JobSpec} from "../../model/job";
import {Subscription} from "rxjs";
import {JobsService} from "../../services/jobs.service";
import {verticleSlide} from "../../animations";
import {AlertService} from "../../services/alert.service";
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
import {faCaretDown, faCaretUp} from "@fortawesome/free-solid-svg-icons";

@Component({
  selector: 'app-jobspec',
  providers: [Location, {provide: LocationStrategy, useClass: PathLocationStrategy}],
  templateUrl: './jobspec.component.html',
  styleUrls: ['./jobspec.component.scss'],
  animations: [verticleSlide]
})
export class JobspecComponent implements OnInit, OnDestroy {

  @Input() job: Job;

  detailsExposed: boolean = false;
  public jobspec$: Subscription;
  public jobspec: JobSpec;

  public faCaretDown = faCaretDown;
  public faCaretUp = faCaretUp;

  constructor(private jobService: JobsService, private alertService: AlertService, private location: Location) {
  }

  ngOnInit() {
  }

  /**
   * is this component being shown in its root tab, or included into another tab for details
   * this is used to change the UI and click behaviors in the template
   */
  isHomePath(): boolean {
    return !!this.location.path(false).match(/jobs/);
  }

  toggleDetails(): void {
    if (this.detailsExposed) {
      this.detailsExposed = false;
    } else {
      this.showDetails();
    }
  }

  showDetails(): void {
    this.detailsExposed = true;
    this.jobspec$ = this.jobService.getJobSpec(this.job.jobspec_id).subscribe((js: JobSpec) => {
      if (js) {
        this.jobspec = js;
      } else {
        this.alertService.error('Job Spec could not be retrieved');
      }
    });
  }

  getJobStatusClass(status: string) {
    switch (status) {
      case 'ERROR':
        return 'badge-danger';
      case 'QA_REJECTED':
        return 'badge-secondary';
      case 'PROCESSING':
        return 'badge-primary';
      case 'QA_READY':
        return 'badge-success';
      case 'QA_ACCEPTED':
        return 'badge-info';
      case 'QA_MANUAL':
        return 'badge-warning';
      default:
        return 'border border-info text-info';
    }
  }

  ngOnDestroy(): void {
    if (this.jobspec$) {
      this.jobspec$.unsubscribe();
    }
  }

}