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