Fix bug in NoTypePolicyEditorComponent
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / no-type-policy-editor / no-type-policy-editor.component.ts
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //  /
20
21 import { Component, Input, OnInit, Output } from "@angular/core";
22 import {
23   AbstractControl,
24   ControlContainer,
25   FormControl,
26   FormGroup,
27   FormGroupDirective,
28   ValidatorFn,
29   Validators,
30 } from "@angular/forms";
31 import { EventEmitter } from "@angular/core";
32
33 @Component({
34   selector: "nrcp-no-type-policy-editor",
35   templateUrl: "./no-type-policy-editor.component.html",
36   styleUrls: ["./no-type-policy-editor.component.scss"],
37   viewProviders: [
38     { provide: ControlContainer, useExisting: FormGroupDirective },
39   ],
40 })
41 export class NoTypePolicyEditorComponent implements OnInit {
42   @Input() policyJson: string = null;
43   @Output() validJson: EventEmitter<string> = new EventEmitter<string>();
44
45   instanceForm: FormGroup = new FormGroup({});
46
47   constructor() {}
48
49   ngOnInit(): void {
50     let initialJson: string;
51     if (this.policyJson) {
52       initialJson = formatJsonString(this.policyJson);
53     } else {
54       initialJson = "{}";
55     }
56     this.instanceForm.addControl(
57       "policyJsonTextArea",
58       new FormControl(initialJson, [
59         Validators.required,
60         this.jsonValidator(),
61       ])
62     );
63   }
64
65   get policyJsonTextArea(): AbstractControl {
66     return this.instanceForm
67       ? this.instanceForm.get("policyJsonTextArea")
68       : null;
69   }
70
71   formatJsonInput(): void {
72     let jsonBefore: string = this.policyJsonTextArea.value;
73     let jsonAfter = formatJsonString(JSON.parse(jsonBefore));
74     this.policyJsonTextArea.setValue(jsonAfter);
75   }
76
77   jsonValidator(): ValidatorFn {
78     return (control: AbstractControl): { [key: string]: any } | null => {
79       const notValid = !this.isJsonValid(control.value);
80       this.handleJsonChangeEvent(notValid, control.value);
81       return notValid ? { invalidJson: { value: control.value } } : null;
82     };
83   }
84
85   handleJsonChangeEvent(notValid: boolean, newValue: string): void {
86     let json = newValue;
87     if (notValid) {
88       json = null;
89     }
90     this.validJson.emit(json);
91   }
92
93   isJsonValid(json: string): boolean {
94     let valid = false as boolean;
95     try {
96       if (json != null) {
97         JSON.parse(json);
98         valid = true;
99       }
100     } catch (jsonError) {}
101     return valid;
102   }
103 }
104
105 export function formatJsonString(jsonToFormat: any): string {
106   return JSON.stringify(jsonToFormat, null, 2);
107 }