/** @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 { AdminUserService } from 'app/service'; import { AsyncActionState } from 'common/component'; import { SessionService } from 'common/service'; import { checkEmailValidity, checkLoginValidity, createLoginState, createPasswordState, isBoolean, isInArray, isNullOrUndefined, isNumber, isObject, isOptionalArrayOf, isOptionalOf, isString, } from 'common/utils'; export enum SettingType { TITLE = 'TITLE', GROUP = 'GROUP', LINE = 'LINE', BOOLEAN = 'BOOLEAN', STRING = 'STRING', PASSWORD = 'PASSWORD', } export function isSettingType(data: any): data is SettingType { return isInArray(data, ['TITLE', 'GROUP', 'LINE', 'BOOLEAN', 'STRING', 'PASSWORD']); } export interface SettingsItem { // Type of the menu Node type: SettingType; // Displayed Title title?: string; // Description of the parameter description?: string; // Image to dsplay that describe the parameter image?: string; // If true the parameter is applied directly to the backend directApply?: boolean; // Parameter key to SET/GET key?: string; // Parameter key to SET/GET or the sub-menu value?: SettingsItem[] | boolean | string | Number; // whendata is change the value is set here: newValue?: boolean | string | Number; } export function isSettingsItem(data: any): data is SettingsItem { if (isNullOrUndefined(data)) { return false; } if (!isObject(data)) { return false; } if (!isSettingType(data.type)) { return false; } if (!isOptionalOf(data.title, isString)) { return false; } if (!isOptionalOf(data.description, isString)) { return false; } if (!isOptionalOf(data.image, isString)) { return false; } if (!isOptionalOf(data.directApply, isBoolean)) { return false; } if (!isOptionalOf(data.key, isString)) { return false; } if ( !isOptionalOf(data.value, isBoolean) && !isOptionalOf(data.value, isString) && !isOptionalOf(data.value, isNumber) && !isOptionalArrayOf(data.value, isSettingsItem) ) { return false; } if ( !isOptionalOf(data.newValue, isBoolean) && !isOptionalOf(data.newValue, isString) && !isOptionalOf(data.newValue, isNumber) ) { return false; } return true; } @Component({ selector: 'app-settings', templateUrl: './manage-accounts.html', styleUrls: ['./manage-accounts.less'], changeDetection: ChangeDetectionStrategy.Default, }) export class ManageAccountsScene implements OnInit { users: any = []; adminLogin: string = ""; constructor( private adminUserService: AdminUserService, private sessionService: SessionService, private cdr: ChangeDetectorRef, ) { } ngOnInit() { this.adminLogin = this.sessionService.getLogin(); let self = this; this.adminUserService .getUsers() .then((response: any) => { console.log(`??? get full response: ${JSON.stringify(response, null, 4)}`); self.users = response; }) .catch((error: any) => { console.log(`??? get ERROR response: ${JSON.stringify(error, null, 4)}`); }); } onSetAdmin(value: boolean, user: any) { user.admin = value; console.log(`onSetAdmin: ${value} on ${user.login}`); user.adminState = AsyncActionState.LOADING; this.cdr.detectChanges(); let self = this; this.adminUserService.setAdmin(user.id, value) .then( () => { user.adminState = AsyncActionState.DONE; self.cdr.detectChanges(); setTimeout(() => { user.adminState = undefined; self.cdr.detectChanges(); }, 3000); } ).catch( (error: any) => { user.adminState = AsyncActionState.FAIL; self.cdr.detectChanges(); setTimeout(() => { user.adminState = undefined; user.admin = !value; self.cdr.detectChanges(); }, 3000); } ); } onSetBlocked(value:boolean, user: any) { user.blocked = value; console.log(`onSetBlocked: ${value} on ${user.login}`); user.blockedState = AsyncActionState.LOADING; this.cdr.detectChanges(); let self = this; this.adminUserService.setBlocked(user.id, value) .then( () => { user.blockedState = AsyncActionState.DONE; self.cdr.detectChanges(); setTimeout(() => { user.blockedState = undefined; self.cdr.detectChanges(); }, 3000); } ).catch( (error: any) => { user.blockedState = AsyncActionState.FAIL; self.cdr.detectChanges(); setTimeout(() => { user.blockedState = undefined; user.blocked = !value; 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; let self = this; this.adminUserService.createUsers(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 ..."; let self = this; this.adminUserService .checkLogin(this.login) .then((isNotUsed: boolean) => { // check if the answer is correct with the question if (newValue !== self.login) { return; } if (isNotUsed === false) { // the login exist ... ==> it is found... self.loginState = 'Login already used ...'; self.updateButtonVisibility(); } else { self.loginState = true; self.updateButtonVisibility(); } }) .catch((error: number) => { console.log(`Status ${error}`); self.loginState = 'ErrorOccured 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 formated."; this.updateButtonVisibility(); return; } this.emailState = "Checking email ..."; let self = this; this.adminUserService.checkEMail(this.email).then( (isNotUsed: boolean) => { // check if the answer is correct with the question if (newValue !== self.email) { return; } if (isNotUsed === false) { // the email exist ... ==> it is found... self.emailState = 'email already used ...'; self.updateButtonVisibility(); } else { self.emailState = true; self.updateButtonVisibility(); return; } }) .catch((error: number) => { console.log(`Status ${error}`); self.emailState = 'ErrorOccured 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(); } }