8c6159c1a09b924fa5b30c1d6eaccd23822d5cb0
[portal/ric-dashboard.git] / 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, OnDestroy, OnInit } from '@angular/core';
22 import { FormControl, FormGroup, Validators } from '@angular/forms';
23 import { MatDialogRef } from '@angular/material/dialog';
24 import { Subscription } from 'rxjs';
25 import { finalize } from 'rxjs/operators';
26 import { RicInstance } from '../../interfaces/dashboard.types';
27 import { InstanceSelectorService } from '../../services/instance-selector/instance-selector.service';
28 import { LoadingDialogService } from '../../services/ui/loading-dialog.service';
29
30 @Component({
31   selector: 'rd-instance-selector-dialog',
32   templateUrl: './instance-selector-dialog.component.html',
33 })
34 export class InstanceSelectorDialogComponent implements OnInit, OnDestroy  {
35
36   private instanceArray: RicInstance[];
37   private instanceForm: FormGroup;
38   private instanceChange: Subscription;
39
40   constructor(
41     private dialogRef: MatDialogRef<InstanceSelectorDialogComponent>,
42     private instanceSelectorService: InstanceSelectorService,
43     private loadingDialogService: LoadingDialogService) { }
44
45   ngOnInit() {
46     this.instanceForm = new FormGroup({
47       instance: new FormControl('', [Validators.required]),
48     })
49
50     this.loadingDialogService.startLoading('Loading RIC instances');
51     this.instanceSelectorService.getInstanceArray()
52       .pipe(
53         finalize(() => this.loadingDialogService.stopLoading())
54       )
55       .subscribe((instanceArray: RicInstance[]) => {
56         this.instanceArray = instanceArray;
57       })
58
59     this.instanceChange = this.instanceSelectorService.getSelectedInstance().subscribe((selectedInstance: RicInstance) => {
60       if (selectedInstance.key) {
61         this.instanceForm.setValue({ instance: selectedInstance })
62       }
63     });
64   }
65
66   ngOnDestroy() {
67     this.instanceChange.unsubscribe();
68   }
69
70   changeInstance(selectedInstance) {
71     this.instanceSelectorService.updateSelectedInstance(selectedInstance);
72     this.dialogRef.close(true);
73   }
74
75 }