aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/components/genericForm/generic-form.component.ts
blob: 6febd66d66770453b7f715f112284c79f92e5df1 (plain)
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
import {Component, EventEmitter, Input, OnChanges, Output, SimpleChanges} from '@angular/core';
import {GenericFormService} from "./generic-form.service";
import {FormControlModel} from "../../models/formControlModels/formControl.model";
import {FormGroup} from "@angular/forms";
import * as _ from 'lodash';

@Component({
  selector : 'generic-form',
  templateUrl : './generic-form.component.html',
  styleUrls : ['./generic-form.component.scss']
})

export class GenericFormComponent implements OnChanges{
  genericFormService: GenericFormService = null;
  dynamicFormGroup: FormGroup = null;

  @Input() formControls : FormControlModel[] = null;
  @Input() dynamicInputs : FormControlModel[] = null;
  @Input() isValidForm : boolean = false;
  @Output() onFormChanged  = new EventEmitter();

  constructor(private _genericFormService: GenericFormService){
    this.genericFormService = _genericFormService;
  }

  ngOnChanges(changes: SimpleChanges): void {
    if (changes["formControls"] !== undefined && changes["formControls"].currentValue !== changes["formControls"].previousValue) {
      this.dynamicFormGroup = this._genericFormService.generateFormBuilder(this.formControls, this.dynamicInputs);
      this.onFormChanged.next(this.dynamicFormGroup);
      this.dynamicFormGroup.valueChanges.subscribe(() => {
        this.onFormChanged.next(this.dynamicFormGroup);
      })
    }
  }

  hasApiError(controlName: string, data: any[], form: FormGroup) {
    if (!_.isNil(data)) {
      if (!form.controls[controlName].disabled && data.length === 0) {
        return true;
      }
    }
    return false;
  }
}