cic-staff-client/src/app/pages/accounts/account-search/account-search.component.ts

86 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-04-02 11:50:52 +02:00
import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
2021-05-10 18:15:25 +02:00
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { CustomErrorStateMatcher } from '@app/_helpers';
import { UserService } from '@app/_services';
import { Router } from '@angular/router';
import { strip0x } from '@src/assets/js/ethtx/dist/hex';
import { environment } from '@src/environments/environment';
2021-04-02 11:50:52 +02:00
@Component({
selector: 'app-account-search',
templateUrl: './account-search.component.html',
styleUrls: ['./account-search.component.scss'],
2021-05-10 18:15:25 +02:00
changeDetection: ChangeDetectionStrategy.OnPush,
2021-04-02 11:50:52 +02:00
})
export class AccountSearchComponent implements OnInit {
phoneSearchForm: FormGroup;
phoneSearchSubmitted: boolean = false;
phoneSearchLoading: boolean = false;
addressSearchForm: FormGroup;
addressSearchSubmitted: boolean = false;
addressSearchLoading: boolean = false;
2021-04-30 14:50:16 +02:00
matcher: CustomErrorStateMatcher = new CustomErrorStateMatcher();
2021-04-02 11:50:52 +02:00
constructor(
private formBuilder: FormBuilder,
private userService: UserService,
2021-05-10 18:15:25 +02:00
private router: Router
) {
this.phoneSearchForm = this.formBuilder.group({
phoneNumber: ['', Validators.required],
});
this.addressSearchForm = this.formBuilder.group({
address: ['', Validators.required],
});
2021-04-02 11:50:52 +02:00
}
ngOnInit(): void {}
2021-05-10 18:15:25 +02:00
get phoneSearchFormStub(): any {
return this.phoneSearchForm.controls;
}
get addressSearchFormStub(): any {
return this.addressSearchForm.controls;
}
async onPhoneSearch(): Promise<void> {
this.phoneSearchSubmitted = true;
2021-05-10 18:15:25 +02:00
if (this.phoneSearchForm.invalid) {
return;
}
this.phoneSearchLoading = true;
2021-05-10 18:15:25 +02:00
(
await this.userService.getAccountByPhone(this.phoneSearchFormStub.phoneNumber.value, 100)
).subscribe(async (res) => {
if (res !== undefined) {
2021-05-10 18:15:25 +02:00
await this.router.navigateByUrl(
`/accounts/${strip0x(res.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0])}`
);
} else {
alert('Account not found!');
}
});
this.phoneSearchLoading = false;
}
async onAddressSearch(): Promise<void> {
this.addressSearchSubmitted = true;
2021-05-10 18:15:25 +02:00
if (this.addressSearchForm.invalid) {
return;
}
this.addressSearchLoading = true;
2021-05-10 18:15:25 +02:00
(
await this.userService.getAccountByAddress(this.addressSearchFormStub.address.value, 100)
).subscribe(async (res) => {
if (res !== undefined) {
2021-05-10 18:15:25 +02:00
await this.router.navigateByUrl(
`/accounts/${strip0x(res.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0])}`
);
} else {
alert('Account not found!');
}
});
this.addressSearchLoading = false;
}
2021-04-02 11:50:52 +02:00
}