All files / src/app/sys-check/questionnaire questionnaire.component.ts

56.52% Statements 13/23
14.28% Branches 1/7
44.44% Functions 4/9
59.09% Lines 13/22

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                  1x 1x 1x     1x   1x 1x       1x 1x       1x       1x 1x         1x 1x                                                  
import { FormControl, FormGroup } from '@angular/forms';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import { SysCheckDataService } from '../sys-check-data.service';
 
@Component({
  templateUrl: './questionnaire.component.html',
  styleUrls: ['./questionnaire.component.css', '../sys-check.component.css']
})
export class QuestionnaireComponent implements OnInit, OnDestroy {
  form: FormGroup = new FormGroup([]);
  private readonly valueChangesSubscription: Subscription | null = null;
 
  constructor(
    public dataservice: SysCheckDataService
  ) {
    const group: { [key: string] : FormControl } = {};
    this.dataservice.checkConfig.questions
      .forEach(question => {
        group[question.id] = new FormControl('');
      });
    this.form = new FormGroup(group);
    this.dataservice.questionnaireReports
      .forEach(reportEntry => {
        this.form.controls[reportEntry.id].setValue(reportEntry.value);
      });
    this.valueChangesSubscription = this.form.valueChanges.subscribe(() => { this.updateReport(); });
  }
 
  ngOnInit(): void {
    setTimeout(() => {
      this.dataservice.setNewCurrentStep('q');
    });
  }
 
  ngOnDestroy(): void {
    if (this.valueChangesSubscription !== null) {
      this.valueChangesSubscription.unsubscribe();
    }
  }
 
  private updateReport() {
    this.dataservice.questionnaireReports = [];
    Iif (this.dataservice.checkConfig) {
      this.dataservice.checkConfig.questions.forEach(element => {
        Iif (element.type !== 'header') {
          const formControl = this.form.controls[element.id];
          Iif (formControl) {
            this.dataservice.questionnaireReports.push({
              id: element.id,
              type: element.type,
              label: element.prompt,
              value: formControl.value,
              warning: (['string', 'select', 'radio', 'text']
                .indexOf(element.type) > -1) && (formControl.value === '') && (element.required)
            });
          }
        }
      });
    }
  }
}