X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=webapp-frontend%2Fsrc%2Fapp%2Fpolicy%2Fpolicy-instance-dialog%2Fpolicy-instance-dialog.component.ts;h=dbc62a1ded649b60f80460a2875a64828ebfb968;hb=fb9a5699b247ad1518c5714224396205485a3c4c;hp=096346eb0689d3d837d3eef7dbb308ed36de25cb;hpb=042a087cf3eea5e6f941ee9add6e1c50cb106e91;p=portal%2Fnonrtric-controlpanel.git diff --git a/webapp-frontend/src/app/policy/policy-instance-dialog/policy-instance-dialog.component.ts b/webapp-frontend/src/app/policy/policy-instance-dialog/policy-instance-dialog.component.ts index 096346e..dbc62a1 100644 --- a/webapp-frontend/src/app/policy/policy-instance-dialog/policy-instance-dialog.component.ts +++ b/webapp-frontend/src/app/policy/policy-instance-dialog/policy-instance-dialog.component.ts @@ -23,31 +23,23 @@ import { Component, Inject, OnInit, - ViewChild, } from "@angular/core"; -import { FormGroup } from "@angular/forms"; import { MatDialogConfig, MatDialogRef, MAT_DIALOG_DATA, } from "@angular/material/dialog"; -import { PolicyService } from "../../services/policy/policy.service"; -import { NotificationService } from "../../services/ui/notification.service"; -import { UiService } from "../../services/ui/ui.service"; +import { PolicyService } from "@services/policy/policy.service"; +import { NotificationService } from "@services/ui/notification.service"; +import { UiService } from "@services/ui/ui.service"; import { HttpErrorResponse } from "@angular/common/http"; -import { ErrorDialogService } from "../../services/ui/error-dialog.service"; +import { ErrorDialogService } from "@services/ui/error-dialog.service"; import * as uuid from "uuid"; import { CreatePolicyInstance, PolicyInstance, PolicyTypeSchema, -} from "../../interfaces/policy.types"; -import { RicSelectorComponent } from "../ric-selector/ric-selector.component"; -import { - formatJsonString, - NoTypePolicyEditorComponent, -} from "../no-type-policy-editor/no-type-policy-editor.component"; -import { TypedPolicyEditorComponent } from "../typed-policy-editor/typed-policy-editor.component"; +} from "@interfaces/policy.types"; @Component({ selector: "nrcp-policy-instance-dialog", @@ -55,19 +47,10 @@ import { TypedPolicyEditorComponent } from "../typed-policy-editor/typed-policy- styleUrls: ["./policy-instance-dialog.component.scss"], }) export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit { - instanceForm: FormGroup; - @ViewChild(RicSelectorComponent) - ricSelector: RicSelectorComponent; - @ViewChild(NoTypePolicyEditorComponent) - noTypePolicyEditor: NoTypePolicyEditorComponent; - @ViewChild(TypedPolicyEditorComponent) - typedPolicyEditor: TypedPolicyEditorComponent; - policyInstanceId: string; // null if not yet created + policyInstance = {} as CreatePolicyInstance; policyJson: string; - policyTypeName: string; jsonSchemaObject: any; darkMode: boolean; - ric: string; allRicIds: string[] = []; constructor( @@ -79,19 +62,19 @@ export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit { @Inject(MAT_DIALOG_DATA) private data, private ui: UiService ) { - this.policyInstanceId = data.instanceId; - this.policyTypeName = data.name; + this.policyInstance.policy_id = data.instanceId; + this.policyInstance.policytype_id = data.name; + this.policyInstance.policy_data = data.instanceJson; this.policyJson = data.instanceJson; this.jsonSchemaObject = data.createSchema; - this.ric = data.ric; + this.policyInstance.ric_id = data.ric; + this.policyInstance.service_id = "controlpanel"; } ngOnInit() { this.ui.darkModeState.subscribe((isDark) => { this.darkMode = isDark; }); - this.instanceForm = new FormGroup({}); - this.formatNoTypePolicyJson(); } // Do not remove! Needed to avoid "Expression has changed after it was checked" warning @@ -99,34 +82,23 @@ export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit { this.cdr.detectChanges(); } - private formatNoTypePolicyJson() { - if (!this.typeHasSchema()) { - if (this.policyJson) { - this.policyJson = formatJsonString(this.policyJson); - } else { - this.policyJson = "{}"; - } - } + onSelectedRicChanged(newRic: string): void { + this.policyInstance.ric_id = newRic; + } + + onJsonChanged(newJson: string): void { + this.policyInstance.policy_data = newJson ? JSON.parse(newJson) : null; } onSubmit() { - if (this.policyInstanceId == null) { - this.policyInstanceId = uuid.v4(); + if (this.policyInstance.policy_id == null) { + this.policyInstance.policy_id = uuid.v4(); } const self: PolicyInstanceDialogComponent = this; - let policyData: string; - if (this.typeHasSchema()) { - policyData = this.typedPolicyEditor.prettyLiveFormData; - } else { - policyData = this.noTypePolicyEditor.policyJsonTextArea.value; - } - let createPolicyInstance: CreatePolicyInstance = this.createPolicyInstance( - policyData - ); - this.policySvc.putPolicy(createPolicyInstance).subscribe({ + this.policySvc.putPolicy(this.policyInstance).subscribe({ next(_) { self.notificationService.success( - "Policy without type:" + self.policyInstanceId + " submitted" + "Policy " + self.policyInstance.policy_id + " submitted" ); self.dialogRef.close(); }, @@ -138,30 +110,14 @@ export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit { } typeHasSchema(): boolean { - return this.jsonSchemaObject !== "{}"; + return this.jsonSchemaObject.description ? true : false; } isFormValid(): boolean { - let isValid: boolean = this.instanceForm.valid; - if (this.typeHasSchema()) { - isValid = - isValid && this.typedPolicyEditor - ? this.typedPolicyEditor.formIsValid - : false; - } - return isValid; - } - - private createPolicyInstance(policyJson: string): CreatePolicyInstance { - let createPolicyInstance = {} as CreatePolicyInstance; - createPolicyInstance.policy_data = JSON.parse(policyJson); - createPolicyInstance.policy_id = this.policyInstanceId; - createPolicyInstance.policytype_id = ""; - createPolicyInstance.ric_id = this.ricSelector - ? this.ricSelector.selectedRic - : this.ric; - createPolicyInstance.service_id = "controlpanel"; - return createPolicyInstance; + return ( + this.policyInstance.ric_id !== null && + this.policyInstance.policy_data !== null + ); } }