import { Component , OnDestroy , OnInit , Renderer2 } from '@angular/core' ; import { ConnectionService } from './connection.service' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { Router } from '@angular/router' ; @ Component ( { templateUrl : './connect.component.html' , styleUrls : [ './connect.component.scss' ] } ) export class ConnectComponent implements OnInit , OnDestroy { public connectionTest : boolean = false ; public connectionResponse ; public connectionForm : FormGroup ; private connectionValues ; private readonly defaultHost : string = '127.0.0.1' ; private readonly defaultPort : number = 4000 ; private readonly defaultProtocol : string = 'http' ; public constructor ( private domRender , private connService , private router ) { this . connectionForm = new FormGroup ( { host : new FormControl ( this . defaultHost , Validators . required ) , port : new FormControl ( this . defaultPort , Validators . required ) , protocol : new FormControl ( this . defaultProtocol ) } ) ; } public ngOnInit ( ) { this . domRender . addClass ( document . body , 'embedded' ) ; } public ngOnDestroy ( ) { this . domRender . removeClass ( document . body , 'embedded' ) ; } public async checkConnection ( ) { const formData = this . connectionForm . getRawValue ( ) ; try { this . connectionResponse = await this . connService . testCustomURL ( formData . protocol , formData . host , formData . port ) ; this . connectionTest = true ; this . connectionValues = formData ; } catch ( e ) { this . connectionTest = false ; } } public useConnection ( ) { this . connService . setConnectionURL ( this . connectionValues . protocol , this . connectionValues . host , this . connectionValues . port ) ; this . router . navigate ( [ '/' ] ) ; } }