/** @file * @author Edouard DUPIN * @copyright 2018, Edouard DUPIN, all right reserved * @license PROPRIETARY (see license file) */ import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit } from '@angular/core'; import { SessionService, AsyncActionState, createPasswordState, createLoginState, checkEmailValidity } from '@kangaroo-and-rabbit/kar-cw'; import { UserAuthGet } from 'back-api'; import { AdminUserService } from 'base/service'; interface UserAccountEdit extends UserAuthGet { adminState?: AsyncActionState; blockedState?: AsyncActionState; } @Component({ selector: 'app-settings', templateUrl: './manage-accounts.html', styleUrls: ['./manage-accounts.less'], changeDetection: ChangeDetectionStrategy.Default, }) export class ManageAccountsScene implements OnInit { users: UserAccountEdit[] = []; adminLogin: string = ''; constructor( private adminUserService: AdminUserService, private sessionService: SessionService, private cdr: ChangeDetectorRef, ) { } ngOnInit() { this.adminLogin = this.sessionService.getLogin(); const self = this; this.adminUserService .getUsers() .then((response: UserAuthGet[]) => { console.log(`??? get full response: ${JSON.stringify(response, null, 4)}`); self.users = response; }) .catch((error) => { console.log(`??? get ERROR response: ${JSON.stringify(error, null, 4)}`); }); } onSetAdmin(value: boolean, user: UserAccountEdit) { user.admin = value; console.log(`onSetAdmin: ${value} on ${user.login}`); user.adminState = AsyncActionState.LOADING; this.cdr.detectChanges(); const self = this; this.adminUserService.setAdmin(user.id, value) .then( () => { user.adminState = AsyncActionState.DONE; self.cdr.detectChanges(); setTimeout(() => { user.adminState = undefined; self.cdr.detectChanges(); }, 300); } ).catch( (error: any) => { user.adminState = AsyncActionState.FAIL; self.cdr.detectChanges(); setTimeout(() => { user.admin = !value; user.adminState = undefined; self.cdr.detectChanges(); }, 3000); } ); } onSetBlocked(value: boolean, user: UserAccountEdit) { user.blocked = value; console.log(`onSetBlocked: ${value} on ${user.login}`); user.blockedState = AsyncActionState.LOADING; this.cdr.detectChanges(); const self = this; this.adminUserService.setBlocked(user.id, value) .then( () => { console.log("all is fine ..."); user.blockedState = AsyncActionState.DONE; self.cdr.detectChanges(); setTimeout(() => { user.blockedState = undefined; self.cdr.detectChanges(); }, 300); } ).catch( (error: any) => { console.log(`catch error ... ${JSON.stringify(error, null, 2)}`); user.blockedState = AsyncActionState.FAIL; user.blocked = !value; self.cdr.detectChanges(); setTimeout(() => { user.blockedState = undefined; self.cdr.detectChanges(); }, 3000); } ); } public password: string = ''; public passwordState: boolean | string = false; public login: string = ''; public loginState: boolean | string = false; public email: string = ''; public emailState: boolean | string = false; public validateButtonCreateUserDisabled: boolean = true; /** * update the state of the validation button. if all is OK, the button will became clickable */ updateButtonVisibility(): void { if (this.passwordState === true && this.loginState === true && this.emailState === true) { this.validateButtonCreateUserDisabled = false; } else { this.validateButtonCreateUserDisabled = true; } } /** * Check if the current password is valid or not (update error message) * @param newValue New password value. */ checkPassword(newValue: string): void { this.password = newValue; this.passwordState = createPasswordState(this.password); this.updateButtonVisibility(); } createState: string | boolean = undefined; /** * Request the creation of a new user. */ onCreateUser(): void { console.log(`create user:`); this.createState = AsyncActionState.LOADING; const self = this; this.adminUserService.createUser(this.email, this.login, this.password) .then( (user_data: any) => { self.createState = AsyncActionState.DONE; console.log(`Get new user: ${JSON.stringify(user_data, null, 2)}`); self.users.push(user_data); self.cdr.detectChanges(); self.checkPassword(""); self.checkLogin(self.login); self.checkEmail(self.email); setTimeout(() => { this.createState = undefined; }, 3000); } ).catch( (error: any) => { self.createState = AsyncActionState.FAIL; setTimeout(() => { self.createState = undefined; }, 3000); } ); } /** * Check the login writing rules */ checkLogin(newValue: string): void { this.login = newValue; this.loginState = createLoginState(this.login); if (this.loginState === true) { this.loginState = "Checking login ..."; const self = this; this.adminUserService .checkLogin(this.login) .then((loginExist: boolean) => { // check if the answer is correct with the question if (newValue !== self.login) { return; } if (loginExist === false) { self.loginState = true; } else { // the login exist ... ==> it is found... self.loginState = 'Login already used ...'; } self.updateButtonVisibility(); }) .catch((error: number) => { console.log(`Status ${error}`); self.loginState = 'Error Occurred in fetching data ...'; self.updateButtonVisibility(); }); } this.updateButtonVisibility(); } checkEmail(newValue: string): void { this.email = newValue; if (!checkEmailValidity(this.email)) { this.emailState = "E-mail is not well formatted."; this.updateButtonVisibility(); return; } this.emailState = "Checking email ..."; const self = this; this.adminUserService.checkEMail(this.email).then( (emailExist: boolean) => { // check if the answer is correct with the question if (newValue !== self.email) { return; } if (emailExist === false) { self.emailState = true; } else { // the email exist ... ==> it is found... self.emailState = 'email already used ...'; } self.updateButtonVisibility(); }) .catch((error: number) => { console.log(`Status ${error}`); self.emailState = 'Error Occurred in fetching data ...'; self.updateButtonVisibility(); } ); this.updateButtonVisibility(); } formatTimestamp(unix_timestamp: number): string { return new Date(unix_timestamp).toISOString().replace("T", " ").replace("Z", " GMT").replace(".000", ""); //return new Date(unix_timestamp).toUTCString(); //return new Date(unix_timestamp).toLocaleString(); } formatIso(isoTS: string): string { return isoTS.replace("T", " ").replace("Z", " GMT").replace(".000", ""); } }