/** * @Component Name: LoginComponent * @Module: App * @Module Functionality : To display the login page * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, OnInit, OnDestroy } from '@angular/core'; import { Router, ActivatedRoute, Params } from '@angular/router'; // utils,constants declaration import * as Constants from '../shared/constants'; import UserUtils from '../shared/user.utils'; // service declaration import { SharedService } from '../shared/shared.service'; import { UserService } from '../services/user.service'; // modal declaration import { IWCToken, IChannel } from '../shared/modal/common'; import { Logon, User } from '../shared/modal/user'; import { Customer } from '../shared/modal/customer'; declare var $: any; @Component({ selector: 'login-cmp', moduleId: module.id, templateUrl: './login.component.html', styleUrls: ['./login.component.css'] }) export class LoginComponent implements OnInit, OnDestroy { private COMPONENT_NAME: string = 'LoginComponent'; karmaTestResult: any = {}; loginModel: any = { username: '', password: '' }; errorMessage: string; storeId: number = 10553; user: User; customer: Customer; wcToken: IWCToken; channels: IChannel[]; showLoginForm: boolean = true; constructor( private route: ActivatedRoute, public router: Router, public sharedService: SharedService, public userService: UserService) { } /** * This function is used to initialize the directive/component after Angular * first displays the data-bound properties.It is invoked only once when the * directive is instantiated. * @return {void} */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); // reset login status console.log('LoginComponent.ngOnInit()>>Clearing the master list.'); this.sharedService.setAppDefault(); if(!sessionStorage.getItem('toggleKey')){ this.sharedService.clearSessionStorage(true); } this.sharedService.clearLocalStorage(); this.updateCarousel(null); } /** * Cleanup just before Angular destroys the directive/component. * Unsubscribe Observables and detach event handlers to avoid memory leaks. * @return {void} */ ngOnDestroy() { const METHOD_NAME: string = 'ngOnDestroy()'; console.log('LoginComponent.ngOnDestroy()>>Clearing local objects.'); this.user = null; this.wcToken = null; this.channels = null; this.customer = null; this.loginModel = null; this.karmaTestResult = null; } /** * This function is used to authorize the agent's login information * after communicating with the login function in user service component * @return void */ login(username:string, password:string): void { const METHOD_NAME: string = 'login()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Agent logged in'); console.log('LoginComponent.login()>>Start.'); this.errorMessage = ''; let inputData: any = {logonId:username.toLowerCase(), logonPassword:password}; this.sharedService.showSpinner(true); this.userService.login(this.storeId, inputData).subscribe ( data => { this.karmaTestResult = data; this.sharedService.showSpinner(false); console.log('LoginComponent.login()>>login success..'); let wcToken:IWCToken = Object.assign({}, data); wcToken.logonId = username; wcToken.password = password; this.profile( wcToken ); }, error => { this.sharedService.showSpinner(false); this.errorMessage = this.sharedService.getErrorMessage(error); console.error('error=>' + error); }); console.log('LoginComponent.login()>>End.'); } isAlphaNumericOnly(e) { let regex = new RegExp("^[a-zA-Z0-9_]*$"); let str = String.fromCharCode(!e.charCode ? e.which : e.charCode); if (regex.test(str)) { return true; } e.preventDefault(); return false; } /** * This function is used to get the agent's profile which is part of * login functionality, and checking the user permission (for channels like CD,BD,CS) * based on agent's roles through getProfile function user service component. * @param {number} userId - Contains the user id * @return void */ profile(wcToken: IWCToken): void { const METHOD_NAME: string = 'profile()'; console.log('LoginComponent.profile()>>Start.'); this.errorMessage = ''; this.sharedService.showSpinner(true); this.userService.getProfile(this.storeId, wcToken.userId).subscribe( data => { this.karmaTestResult = data; this.sharedService.showSpinner(false); // @Hema -LoginPoc console.log('LoginComponent.login()>>User prfile success..'); let user:User = Object.assign(wcToken, data); UserUtils.updateFavorites(user); UserUtils.setPermissionsForAgent(user); if (user.channels && user.channels.length > 0) { if(UserUtils.hasAccessStore(this.storeId, user)) { UserUtils.activeSelectedStore(this.storeId, user); this.userService.setUser(user); this.router.navigate(['home/search']); }else{ let accStroreId = UserUtils.getAccessStore(user); if(accStroreId && accStroreId > 0) { this.sharedService.updateStoreId(accStroreId); this.login(user.logonId, user.password); //reloging in } else { this.errorMessage = this.sharedService.getErrorMessage('_ERR_CHANNEL_ACCESS_DENIED'); } } } else { this.errorMessage = this.sharedService.getErrorMessage('_ERR_CHANNEL_ACCESS_DENIED'); } }, error => { this.sharedService.showSpinner(false); this.errorMessage = this.sharedService.getErrorMessage(error); console.error('error=>' + error); }); console.log('LoginComponent.profile()>>End.'); } /** * This function is used to check the window size and return values on window resize. * @param {} event * @returns void */ updateCarousel(event): void { const METHOD_NAME: string = 'updateCarousel()'; $('.carousel-inner .item.background').height($(window).height()); } }