Add typeahead address prefix.
This commit is contained in:
		
							parent
							
								
									0882cf289a
								
							
						
					
					
						commit
						77dacfdb6e
					
				@ -39,6 +39,7 @@ import { AssetsComponent } from './assets/assets.component';
 | 
			
		||||
import { StatusViewComponent } from './components/status-view/status-view.component';
 | 
			
		||||
import { MinerComponent } from './components/miner/miner.component';
 | 
			
		||||
import { SharedModule } from './shared/shared.module';
 | 
			
		||||
import { NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap';
 | 
			
		||||
 | 
			
		||||
@NgModule({
 | 
			
		||||
  declarations: [
 | 
			
		||||
@ -76,6 +77,7 @@ import { SharedModule } from './shared/shared.module';
 | 
			
		||||
    HttpClientModule,
 | 
			
		||||
    BrowserAnimationsModule,
 | 
			
		||||
    InfiniteScrollModule,
 | 
			
		||||
    NgbTypeaheadModule,
 | 
			
		||||
    SharedModule,
 | 
			
		||||
  ],
 | 
			
		||||
  providers: [
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,7 @@
 | 
			
		||||
<form [formGroup]="searchForm" (submit)="searchForm.valid && search()" class="mr-4" novalidate>
 | 
			
		||||
  <div class="form-row">
 | 
			
		||||
    <div style="width: 350px;" class="mr-2">
 | 
			
		||||
      <input formControlName="searchText" type="text" class="form-control" placeholder="Transaction, block height, hash or address">
 | 
			
		||||
      <input #instance="ngbTypeahead" [ngbTypeahead]="typeaheadSearch" (focus)="focus$.next($any($event).target.value)" (click)="click$.next($any($event).target.value)" formControlName="searchText" type="text" class="form-control" placeholder="Transaction, block height, hash or address">
 | 
			
		||||
    </div>
 | 
			
		||||
    <div>
 | 
			
		||||
      <button type="submit" class="btn btn-block btn-primary">Search</button>
 | 
			
		||||
 | 
			
		||||
@ -1,8 +1,12 @@
 | 
			
		||||
import { Component, OnInit, ChangeDetectionStrategy, EventEmitter, Output } from '@angular/core';
 | 
			
		||||
import { Component, OnInit, ChangeDetectionStrategy, EventEmitter, Output, ViewChild } from '@angular/core';
 | 
			
		||||
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
 | 
			
		||||
import { Router } from '@angular/router';
 | 
			
		||||
import { AssetsService } from 'src/app/services/assets.service';
 | 
			
		||||
import { StateService } from 'src/app/services/state.service';
 | 
			
		||||
import { Observable, of, Subject, merge } from 'rxjs';
 | 
			
		||||
import { debounceTime, distinctUntilChanged, switchMap, filter } from 'rxjs/operators';
 | 
			
		||||
import { ElectrsApiService } from 'src/app/services/electrs-api.service';
 | 
			
		||||
import { NgbTypeahead } from '@ng-bootstrap/ng-bootstrap';
 | 
			
		||||
 | 
			
		||||
@Component({
 | 
			
		||||
  selector: 'app-search-form',
 | 
			
		||||
@ -22,11 +26,30 @@ export class SearchFormComponent implements OnInit {
 | 
			
		||||
  regexTransaction = /^[a-fA-F0-9]{64}$/;
 | 
			
		||||
  regexBlockheight = /^[0-9]+$/;
 | 
			
		||||
 | 
			
		||||
  @ViewChild('instance', {static: true}) instance: NgbTypeahead;
 | 
			
		||||
  focus$ = new Subject<string>();
 | 
			
		||||
  click$ = new Subject<string>();
 | 
			
		||||
 | 
			
		||||
  typeaheadSearch = (text$: Observable<string>) => {
 | 
			
		||||
    const debouncedText$ = text$.pipe(debounceTime(200), distinctUntilChanged());
 | 
			
		||||
    const clicksWithClosedPopup$ = this.click$.pipe(filter(() => !this.instance.isPopupOpen()));
 | 
			
		||||
    const inputFocus$ = this.focus$;
 | 
			
		||||
 | 
			
		||||
    return merge(debouncedText$, inputFocus$, clicksWithClosedPopup$)
 | 
			
		||||
      .pipe(
 | 
			
		||||
        switchMap((text) => {
 | 
			
		||||
          if (!text.length) { return of([]); }
 | 
			
		||||
          return this.electrsApiService.getAddressesByPrefix$(text);
 | 
			
		||||
        })
 | 
			
		||||
      );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  constructor(
 | 
			
		||||
    private formBuilder: FormBuilder,
 | 
			
		||||
    private router: Router,
 | 
			
		||||
    private assetsService: AssetsService,
 | 
			
		||||
    private stateService: StateService,
 | 
			
		||||
    private electrsApiService: ElectrsApiService,
 | 
			
		||||
  ) { }
 | 
			
		||||
 | 
			
		||||
  ngOnInit() {
 | 
			
		||||
 | 
			
		||||
@ -81,4 +81,7 @@ export class ElectrsApiService {
 | 
			
		||||
    return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/asset/' + assetId + '/txs/chain/' + txid);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  getAddressesByPrefix$(prefix: string): Observable<string[]> {
 | 
			
		||||
    return this.httpClient.get<string[]>(this.apiBaseUrl + '/address-prefix/' + prefix);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user