import { Compiler, Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
import { config } from '../../../dinazor.config';
import { AuthUser } from '../../../model/auth-user';
import { DnNotificationService } from '../../../services/notification.service';
import { DnStorageService } from '../../../services/storage.service';
import { DnAuthService } from '../auth.service';
import { DnLoginConfigService } from './login-config.service';
@Component({
selector: 'dn-login',
template: `
`
})
export class DnLoginComponent implements OnInit {
public loading = false;
public loadingText: string;
public loginForm: FormGroup;
public submitted: boolean;
_loginConfigService: DnLoginConfigService;
// @Output() loginSuccesEvent: EventEmitter = new EventEmitter();
constructor(private router: Router,
private authService: DnAuthService,
private _fb: FormBuilder,
private notificationService: DnNotificationService,
private loginConfigService: DnLoginConfigService,
private storageService: DnStorageService,
private _compiler: Compiler) {
this._loginConfigService = loginConfigService;
}
checkLoginStatus(loginSubs: Subscription) {
if (loginSubs.closed) {
this.loading = false;
} else {
setTimeout(() => this.checkLoginStatus(loginSubs), 1000);
}
}
login(data: AuthUser, isValid: boolean) {
this.loading = true;
if (!isValid) return;
this.submitted = true;
this.loadingText = 'Giriş Yapılıyor';
const loginSubs = this.authService.login(data.mail, data.password).subscribe(res => {
this.loadingText = '';
if (res.isSuccess) {
const user: AuthUser = res.data as AuthUser;
this._compiler.clearCache();
this.setLocalStorage(res.data);
if (this._loginConfigService.loginConfig.afterLoginSuccessEvent)
this._loginConfigService.loginConfig.afterLoginSuccessEvent(user);
if (this.router.routerState.snapshot.root.queryParams && this.router.routerState.snapshot.root
.queryParams['returnUrl']) {
this.router.navigate([this.router.routerState.snapshot.root.queryParams['returnUrl']]);
} else {
this.router.navigate([this._loginConfigService.loginConfig.afterLoginNavigateRoute]);
}
if (this._loginConfigService.loginConfig.afterNavigateEvent)
setTimeout(() => this._loginConfigService.loginConfig.afterNavigateEvent(user), 10);
// this.loginSuccesEvent.emit(res.data);
// this.initializeLoginConfigs(res.data);
} else {
this.notificationService.showDinazorResultMessage(res);
}
this.loading = false;
});
setTimeout(() => this.checkLoginStatus(loginSubs), 500);
}
ngOnInit() {
/* if (config.debugState) {
this.loginForm = this._fb.group({
mail: ['gtb@gtb.gov.tr', [Validators.required as any, Validators.minLength(3) as any]],
password: ['gtb', [Validators.required as any, Validators.minLength(1)as any]]
});
} else {
this.loginForm = this._fb.group({
mail: ['', [Validators.required as any, Validators.minLength(3) as any]],
password: ['', [Validators.required as any, Validators.minLength(1)as any]]
});
}
*/
this.loginForm = this._fb.group({
mail: ['', [Validators.required as any, Validators.minLength(3) as any]],
password: ['', [Validators.required as any, Validators.minLength(1)as any]]
});
}
/* private initializeLoginConfigs(data: any) {
this.setLocalStorage(data);
// this.initCacheableValues(data.token);
}*/
private setLocalStorage(data: any) {
this.storageService.removeItem(config.DINAZOR_USER_KEY);
this.storageService.setItem(config.DINAZOR_USER_KEY, data);
/* this.dnHttpService.setUrl('Gtbuser/' + data.id + '/User');
this.dnHttpService.get('Gtbuser/' + data.id + '/User').subscribe(res => {
if (res.isSuccess) {
this.storageService.setItem(gtbConst.gtbUserKey, res.data);
}
});*/
}
/* private initCacheableValues(token: string) {
this.cacheHttp.removeAll();
this.cacheHttp.addCacheUrl('limanKod')
.addCacheUrl('havalimani/havalimani')
.addCacheUrl('firma')
.addCacheUrl('gumrukidaresi')
.addCacheUrl('ulke')
.addCacheUrl('rejimkod')
.addCacheUrl('beyanturu')
.addCacheUrl('doviz')
.addCacheUrl('tasimasekli')
.addCacheUrl('odemesekli')
.addCacheUrl('tasimaaraci')
.addCacheUrl('antrepokod')
.addCacheUrl('alicisaticiiliskisi')
.addCacheUrl('konteynertip')
.addCacheUrl('basitlestirilmisusulkod')
.addCacheUrl('teslimsekli')
.addCacheUrl('islemniteligi')
.addCacheUrl('bankakod')
.addCacheUrl('dil')
.addCacheUrl('nctsulkekod')
.loadAllUrls(token);
}*/
}