import { Injectable } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { environment } from '../../environments/environment' ; import { GetTableByScopeResponse , TableData } from '../interfaces' ; import { Router } from '@angular/router' ; @ Injectable ( { providedIn : 'root' } ) export class SearchService { searchAccountUrl : string ; constructor ( private httpClient , private router ) { this . searchAccountUrl = environment . eosioNodeUrl + '/v1/chain/get_table_by_scope' ; } async filterAccountNames ( value ) { if ( ( value && value . length > 12 ) || ! value ) { return [ ] ; } try { const sValue = value . toLowerCase ( ) ; const requestBody = { code : environment . systemContract , table : environment . userResourcesTable , lower_bound : sValue , limit : 5 } ; const response = await this . httpClient . post ( this . searchAccountUrl , requestBody ) . toPromise ( ) as GetTableByScopeResponse ; if ( response . rows ) { return response . rows . filter ( ( tableData ) => { return tableData . scope . startsWith ( sValue ) ; } ) . map ( ( tableData ) => { return tableData . scope ; } ) ; } } catch ( error ) { console . log ( error ) ; return [ ] ; } } async submitSearch ( searchText , filteredAccounts ) { const sValue = searchText . toLowerCase ( ) ; if ( filteredAccounts . length > 0 ) { await this . router . navigate ( [ '/account' , sValue ] ) ; return true ; } if ( sValue . length === 64 ) { await this . router . navigate ( [ '/transaction' , sValue ] ) ; return true ; } if ( sValue . length > 0 && sValue . length <= 12 && isNaN ( sValue ) ) { await this . router . navigate ( [ '/account' , sValue ] ) ; return true ; } if ( searchText . startsWith ( 'PUB_K1_' ) || searchText . startsWith ( 'EOS' ) ) { await this . router . navigate ( [ '/key' , searchText ] ) ; return true ; } const blockNum = searchText . replace ( /[,.]/g , '' ) ; if ( parseInt ( blockNum , 10 ) > 0 ) { await this . router . navigate ( [ '/block' , blockNum ] ) ; return true ; } return false ; } }