Project

General

Profile

1
import { VerificationRule } from 'src/app/shared/models/verification-rule.interface';
2
import { VerificationRulesService } from 'src/app/shared/services/administration/verification-rules.service';
3
import { VerificationRuleFormComponent } from './../../forms/verification-rule-form/verification-rule-form.component';
4
import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
5
import { NotificationsHandlingService } from 'src/app/shared/services/notifications-handling/notifications-handling.service';
6

    
7
@Component({
8
  selector: 'app-create-verification-rule-dialog',
9
  templateUrl: './create-verification-rule-dialog.component.html',
10
  styleUrls: ['./create-verification-rule-dialog.component.scss']
11
})
12
export class CreateVerificationRuleDialogComponent implements OnInit {
13

    
14
  @ViewChild(VerificationRuleFormComponent) verificationRuleForm: VerificationRuleFormComponent;
15

    
16
  @Input() displayDialog: boolean;
17
  @Input() header: string;
18
  @Input() verificationRuleToEdit: VerificationRule;
19
  @Output() cancelled = new EventEmitter<boolean>();
20
  @Output() valueChange = new EventEmitter<any>();
21

    
22
  displayValidationMessagesEvenIfPristine: boolean;
23

    
24
  constructor(private verificationRulesService: VerificationRulesService, private notificationService: NotificationsHandlingService) { }
25

    
26
  ngOnInit(): void {
27
    this.reset();   // Make sure everything is cleanly initialised.
28
  }
29

    
30
  reset() {
31
    this.displayValidationMessagesEvenIfPristine = false;
32
    if (this.verificationRuleForm) {
33
      this.verificationRuleForm.resetForm();
34
    }
35
  }
36

    
37
  initDisplay() {
38
    // First initialise the form for creation, and if a verificationRuleToEdit exists, it will overwrite any unwanted initialisations.
39
    this.verificationRuleForm.initForCreation();
40

    
41
    // If a verificationRuleToEdit was provided.
42
    if (this.verificationRuleToEdit) {
43
      this.verificationRuleForm.setValue(this.verificationRuleToEdit);
44
    }
45
  }
46

    
47
  cancel() {
48
    this.cancelled.emit(true);
49
  }
50

    
51
  addVerificationRule() {
52
    // If the form is not valid, make sure validator messages are displayed and then return without doing anything.
53
    if (!this.verificationRuleForm.isValid()) {
54
      this.displayValidationMessagesEvenIfPristine = true;
55
      return;
56
    }
57

    
58
    // TODO: What is returned from backend on create()??
59
    this.verificationRulesService.createNewVerificationRule(this.verificationRuleForm.formValue()).subscribe(result => {
60
      this.valueChange.emit(result);
61
      this.cancel();
62
      this.notificationService.showCreateNewVerificationRuleSuccess();
63
      
64
    },
65
      error => {
66
        
67
      });
68
  }
69

    
70
  editVerificationRule() {
71
    // If the form is not valid, make sure validator messages are displayed and then return without doing anything.
72
    if (!this.verificationRuleForm.isValid()) {
73
      this.displayValidationMessagesEvenIfPristine = true;
74
      return;
75
    }
76

    
77
    // TODO: What is returned from backend on create()??
78
    this.verificationRulesService.updateVerificationRule(this.verificationRuleForm.formValue()).subscribe(result => {
79
    this.valueChange.emit(result);
80
    this.cancel();
81
    this.notificationService.showUpdateVerificationRuleSuccess(); 
82
    },
83
      error => {
84
        
85
      });
86
  }
87
}
(4-4/4)