dddd2105b152fef2efbf6cd2c4e3393f443fc5e9
[src/xds/xds-agent.git] / webapp / src / app / pages / config / config-xds / config-xds.component.ts
1 import { Component, OnInit } from '@angular/core';
2 import { Observable } from 'rxjs/Observable';
3
4 import { XDSConfigService } from '../../../@core-xds/services/xds-config.service';
5 import { IXDServerCfg } from '../../../@core-xds/services/xdsagent.service';
6 import { AlertService, IAlert } from '../../../@core-xds/services/alert.service';
7 import { NotificationsComponent } from '../../notifications/notifications.component';
8
9 // Import RxJs required methods
10 import 'rxjs/add/operator/map';
11 import 'rxjs/add/operator/catch';
12
13 @Component({
14   selector: 'xds-config-xds',
15   styleUrls: ['./config-xds.component.scss'],
16   templateUrl: './config-xds.component.html',
17 })
18 export class ConfigXdsComponent {
19
20   // TODO: cleanup agentStatus$: Observable<IAgentStatus>;
21   applying = false;
22   xdsServerUrl = '';
23   server: IXDServerCfg = { id: '', url: 'http://localhost:8000', connRetry: 10, connected: false };
24
25   configFormChanged = false;
26
27   constructor(
28     private XdsConfigSvr: XDSConfigService,
29     private alert: AlertService,
30   ) {
31     // FIXME support multiple servers
32     this._updateServerCfg(this.XdsConfigSvr.getCurServer());
33     this.XdsConfigSvr.onCurServer().subscribe(svr => this._updateServerCfg(svr));
34   }
35
36   private _updateServerCfg(svr: IXDServerCfg) {
37     if (!svr || svr.url === '') {
38       return;
39     }
40     this.xdsServerUrl = svr.url;
41     this.server = Object.assign({}, svr);
42   }
43
44   isApplyBtnEnable(): boolean {
45     return this.xdsServerUrl !== '' && (!this.server.connected || this.configFormChanged);
46   }
47
48   onSubmit() {
49     if (!this.configFormChanged && this.server.connected) {
50       return;
51     }
52     this.configFormChanged = false;
53     this.applying = true;
54     this.server.url = this.xdsServerUrl;
55     this.XdsConfigSvr.setCurServer(this.server)
56       .subscribe(cfg => {
57         this.alert.info('XDS Server successfully connected (' + cfg.url + ')');
58         this.server = Object.assign({}, cfg);
59         this.applying = false;
60       },
61       err => {
62         this.applying = false;
63         this.alert.error(err);
64       });
65   }
66
67 }
68