Limit number of local servers to one, Fixes: #322

This commit is contained in:
ziajka 2019-03-05 07:14:21 +01:00
parent 23819b48c0
commit 7ccc9db33b
2 changed files with 19 additions and 7 deletions

View File

@ -3,6 +3,7 @@ import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { Server } from '../../../models/server';
import { ElectronService } from 'ngx-electron';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { ServerService } from '../../../services/server.service';
@Component({
@ -27,25 +28,35 @@ export class AddServerDialogComponent implements OnInit {
constructor(
public dialogRef: MatDialogRef<AddServerDialogComponent>,
private electronService: ElectronService,
private serverService: ServerService,
@Inject(MAT_DIALOG_DATA) public data: any,
) {}
getLocations() {
async getLocations() {
const localServers = await this.numberOfLocalServers();
let locations = [];
if(this.electronService.isElectronApp) {
if(this.electronService.isElectronApp && localServers === 0) {
locations.push({ key: 'local', name: 'Local' });
}
locations.push({ key: 'remote', name: 'Remote' });
return locations
}
getDefaultLocation() {
if(this.electronService.isElectronApp) {
async getDefaultLocation() {
const localServers = await this.numberOfLocalServers();
if(this.electronService.isElectronApp && localServers === 0) {
return 'local';
}
return 'remote';
}
async numberOfLocalServers() {
const servers = await this.serverService.findAll();
return servers.filter((server) => server.location === 'local').length;
}
getDefaultHost() {
return '127.0.0.1';
}
@ -62,7 +73,7 @@ export class AddServerDialogComponent implements OnInit {
}
async ngOnInit() {
this.locations = this.getLocations();
this.locations = await this.getLocations();
const defaultLocalServerPath = await this.getDefaultLocalServerPath();
@ -105,7 +116,8 @@ export class AddServerDialogComponent implements OnInit {
})
});
this.serverForm.get('location').setValue(this.getDefaultLocation());
const defaultLocation = await this.getDefaultLocation();
this.serverForm.get('location').setValue(defaultLocation);
this.serverForm.get('host').setValue(this.getDefaultHost());
this.serverForm.get('port').setValue(this.getDefaultPort());
this.serverForm.get('authorization').setValue('none');

View File

@ -48,7 +48,7 @@ export class ServerService {
}
public findAll() {
return this.onReady(() => this.indexedDbService.get().getAll(this.tablename));
return this.onReady(() => this.indexedDbService.get().getAll(this.tablename)) as Promise<Server[]>;
}
public delete(server: Server) {