import { Component, Inject, OnInit } from '@angular/core'; import { DataSource } from '@angular/cdk/collections'; import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material'; import { Observable, merge } from 'rxjs'; import { map } from 'rxjs/operators'; import { Server } from '../../models/server'; import { ServerService } from '../../services/server.service'; import { ServerDatabase } from '../../services/server.database'; import { ElectronService } from 'ngx-electron'; @Component({ selector: 'app-server-list', templateUrl: './servers.component.html', styleUrls: ['./servers.component.css'] }) export class ServersComponent implements OnInit { dataSource: ServerDataSource; displayedColumns = ['id', 'name', 'location', 'ip', 'port', 'actions']; constructor( private dialog: MatDialog, private serverService: ServerService, private serverDatabase: ServerDatabase ) {} ngOnInit() { this.serverService.findAll().then((servers: Server[]) => { this.serverDatabase.addServers(servers); }); this.dataSource = new ServerDataSource(this.serverDatabase); } createModal() { const dialogRef = this.dialog.open(AddServerDialogComponent, { width: '350px' }); dialogRef.afterClosed().subscribe(server => { if (server) { this.serverService.create(server).then((created: Server) => { this.serverDatabase.addServer(created); }); } }); } deleteServer(server: Server) { this.serverService.delete(server).then(() => { this.serverDatabase.remove(server); }); } } @Component({ selector: 'app-add-server-dialog', templateUrl: 'add-server-dialog.html' }) export class AddServerDialogComponent implements OnInit { server: Server = new Server(); authorizations = [{ key: 'none', name: 'No authorization' }, { key: 'basic', name: 'Basic authorization' }]; locations = []; constructor( public dialogRef: MatDialogRef, private electronService: ElectronService, @Inject(MAT_DIALOG_DATA) public data: any, ) {} getLocations() { let locations = []; if(this.electronService.isElectronApp) { locations.push({ key: 'local', name: 'Local' }); } locations.push({ key: 'remote', name: 'Remote' }); return locations } getDefaultLocation() { if(this.electronService.isElectronApp) { return 'local'; } return 'remote'; } ngOnInit() { this.locations = this.getLocations(); this.server.authorization = 'none'; this.server.location = this.getDefaultLocation(); } onAddClick(): void { this.dialogRef.close(this.server); } onNoClick(): void { this.dialogRef.close(); } } export class ServerDataSource extends DataSource { constructor(private serverDatabase: ServerDatabase) { super(); } connect(): Observable { return merge(this.serverDatabase.dataChange).pipe( map(() => { return this.serverDatabase.data; }) ); } disconnect() {} }