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