/** @file * @author Edouard DUPIN * @copyright 2018, Edouard DUPIN, all right reserved * @license PROPRIETARY (see license file) */ import { Component, OnInit } from '@angular/core'; import { Location } from '@angular/common'; import { createPasswordState } from 'common/utils'; import { AdminUserService } from 'app/service'; export enum PasswordState { FILLING = "filling", SENDING = "sending", DONE = "done", ERROR = "error", } @Component({ selector: 'app-change-password', templateUrl: './change-password.html', styleUrls: ['./change-password.less'], }) export class ChangePasswordScene { /// State of the password scene... public updateState: PasswordState = PasswordState.FILLING; /// status of the 3 fields: public passwordOldState: boolean|string = false; public password1State: boolean|string = false; public password2State: boolean|string = false; /// data of the 3 field public passwordOld: string = ''; public password1: string = ''; public password2: string = ''; /// Validation button state public validateButtonDisabled: boolean = true; public error: string= ""; constructor( private locate: Location, private adminUserService: AdminUserService ) {} /** * update the state of the validation button. if all is OK, the button will became clickable */ updateButtonVisibility(): void { if (this.password1State === true && this.password2State === true && this.passwordOldState === true && this.password2 === this.password1) { this.validateButtonDisabled = false; } else { this.validateButtonDisabled = true; } } /** * Check if the current password is valid or not (update error message) * @param newValue New password value. */ checkPasswordOld(newValue: string): void { this.passwordOld = newValue; this.passwordOldState = createPasswordState(this.passwordOld); this.checkPassword(this.password1); this.updateButtonVisibility(); } /** * Check if the new password is valid or not * @param newValue New password value. */ checkPassword(newValue: string): void { this.password1 = newValue; this.password1State = createPasswordState(this.password1); if (this.password1State === true && this.passwordOld === this.password1) { this.password1State = 'Wrong new password (identical with current)'; } this.checkPassword2(this.password2); this.updateButtonVisibility(); } /** * Check if the new password is conform with the previous one * @param newValue New password value. */ checkPassword2(newValue: string): void { this.password2 = newValue; this.password2State = createPasswordState(this.password2); if (this.password2State === true && this.password2 !== this.password1) { this.password2State = 'Wrong 2nd password (not identical)'; } this.updateButtonVisibility(); } /** * Summit change password. */ onChangePassword(): void { this.updateState = PasswordState.SENDING; this.adminUserService.changePassword(this.passwordOld, this.password1) .then(() => { this.updateState = PasswordState.DONE; }) .catch((error: any) => { this.updateState = PasswordState.ERROR; this.error = error; }); } /** * Apply cancel on this page */ onCancel(): void { this.locate.back(); } /** * Whe error occured, the display permit to go back to retry the update: */ onRetry(): void { this.updateState = PasswordState.FILLING; this.error = ""; } }