import { Component , OnInit , ViewChild , Output , EventEmitter } from '@angular/core' ; import { SuggestedFees , Account , AddressPrefix } from '@signumjs/core' ; import { NgForm } from '@angular/forms' ; import { ActivatedRoute } from '@angular/router' ; import { AccountService } from 'app/setup/account/account.service' ; import { NotifierService } from 'angular-notifier' ; import { I18nService } from 'app/layout/components/i18n/i18n.service' ; import { AddressPattern } from 'app/util/addressPattern' ; import { NetworkService } from '../../../network/network.service' ; import { CurrencySymbol } from '@signumjs/util' ; import { handleException } from '../../../util/exceptions/handleException' ; const isNotEmpty = ( value ) => value && value . length > 0 ; @ Component ( { selector : 'app-add-alias' , templateUrl : './add-alias.component.html' , styleUrls : [ './add-alias.component.scss' ] } ) export class AddAliasComponent implements OnInit { @ ViewChild ( 'setAliasForm' , { static : false } ) public setAliasForm : NgForm ; @ ViewChild ( 'alias' , { static : false } ) public alias : string ; @ ViewChild ( 'description' , { static : false } ) public description : string ; @ ViewChild ( 'fullHash' , { static : false } ) public fullHash : string ; @ ViewChild ( 'pin' , { static : false } ) public pin : string ; @ ViewChild ( 'uri' , { static : false } ) public uri : string ; @ ViewChild ( 'accountAliasURI' , { static : true } ) public accountAliasURI : string ; @ Output ( ) submit = new EventEmitter < any > ( ) ; public feeNQT : string ; advanced = false ; showMessage = false ; addressPatternRef = AddressPattern ; type = 'acct' ; account : Account ; deadline = '24' ; fees : SuggestedFees ; addressPrefix : AddressPrefix . MainNet | AddressPrefix . TestNet ; symbol = CurrencySymbol ; isSending = false ; constructor ( private route , private accountService , private notifierService , private networkService , private i18nService ) { } ngOnInit ( ) { this . account = this . route . snapshot . data . account as Account ; this . fees = this . route . snapshot . data . suggestedFees as SuggestedFees ; this . addressPrefix = this . networkService . isMainNet ( ) ? AddressPrefix . MainNet : AddressPrefix . TestNet ; this . accountAliasURI = this . account . accountRS ; } async onSubmit ( event ) { this . isSending = true ; event . stopImmediatePropagation ( ) ; try { await this . accountService . setAlias ( { aliasName : this . alias , aliasURI : this . getAliasURI ( ) , feeNQT : this . feeNQT , deadline : parseFloat ( this . deadline ) * 60 , pin : this . pin , keys : this . account . keys , } ) ; this . notifierService . notify ( 'success' , this . i18nService . getTranslation ( 'success_alias_register' ) ) ; this . setAliasForm . resetForm ( ) ; this . accountAliasURI = this . account . accountRS ; this . type = 'acct' ; } catch ( e ) { handleException ( { e , i18nService : this . i18nService , notifierService : this . notifierService } ) ; } finally { this . isSending = false ; } } private getAliasURI ( ) { switch ( this . type ) { case 'acct' : return `${ this . type }:${ this . accountAliasURI . toLowerCase ( ) }@burst` ; default : return this . uri ; } } canSubmit ( ) { let value = '' ; switch ( this . type ) { case 'acct' : value = this . accountAliasURI ; break ; case 'uri' : value = this . uri ; break ; case 'other' : default : value = this . alias ; } return isNotEmpty ( this . pin ) && isNotEmpty ( this . alias ) && isNotEmpty ( value ) ; } }