Connect account address search to meta service.

This commit is contained in:
Spencer Ofwiti 2021-04-08 15:16:00 +03:00
parent 62655a691a
commit 9a87e5af04
3 changed files with 41 additions and 33 deletions

View File

@ -1,10 +1,10 @@
import {Injectable} from '@angular/core';
import {BehaviorSubject, Observable} from 'rxjs';
import {BehaviorSubject, Observable, of, Subject} from 'rxjs';
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {environment} from '@src/environments/environment';
import {first} from 'rxjs/operators';
import {ArgPair, Envelope, Syncable, User} from 'cic-client-meta';
import {MetaResponse} from '@app/_models';
import {ArgPair, Envelope, Phone, Syncable, User} from 'cic-client-meta';
import {AccountDetails, MetaResponse} from '@app/_models';
import {LoggingService} from '@app/_services/logging.service';
import {TokenService} from '@app/_services/token.service';
import {AccountIndex, Registry} from '@app/_eth';
@ -157,21 +157,35 @@ export class UserService {
const accountAddresses = await accountIndexQuery.last(await accountIndexQuery.totalAccounts());
this.loggingService.sendInfoLevelMessage(accountAddresses);
for (const accountAddress of accountAddresses.slice(offset, offset + limit)) {
this.getAccountDetailsFromMeta(await User.toKey(accountAddress)).pipe(first()).subscribe(async res => {
const account = Envelope.fromJSON(JSON.stringify(res)).unwrap();
this.accountsMeta.push(account);
const accountInfo = account.m.data;
accountInfo.balance = await this.tokenService.getTokenBalance(accountInfo.identities.evm['bloxberg:8996'][0]);
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
this.accounts.unshift(accountInfo);
if (this.accounts.length > limit) {
this.accounts.length = limit;
}
this.accountsList.next(this.accounts);
});
await this.getAccountByAddress(accountAddress, limit);
}
}
async getAccountByAddress(accountAddress: string, limit: number = 100): Promise<Observable<AccountDetails>> {
let accountSubject = new Subject<any>();
this.getAccountDetailsFromMeta(await User.toKey(accountAddress)).pipe(first()).subscribe(async res => {
const account = Envelope.fromJSON(JSON.stringify(res)).unwrap();
this.accountsMeta.push(account);
const accountInfo = account.m.data;
accountInfo.balance = await this.tokenService.getTokenBalance(accountInfo.identities.evm['bloxberg:8996'][0]);
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
this.accounts.unshift(accountInfo);
if (this.accounts.length > limit) {
this.accounts.length = limit;
}
this.accountsList.next(this.accounts);
accountSubject.next(accountInfo);
console.log(accountInfo);
});
return accountSubject.asObservable();
}
async getAccountByPhone(phoneNumber: string): Promise<any> {
this.getAccountDetailsFromMeta(await Phone.toKey(Number(phoneNumber))).pipe(first()).subscribe(res => {
console.log(res);
});
}
resetAccountsList(): void {
this.accounts = [];
this.accountsList.next(this.accounts);
@ -180,6 +194,4 @@ export class UserService {
searchAccountByName(name: string): any { return; }
searchAccountByPhone(phoneNumber: string): any { return; }
searchAccountByAddress(address: string): any { return; }
}

View File

@ -23,18 +23,6 @@
</mat-card-title>
<div class="card-body">
<mat-tab-group>
<mat-tab label="Name">
<form [formGroup]="nameSearchForm" (ngSubmit)="onNameSearch()">
<mat-form-field appearance="outline">
<mat-label> Search </mat-label>
<input matInput type="text" placeholder="Search by name" formControlName="name" [errorStateMatcher]="matcher">
<mat-error *ngIf="nameSearchSubmitted && nameSearchFormStub.name.errors">Name is required.</mat-error>
<mat-icon matSuffix>face</mat-icon>
<mat-hint>Name</mat-hint>
</mat-form-field>
<button mat-raised-button color="primary" type="submit" class="btn btn-outline-primary ml-3"> SEARCH </button>
</form>
</mat-tab>
<mat-tab label="Phone Number">
<form [formGroup]="phoneSearchForm" (ngSubmit)="onPhoneSearch()">
<mat-form-field appearance="outline">

View File

@ -2,6 +2,7 @@ import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {CustomErrorStateMatcher} from '@app/_helpers';
import {UserService} from '@app/_services';
import {Router} from '@angular/router';
@Component({
selector: 'app-account-search',
@ -24,6 +25,7 @@ export class AccountSearchComponent implements OnInit {
constructor(
private formBuilder: FormBuilder,
private userService: UserService,
private router: Router,
) { }
ngOnInit(): void {
@ -50,19 +52,25 @@ export class AccountSearchComponent implements OnInit {
this.nameSearchLoading = false;
}
onPhoneSearch(): void {
async onPhoneSearch(): Promise<void> {
this.phoneSearchSubmitted = true;
if (this.phoneSearchForm.invalid) { return; }
this.phoneSearchLoading = true;
this.userService.searchAccountByPhone(this.phoneSearchFormStub.phoneNumber.value);
await this.userService.getAccountByPhone(this.phoneSearchFormStub.phoneNumber.value);
this.phoneSearchLoading = false;
}
onAddressSearch(): void {
async onAddressSearch(): Promise<void> {
this.addressSearchSubmitted = true;
if (this.addressSearchForm.invalid) { return; }
this.addressSearchLoading = true;
this.userService.searchAccountByAddress(this.addressSearchFormStub.address.value);
(await this.userService.getAccountByAddress(this.addressSearchFormStub.address.value, 100)).subscribe(async res => {
if (res !== undefined) {
await this.router.navigateByUrl(`/accounts/${res.identities.evm['bloxberg:8996']}`);
} else {
alert('Account not found!');
}
});
this.addressSearchLoading = false;
}
}