import {Component, OnInit, Input} from '@angular/core';
import {FormGroup, FormBuilder, Validators} from "@angular/forms";

@Component({
    selector: 'community-form',
    templateUrl: './community-form.component.html',
})

export class CommunityFormComponent implements OnInit{

    @Input('group')
    myForm: FormGroup;

    public errorMessage: string;

    constructor(public _fb: FormBuilder) {}

    ngOnInit(): void {}

    public get form() {
      return this._fb.group({
        _id: '',
        pid: ['', Validators.required],
        name: ['', Validators.required],
        type: ['', Validators.required]
      });
    }

    public reset() {
      this.myForm.patchValue({
        name : '',
        _id : '',
        pid: '',
        type: ''
      });
    }

    handleError(message: string, error) {
      if(error == null) {
        this.reset();
      }
      this.errorMessage = message + ' (Server responded: ' + error + ')';
    }
}
