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=56c3a119a6466cdf3e4bfbab55ab624a578169ef;hpb=800ab849929561945135745ba64ac563f816f333;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 56c3a11..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 @@ -17,140 +17,133 @@ * limitations under the License. * ========================LICENSE_END=================================== */ -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 { HttpErrorResponse } from '@angular/common/http'; -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'; +import { + AfterViewInit, + ChangeDetectorRef, + Component, + Inject, + OnInit, +} from "@angular/core"; +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 { HttpErrorResponse } from "@angular/common/http"; +import { ErrorDialogService } from "@services/ui/error-dialog.service"; +import * as uuid from "uuid"; +import { + CreatePolicyInstance, + PolicyInstance, + PolicyTypeSchema, +} from "@interfaces/policy.types"; @Component({ - selector: 'nrcp-policy-instance-dialog', - templateUrl: './policy-instance-dialog.component.html', - styleUrls: ['./policy-instance-dialog.component.scss'] + selector: "nrcp-policy-instance-dialog", + templateUrl: "./policy-instance-dialog.component.html", + styleUrls: ["./policy-instance-dialog.component.scss"], }) -export class PolicyInstanceDialogComponent implements OnInit { - instanceForm: FormGroup; - @ViewChild(RicSelectorComponent) - ricSelector: RicSelectorComponent; - @ViewChild(NoTypePolicyEditorComponent) - noTypePolicyEditor: NoTypePolicyEditorComponent; - @ViewChild(TypedPolicyEditorComponent) - typedPolicyEditor: TypedPolicyEditorComponent; - policyInstanceId: string; // null if not yet created +export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit { + policyInstance = {} as CreatePolicyInstance; policyJson: string; - policyTypeName: string; - jsonSchemaObject: any = {}; + jsonSchemaObject: any; darkMode: boolean; - ric: string; allRicIds: string[] = []; constructor( + private cdr: ChangeDetectorRef, public dialogRef: MatDialogRef, private policySvc: PolicyService, private errorService: ErrorDialogService, private notificationService: NotificationService, @Inject(MAT_DIALOG_DATA) private data, - private ui: UiService) { - this.policyInstanceId = data.instanceId; - this.policyTypeName = data.name ? data.name : '< No Type >'; + private ui: UiService + ) { + 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(); } - private formatNoTypePolicyJson() { - if (!this.typeHasSchema()) { - if (this.policyJson) { - this.policyJson = formatJsonString(this.policyJson); - } else { - this.policyJson = '{}'; - } - } + // Do not remove! Needed to avoid "Expression has changed after it was checked" warning + ngAfterViewInit() { + this.cdr.detectChanges(); + } + + 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( - { - next(_) { - self.notificationService.success('Policy without type:' + self.policyInstanceId + ' submitted'); - self.dialogRef.close(); - }, - error(error: HttpErrorResponse) { - self.errorService.displayError('Submit failed: ' + error.error); - }, - complete() { } - }); + this.policySvc.putPolicy(this.policyInstance).subscribe({ + next(_) { + self.notificationService.success( + "Policy " + self.policyInstance.policy_id + " submitted" + ); + self.dialogRef.close(); + }, + error(error: HttpErrorResponse) { + self.errorService.displayError("Submit failed: " + error.error); + }, + complete() {}, + }); } 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 + ); } } -export function getPolicyDialogProperties(policyTypeSchema: PolicyTypeSchema, instance: PolicyInstance, darkMode: boolean): MatDialogConfig { +export function getPolicyDialogProperties( + policyTypeSchema: PolicyTypeSchema, + instance: PolicyInstance, + darkMode: boolean +): MatDialogConfig { const createSchema = policyTypeSchema.schemaObject; const instanceId = instance ? instance.policy_id : null; const instanceJson = instance ? instance.policy_data : null; const name = policyTypeSchema.name; const ric = instance ? instance.ric_id : null; return { - maxWidth: '1200px', - maxHeight: '900px', - width: '900px', - role: 'dialog', - disableClose: false, - panelClass: darkMode ? 'dark-theme' : '', - data: { - createSchema, - instanceId, - instanceJson, - name, - ric - } + maxWidth: "1200px", + maxHeight: "900px", + width: "900px", + role: "dialog", + disableClose: false, + panelClass: darkMode ? "dark-theme" : "", + data: { + createSchema, + instanceId, + instanceJson, + name, + ric, + }, }; -} \ No newline at end of file +}