15e1a61e0339e6068a6fabffe1d6e7e600bc6917
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / ric-selector / ric-selector.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   FormBuilder,
26   FormControl,
27   FormGroup,
28   FormGroupDirective,
29   Validators,
30 } from "@angular/forms";
31 import { EventEmitter } from "@angular/core";
32 import { Rics } from "src/app/interfaces/ric";
33 import { PolicyService } from "src/app/services/policy/policy.service";
34 import { MatSelectChange } from "@angular/material/select";
35
36 @Component({
37   selector: "nrcp-ric-selector",
38   templateUrl: "./ric-selector.component.html",
39   styleUrls: ["./ric-selector.component.scss"],
40   viewProviders: [
41     { provide: ControlContainer, useExisting: FormGroupDirective },
42   ],
43 })
44 export class RicSelectorComponent implements OnInit {
45   @Input() policyTypeName: string = "";
46   @Output() selectedRic: EventEmitter<string> = new EventEmitter<string>();
47
48   ric: string = null;
49   instanceForm: FormGroup = new FormGroup({
50     ricSelector: new FormControl(this.ric, [Validators.required]),
51   });
52   allRics: string[] = [];
53
54   constructor(
55     private dataService: PolicyService,
56     private formBuilder: FormBuilder
57   ) {}
58
59   ngOnInit(): void {
60     console.log("Ric:", this.ric);
61     this.fetchRics();
62   }
63
64   onRicChanged(newvalue: MatSelectChange): void {
65     this.selectedRic.emit(newvalue.value);
66   }
67
68   get ricSelector(): AbstractControl {
69     return this.instanceForm.get("ricSelector");
70   }
71
72   private fetchRics() {
73     console.log("fetchRics ", this.policyTypeName);
74     const self: RicSelectorComponent = this;
75     this.dataService.getRics(this.policyTypeName).subscribe({
76       next(value: Rics) {
77         value.rics.forEach((ric) => {
78           self.allRics.push(ric.ric_id);
79         });
80         console.log(value);
81       },
82     });
83   }
84 }