import { Component } from '@angular/core' ; import { Router } from '@angular/router' ; import { CreateService } from '../../create.service' ; import { NotifierService } from 'angular-notifier' ; import { sampleSize } from 'lodash' ; import { AccountService } from '../../account.service' ; import { I18nService } from '../../../../layout/components/i18n/i18n.service' ; interface Token { text : string ; ok : boolean ; } @ Component ( { selector : 'app-account-activate' , styleUrls : [ './activate.component.scss' ] , templateUrl : './activate.component.html' } ) export class AccountActivateComponent { isActivating = false ; passphrase = '' ; randomizedTokens : Token [ ] = [ ] ; constructor ( private router , private createService , private accountService , private i18nService , private notificationService ) { } isStepActive ( ) { return this . createService . getStep ( ) === 4 ; } ngDoCheck ( ) { if ( ! ( this . isStepActive ( ) && this . randomizedTokens . length === 0 ) ) { return ; } this . randomizePassphraseTokens ( ) ; } public back ( ) { setTimeout ( ( ) => { this . createService . previousStep ( ) ; } , 0 ) ; } public reset ( ) { this . passphrase = '' ; this . randomizedTokens = [ ] ; this . randomizePassphraseTokens ( ) ; } private randomizePassphraseTokens ( ) { const tokens = this . createService . getPhrase ( ) . split ( ' ' ) ; if ( tokens && tokens . length ) { this . randomizedTokens = sampleSize ( tokens , tokens . length ) . map ( t => ( { text : t , ok : null , } ) ) ; } } public get isValid ( ) : boolean { if ( ! this . isStepActive ( ) ) { return false ; } return this . createService . getPhrase ( ) === this . passphrase ; } private async activateAccount ( ) { const newAccount = this . createService . getAccount ( ) ; await this . accountService . activateAccount ( newAccount ) ; this . notificationService . notify ( 'success' , this . i18nService . getTranslation ( 'activate_request_successful' ) ) ; } public async activate ( ) { if ( ! this . isValid ) { return ; } try { this . isActivating = true ; await this . createService . createActiveAccount ( ) ; await this . activateAccount ( ) ; this . createService . reset ( ) ; this . notificationService . notify ( 'success' , this . i18nService . getTranslation ( 'account_added' ) ) ; } catch ( error ) { this . notificationService . notify ( 'error' , error . toString ( ) ) ; } finally { await this . router . navigate ( [ '/' ] ) ; this . isActivating = false ; } } private isPassphraseFine ( ) { return this . createService . getPhrase ( ) . startsWith ( this . passphrase ) ; } public confirm ( $event ) { const text = $event . target . innerText ; this . passphrase += ' ' + text ; this . passphrase = this . passphrase . trim ( ) ; const index = this . randomizedTokens . findIndex ( t => t . text === text ) ; if ( index >= 0 ) { this . randomizedTokens [ index ] . ok = this . isPassphraseFine ( ) ; } } }