Add multi-layer RIC instance selector
[portal/ric-dashboard.git] / dashboard / webapp-frontend / src / app / ui / instance-selector-dialog / instance-selector-dialog.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 AT&T Intellectual Property
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, OnInit } from '@angular/core';
22 import { FormControl, FormGroup, Validators } from '@angular/forms';
23 import { MatDialogRef } from '@angular/material/dialog';
24 import { finalize } from 'rxjs/operators';
25 import { RicInstance, RicRegion } from '../../interfaces/dashboard.types';
26 import { InstanceSelectorService } from '../../services/instance-selector/instance-selector.service';
27 import { LoadingDialogService } from '../../services/ui/loading-dialog.service';
28
29 @Component({
30   selector: 'rd-instance-selector-dialog',
31   templateUrl: './instance-selector-dialog.component.html',
32 })
33 export class InstanceSelectorDialogComponent implements OnInit  {
34
35   private allRegions: RicRegion[];
36   private regionInstances: RicInstance[];
37   private instanceForm: FormGroup;
38
39   constructor(
40     private dialogRef: MatDialogRef<InstanceSelectorDialogComponent>,
41     private instanceSelectorService: InstanceSelectorService,
42     private loadingDialogService: LoadingDialogService) { }
43
44   ngOnInit() {
45     this.instanceForm = new FormGroup({
46       instance: new FormControl('', [Validators.required])
47     });
48
49     this.loadingDialogService.startLoading('Loading RIC instances');
50     this.instanceSelectorService.getAllInstances()
51       .pipe(
52         finalize(() => this.loadingDialogService.stopLoading())
53       )
54       .subscribe((regArray: RicRegion[]) => {
55         this.allRegions = regArray;
56       });
57   }
58
59   changeInstance(selectedInstance: RicInstance) {
60     this.instanceSelectorService.updateSelectedInstance(selectedInstance);
61     this.dialogRef.close(true);
62   }
63
64   changeRegion(selectedRegion: RicRegion) {
65     this.instanceForm.setValue({ instance: '' });
66     this.regionInstances = selectedRegion.instances;
67   }
68 }