import { AfterViewInit , Component , ElementRef , ViewChild } from "@angular/core" ; import { SessionService } from "../session-service/session.service" ; import { LoginState } from "../session-states/login-state.enum" ; import { ActivatedRoute , Router } from "@angular/router" ; import { LoggingService } from "../../logging/logging.service" ; /**
 * Form to allow users to enter their credentials and log in.
 */ @ Component ( { selector : "app-login" , templateUrl : "./login.component.html" , styleUrls : [ "./login.component.scss" ] , } ) export class LoginComponent implements AfterViewInit { /** true while a login is started but result is not received yet */ loginInProgress = false ; /** username as entered in form */ username : string ; /** password as entered in form */ password : string ; /** errorMessage displayed in form */ errorMessage : string ; @ ViewChild ( "usernameInput" ) usernameInput : ElementRef ; constructor ( private _sessionService , private loggingService , private router , private route ) { } ngAfterViewInit ( ) { setTimeout ( ( ) => this . usernameInput ?. nativeElement . focus ( ) ) ; } /**
     * Do a login with the SessionService.
     */ login ( ) { this . loginInProgress = true ; this . errorMessage = "" ; this . _sessionService . login ( this . username , this . password ) . then ( ( loginState ) => { switch ( loginState ) { case LoginState . LOGGED_IN : this . onLoginSuccess ( ) ; break ; case LoginState . UNAVAILABLE : this . onLoginFailure ( $localize `:LoginError:Please connect to the internet and try again` ) ; break ; case LoginState . LOGIN_FAILED : this . onLoginFailure ( $localize `:LoginError:Username and/or password incorrect` ) ; break ; default : throw new Error ( `Unexpected login state: ${ loginState }` ) ; } } ) . catch ( ( reason ) => { this . loggingService . error ( `Unexpected login error: ${ reason }` ) ; this . onLoginFailure ( $localize `:LoginError:An unexpected error occurred.
          Please reload the the page and try again.
          If you keep seeing this error message, please contact your system administrator.
        ` ) ; } ) ; } private onLoginSuccess ( ) { this . router . navigate ( [ ] , { relativeTo : this . route , } ) ; this . reset ( ) ; } private onLoginFailure ( reason ) { this . reset ( ) ; this . errorMessage = reason ; } private reset ( ) { this . errorMessage = "" ; this . password = "" ; this . loginInProgress = false ; } }