import { Component, OnDestroy, OnInit } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; import { User } from '../../_models/user.class'; import { LoginService } from './login.service'; import { Subscription } from 'rxjs/Subscription'; import { CookieService } from '../../_services/cookie.service'; import { AuthenticationService } from '../../_services/authentication.service'; import { LocalService } from '../../_services/local.service'; @Component({ selector: 'app-dashboard', templateUrl: 'login.component.html' }) export class LoginComponent implements OnInit, OnDestroy { public user: any = {}; public showError: boolean = false; public showUpdateError: boolean = false; public errorString: string = ''; public loadingClass: string = 'register-hr'; public returnUrl: string = ''; public subscription: Subscription; public loginladda = false; constructor(private route: ActivatedRoute, private router: Router, private _loginService: LoginService, private _authenticationService: AuthenticationService, private _localService: LocalService, private _cookieService: CookieService) { let element = document.getElementsByTagName(`body`)[0]; element.classList.remove(`sidebar-lg-show`); } ngOnInit() { this.subscription = this.route.queryParams.subscribe( (params: any) => { if (params.hasOwnProperty('returnUrl')) { this.returnUrl = params['returnUrl']; } } ); if (this._cookieService.getCookie('ussr')) { this.router.navigate(['/dashboard']); } } public login() { this.loading(true); this._loginService.login({ username: this.user.username, password: this.user.password }).subscribe((response: any) => { if (this._authenticationService.setUser(response)) { this.loading(false); this.router.navigate([`/${this.returnUrl}`]); } }, (err) => { if (err.error && err.error.error_description && err.error.error_description == 'NA') { this.showUpdateError = true; } else { this.showUpdateError = false; this.showError = true; } this.loading(false); } ); } //Loading Helper Function public loading(start: boolean) { if (start) { this.loadingClass = 'register-hr-anime'; } else { this.loadingClass = 'register-hr'; } this.loginladda = true; } ngOnDestroy(): void { if (this.subscription) { this.subscription.unsubscribe(); } } }