import { Component , OnInit } from '@angular/core' ; import { FormGroup , Validators , FormBuilder } from '@angular/forms' ; import { ApiService } from '@shared/services/api.service' ; import { GlobalService } from '@shared/services/global.service' ; import { ModalService } from '@shared/services/modal.service' ; import { WalletInfo } from '@shared/models/wallet-info' ; @ Component ( { selector : 'app-generate-addresses' , templateUrl : './generate-addresses.component.html' , styleUrls : [ './generate-addresses.component.scss' ] } ) export class GenerateAddressesComponent implements OnInit { constructor ( private apiService , private globalService , private genericModalService , private fb ) { this . buildGenerateAddressesForm ( ) ; } public generateAddressesForm : FormGroup ; public addresses : string [ ] ; public pageNumber = 1 ; formErrors = { 'generateAddresses' : '' } ; validationMessages = { 'generateAddresses' : { 'required' : 'Please enter an amount to generate.' , 'pattern' : 'Please enter a number between 1 and 10.' , 'min' : 'Please generate at least one address.' , 'max' : 'You can only generate 1000 addresses at once.' } } ; ngOnInit ( ) { } private buildGenerateAddressesForm ( ) { this . generateAddressesForm = this . fb . group ( { 'generateAddresses' : [ '' , Validators . compose ( [ Validators . required , Validators . pattern ( '^[0-9]*$' ) , Validators . min ( 1 ) , Validators . max ( 1000 ) ] ) ] } ) ; this . generateAddressesForm . valueChanges . subscribe ( ( ) => this . onValueChanged ( ) ) ; this . onValueChanged ( ) ; } onValueChanged ( ) { if ( ! this . generateAddressesForm ) { return ; } const form = this . generateAddressesForm ; for ( const field in this . formErrors ) { this . formErrors [ field ] = '' ; const control = form . get ( field ) ; if ( control && control . dirty && ! control . valid ) { const messages = this . validationMessages [ field ] ; for ( const key in control . errors ) { this . formErrors [ field ] += messages [ key ] + ' ' ; } } } } public onGenerateClicked ( ) { const walletInfo = new WalletInfo ( this . globalService . getWalletName ( ) ) ; this . apiService . getUnusedReceiveAddresses ( walletInfo , this . generateAddressesForm . get ( 'generateAddresses' ) . value ) . subscribe ( response => { this . addresses = response ; } ) ; } public onBackClicked ( ) { this . addresses = [ '' ] ; } }