mycontrolpanel/src/app/app.component.ts
2019-08-18 18:41:31 +02:00

287 lines
8.6 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { MenuItem, SelectItem } from 'primeng/api';
import { AppService } from './_services/app.service';
import { TranslateService } from '@ngx-translate/core';
import { AuthenticationService } from './_services/authentication.service';
import { Router } from '@angular/router';
import { first } from 'rxjs/operators';
import { UserInfo } from './_api/models';
import { UserService } from './_api/services';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'My Control Panel';
ItemsUserDropDown: MenuItem[];
public ConnectedUser: UserInfo;
public Connected = false;
public DisplayLoginModal = false;
public DisplaySignInModal = false;
public DisplayPersonalField = false;
// Login
public Email: string = null;
public Password: string = null;
public ErrorTextLogin = '';
public CreateUser: UserInfo = {};
public ConfirmPassword: string;
public UserTryingToLogin: UserInfo = {};
// Sign in
public ErrorTextSignIn = '';
// Form Label
public LabelFormLastName: string;
public LabelFormFirstName: string;
public LabelFormEmail: string;
public LabelFormPassword: string;
public LabelFormConfirmPassword: string;
public LabelFormCreate: string;
// Labels
// Headers
public LabelHeaderLogIn: string;
public LabelHeaderSignIn: string;
public LabelHeaderAbout: string;
public LabelHeaderTeam: string;
public LabelHeaderContact: string;
// LogIn
public LabelLoginIdentify: string;
// SignIn
public LabelSignInCreateProfile: string;
// DropDown
public LabelDropDownLogout: string;
public LabelDropDownModifyUser: string;
constructor(
private _appService: AppService,
private _translateService: TranslateService,
private _authService: AuthenticationService,
private _userService: UserService,
private _router: Router
) {}
ngOnInit() {
const currentUser = this._authService.currentUserValue;
console.log('currentUser', currentUser);
if (currentUser && currentUser.token) {
this.ConnectedUser = currentUser;
this.Connected = true;
this.DisplayLoginModal = false;
this.Email = null;
this.Password = null;
this._authService.IsLoggedIn = true;
console.log(this.ConnectedUser.language);
if (this.ConnectedUser.language === null) {
this._translateService.setDefaultLang('en');
} else {
if (this.ConnectedUser.language === 'gb') {
this._translateService.setDefaultLang('en');
} else {
this._translateService.setDefaultLang(this.ConnectedUser.language);
}
}
} else {
this._translateService.setDefaultLang('en');
}
this.InitLabels();
this._appService.OpenSignInModal.subscribe(
value => {
if (value) {
this.DisplaySignInModal = true;
}
}
);
this._translateService.get('App.Home.UserDropDown.ModifyUser').subscribe(modifyLabel => {
this._translateService.get('App.Home.UserDropDown.Logout').subscribe(logoutLabel => {
this.ItemsUserDropDown = [
{label: modifyLabel, icon: 'fa fa-edit', command: () => {
this._router.navigate(['/profile/' + this.ConnectedUser.id + '/edit']);
}},
{label: logoutLabel, icon: 'fa fa-sign-out', command: () => {
this.Logout();
}}
];
});
});
}
public LogIn(email, password) {
this.UserTryingToLogin.email = email;
this.UserTryingToLogin.password = password;
this._authService.Login(this.UserTryingToLogin)
.pipe(first())
.subscribe(
data => {
console.log(data);
this.ConnectedUser = data;
this.Connected = true;
this.DisplayLoginModal = false;
this.Email = null;
this.Password = null;
this._authService.IsLoggedIn = true;
this._router.navigate(['/profile/' + this.ConnectedUser.id]);
this.ErrorTextLogin = '';
},
error => {
this._translateService.get('App.Home.Login.ErrorText').subscribe(loginErrorText => {
this.ErrorTextLogin = loginErrorText;
});
});
}
public Logout() {
this._authService.Logout();
this.Connected = false;
this.Email = null;
this.Password = null;
this._authService.IsLoggedIn = false;
this._router.navigate(['/home']);
}
public SignUp() {
this.DisplaySignInModal = true;
}
public GoHome() {
this._router.navigate(['/home']);
}
public GoToProfile() {
this._router.navigate(['/profile/' + this.ConnectedUser.id]);
}
public GoToConfiguration() {
this._router.navigate(['/profile/' + this.ConnectedUser.id + '/configurations']);
}
public GoToDevices() {
this._router.navigate(['/profile/' + this.ConnectedUser.id + '/devices']);
}
public CreateProfile() {
if (this.CorrectForm()) {
this.CreateUser.deviceIds = [];
this.CreateUser.language = 'gb';
this._userService.CreateUser(this.CreateUser)
.subscribe(
data => {
// TO CHANGE
this.ErrorTextSignIn = '';
this.UserTryingToLogin.email = data.email;
this.UserTryingToLogin.password = data.password;
this.DisplaySignInModal = false;
/*this.ConnectedUser = data;
this.Connected = true;*/
this.LogIn(data.email, data.password);
},
error => {
if (error !== null && error === 'Conflict') {
this._translateService.get('App.Home.SignIn.ErrorAlreadyExist').subscribe(text => {
this.ErrorTextSignIn = text;
});
} else {
this._translateService.get('App.Home.SignIn.ErrorUnknown').subscribe(text => {
this.ErrorTextSignIn = text;
});
}
});
}
}
public CorrectForm(): boolean {
if (this.CreateUser.firstName == null || this.CreateUser.lastName == null ||
this.CreateUser.email == null || this.CreateUser.password == null) {
this._translateService.get('App.Home.SignIn.ErrorFieldsMissing').subscribe(text => {
this.ErrorTextSignIn = text;
});
return false;
}
if (this.ConfirmPassword !== this.CreateUser.password) {
this._translateService.get('App.Home.SignIn.ErrorConfirmPassword').subscribe(text => {
this.ErrorTextSignIn = text;
});
return false;
}
this.ErrorTextSignIn = '';
return true;
}
public ResetLogInModal() {
this.ErrorTextLogin = '';
this.Email = '';
this.Password = '';
}
public ResetSignInModal() {
this.ErrorTextSignIn = '';
this.CreateUser = {};
this.ConfirmPassword = '';
}
public InitLabels() {
// HEADERS
this._translateService.get('App.Home.Login.Header').subscribe(label => {
this.LabelHeaderLogIn = label;
});
this._translateService.get('App.Home.SignIn.Header').subscribe(label => {
this.LabelHeaderSignIn = label;
});
this._translateService.get('App.Home.About.Header').subscribe(label => {
this.LabelHeaderAbout = label;
});
this._translateService.get('App.Home.Team.Header').subscribe(label => {
this.LabelHeaderTeam = label;
});
this._translateService.get('App.Home.Contact.Header').subscribe(label => {
this.LabelHeaderContact = label;
});
// LOGIN
this._translateService.get('App.Home.Login.Identify').subscribe(label => {
this.LabelLoginIdentify = label;
});
// SIGN IN
this._translateService.get('App.Home.SignIn.CreateProfile').subscribe(label => {
this.LabelSignInCreateProfile = label;
});
// FORM
this._translateService.get('App.Common.Form.LastName').subscribe(label => {
this.LabelFormLastName = label;
});
this._translateService.get('App.Common.Form.FirstName').subscribe(label => {
this.LabelFormFirstName = label;
});
this._translateService.get('App.Common.Form.Email').subscribe(label => {
this.LabelFormEmail = label;
});
this._translateService.get('App.Common.Form.Password').subscribe(label => {
this.LabelFormPassword = label;
});
this._translateService.get('App.Common.Form.ConfirmPassword').subscribe(label => {
this.LabelFormConfirmPassword = label;
});
this._translateService.get('App.Common.Form.Create').subscribe(label => {
this.LabelFormCreate = label;
});
}
}