Switch to async/await schema for all asynchronous function calls.

This commit is contained in:
Spencer Ofwiti 2021-03-18 12:10:55 +03:00
parent 6dc44be4eb
commit 9ec50e9c1a
10 changed files with 61 additions and 68 deletions

View File

@ -76,11 +76,11 @@ export class AuthService {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer'; xhr.responseType = 'arraybuffer';
xhr.open('GET', environment.cicAuthUrl + window.location.search.substring(1)); xhr.open('GET', environment.cicAuthUrl + window.location.search.substring(1));
xhr.onload = (e) => { xhr.onload = async (e) => {
if (xhr.status === 401) { if (xhr.status === 401) {
const authHeader = xhr.getResponseHeader('WWW-Authenticate'); const authHeader = xhr.getResponseHeader('WWW-Authenticate');
const o = hobaParseChallengeHeader(authHeader); const o = hobaParseChallengeHeader(authHeader);
this.loginResponse(o).then(); await this.loginResponse(o);
} }
}; };
xhr.send(); xhr.send();

View File

@ -19,8 +19,11 @@ export class AppComponent {
private transactionService: TransactionService, private transactionService: TransactionService,
private loggingService: LoggingService, private loggingService: LoggingService,
) { ) {
this.authService.mutableKeyStore.loadKeyring().then(() => this.authService.getPublicKeys().then()); (async () => {
this.tokenService.getTokens().then(async r => loggingService.sendInfoLevelMessage(await r)); await this.authService.mutableKeyStore.loadKeyring();
await this.authService.getPublicKeys();
this.loggingService.sendInfoLevelMessage(await this.tokenService.getTokens());
})();
this.mediaQuery.addListener(this.onResize); this.mediaQuery.addListener(this.onResize);
this.onResize(this.mediaQuery); this.onResize(this.mediaQuery);
} }
@ -51,14 +54,14 @@ export class AppComponent {
} }
@HostListener('window:cic_transfer', ['$event']) @HostListener('window:cic_transfer', ['$event'])
cicTransfer(event: CustomEvent): void { async cicTransfer(event: CustomEvent): Promise<void> {
const transaction = event.detail.tx; const transaction = event.detail.tx;
this.transactionService.setTransaction(transaction, 100).then(); await this.transactionService.setTransaction(transaction, 100);
} }
@HostListener('window:cic_convert', ['$event']) @HostListener('window:cic_convert', ['$event'])
cicConvert(event: CustomEvent): void { async cicConvert(event: CustomEvent): Promise<void> {
const conversion = event.detail.tx; const conversion = event.detail.tx;
this.transactionService.setConversion(conversion, 100).then(); await this.transactionService.setConversion(conversion, 100);
} }
} }

View File

@ -22,35 +22,33 @@ export class AuthComponent implements OnInit {
private router: Router private router: Router
) { } ) { }
ngOnInit(): void { async ngOnInit(): Promise<void> {
this.keyForm = this.formBuilder.group({ this.keyForm = this.formBuilder.group({
key: ['', Validators.required], key: ['', Validators.required],
}); });
if (this.authService.privateKey !== undefined ) { if (this.authService.privateKey !== undefined) {
this.authService.setKey(this.authService.privateKey).then(r => { const setKey = await this.authService.setKey(this.authService.privateKey);
if (this.authService.sessionToken !== undefined) { if (setKey && this.authService.sessionToken !== undefined) {
this.authService.setState( this.authService.setState(
'Click button to perform login ' + this.authService.sessionLoginCount + ' with token ' + this.authService.sessionToken); 'Click button to perform login ' + this.authService.sessionLoginCount + ' with token ' + this.authService.sessionToken);
} }
});
} }
} }
get keyFormStub(): any { return this.keyForm.controls; } get keyFormStub(): any { return this.keyForm.controls; }
onSubmit(): void { async onSubmit(): Promise<void> {
this.submitted = true; this.submitted = true;
if (this.keyForm.invalid) { return; } if (this.keyForm.invalid) { return; }
this.loading = true; this.loading = true;
this.authService.setKey(this.keyFormStub.key.value).then(); await this.authService.setKey(this.keyFormStub.key.value);
this.loading = false; this.loading = false;
} }
login(): void { login(): void {
const loginStatus = this.authService.login(); const loginStatus = this.authService.login();
console.log(loginStatus);
if (loginStatus) { if (loginStatus) {
this.router.navigate(['/home']); this.router.navigate(['/home']);
} }

View File

@ -1,6 +1,5 @@
import {ChangeDetectionStrategy, Component, OnInit, ViewChild} from '@angular/core'; import {ChangeDetectionStrategy, Component, OnInit, ViewChild} from '@angular/core';
import {MatTableDataSource} from '@angular/material/table'; import {MatTableDataSource} from '@angular/material/table';
import {SelectionModel} from '@angular/cdk/collections';
import {MatPaginator} from '@angular/material/paginator'; import {MatPaginator} from '@angular/material/paginator';
import {MatSort} from '@angular/material/sort'; import {MatSort} from '@angular/material/sort';
import {BlockSyncService, LocationService, LoggingService, TokenService, TransactionService, UserService} from '@app/_services'; import {BlockSyncService, LocationService, LoggingService, TokenService, TransactionService, UserService} from '@app/_services';
@ -81,11 +80,11 @@ export class AccountDetailsComponent implements OnInit {
this.route.paramMap.subscribe(async (params: Params) => { this.route.paramMap.subscribe(async (params: Params) => {
this.accountAddress = params.get('id'); this.accountAddress = params.get('id');
this.bloxbergLink = 'https://blockexplorer.bloxberg.org/address/' + this.accountAddress + '/transactions'; this.bloxbergLink = 'https://blockexplorer.bloxberg.org/address/' + this.accountAddress + '/transactions';
this.userService.getAccountDetailsFromMeta(await User.toKey(this.accountAddress)).pipe(first()).subscribe(res => { this.userService.getAccountDetailsFromMeta(await User.toKey(this.accountAddress)).pipe(first()).subscribe(async res => {
this.metaAccount = Envelope.fromJSON(JSON.stringify(res.body)).unwrap(); this.metaAccount = Envelope.fromJSON(JSON.stringify(res.body)).unwrap();
this.account = this.metaAccount.m.data; this.account = this.metaAccount.m.data;
this.loggingService.sendInfoLevelMessage(this.account); this.loggingService.sendInfoLevelMessage(this.account);
this.tokenService.getTokenBalance(this.accountAddress).then(r => this.accountBalance = r); this.accountBalance = await this.tokenService.getTokenBalance(this.accountAddress);
this.account.vcard = vCard.parse(atob(this.account.vcard)); this.account.vcard = vCard.parse(atob(this.account.vcard));
this.accountInfoForm.patchValue({ this.accountInfoForm.patchValue({
name: this.account.vcard?.fn[0].value, name: this.account.vcard?.fn[0].value,
@ -159,10 +158,10 @@ export class AccountDetailsComponent implements OnInit {
get accountInfoFormStub(): any { return this.accountInfoForm.controls; } get accountInfoFormStub(): any { return this.accountInfoForm.controls; }
saveInfo(): void { async saveInfo(): Promise<void> {
this.submitted = true; this.submitted = true;
if (this.accountInfoForm.invalid) { return; } if (this.accountInfoForm.invalid) { return; }
this.userService.changeAccountInfo( const accountKey = await this.userService.changeAccountInfo(
this.account.address, this.account.address,
this.accountInfoFormStub.name.value, this.accountInfoFormStub.name.value,
this.accountInfoFormStub.phoneNumber.value, this.accountInfoFormStub.phoneNumber.value,
@ -175,7 +174,8 @@ export class AccountDetailsComponent implements OnInit {
this.accountInfoFormStub.location.value, this.accountInfoFormStub.location.value,
this.accountInfoFormStub.locationType.value, this.accountInfoFormStub.locationType.value,
this.metaAccount this.metaAccount
).then(res => this.loggingService.sendInfoLevelMessage(`Response: ${res}`)); );
this.loggingService.sendInfoLevelMessage(`Response: ${accountKey}`);
this.submitted = false; this.submitted = false;
} }

View File

@ -2,7 +2,7 @@ import {ChangeDetectionStrategy, Component, OnInit, ViewChild} from '@angular/co
import {MatTableDataSource} from '@angular/material/table'; import {MatTableDataSource} from '@angular/material/table';
import {MatPaginator} from '@angular/material/paginator'; import {MatPaginator} from '@angular/material/paginator';
import {MatSort} from '@angular/material/sort'; import {MatSort} from '@angular/material/sort';
import {UserService} from '@app/_services'; import {LoggingService, UserService} from '@app/_services';
import {Router} from '@angular/router'; import {Router} from '@angular/router';
@Component({ @Component({
@ -24,9 +24,16 @@ export class AccountsComponent implements OnInit {
constructor( constructor(
private userService: UserService, private userService: UserService,
private loggingService: LoggingService,
private router: Router private router: Router
) { ) {
this.userService.loadAccounts(100).then(); (async () => {
try {
await this.userService.loadAccounts(100);
} catch (error) {
this.loggingService.sendErrorLevelMessage('Failed to load accounts', this, {error});
}
})();
} }
ngOnInit(): void { ngOnInit(): void {
@ -42,8 +49,8 @@ export class AccountsComponent implements OnInit {
this.dataSource.filter = value.trim().toLocaleLowerCase(); this.dataSource.filter = value.trim().toLocaleLowerCase();
} }
viewAccount(account): void { async viewAccount(account): Promise<void> {
this.router.navigateByUrl(`/accounts/${account.identities.evm['bloxberg:8996']}`).then(); await this.router.navigateByUrl(`/accounts/${account.identities.evm['bloxberg:8996']}`);
} }
filterAccounts(): void { filterAccounts(): void {

View File

@ -1,7 +1,6 @@
import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core'; import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {FormBuilder, FormGroup, Validators} from '@angular/forms'; import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {LocationService, UserService} from '@app/_services'; import {LocationService} from '@app/_services';
import {CustomErrorStateMatcher} from '@app/_helpers'; import {CustomErrorStateMatcher} from '@app/_helpers';
@Component({ @Component({
@ -18,8 +17,6 @@ export class CreateAccountComponent implements OnInit {
constructor( constructor(
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
private router: Router,
private userService: UserService,
private locationService: LocationService private locationService: LocationService
) { } ) { }
@ -47,21 +44,6 @@ export class CreateAccountComponent implements OnInit {
onSubmit(): void { onSubmit(): void {
this.submitted = true; this.submitted = true;
if (this.createForm.invalid) { return; } if (this.createForm.invalid) { return; }
// this.userService.createAccount(
// this.createFormStub.accountType.value,
// this.createFormStub.idNumber.value,
// this.createFormStub.phoneNumber.value,
// this.createFormStub.givenName.value,
// this.createFormStub.surname.value,
// this.createFormStub.directoryEntry.value,
// this.createFormStub.location.value,
// this.createFormStub.gender.value,
// this.createFormStub.referrer.value,
// this.createFormStub.businessCategory.value,
// ).pipe(first()).subscribe(res => {
// console.log(res);
// });
// this.router.navigateByUrl(`/accounts`);
this.submitted = false; this.submitted = false;
} }

View File

@ -2,7 +2,7 @@ import {ChangeDetectionStrategy, Component, OnInit, ViewChild} from '@angular/co
import {MatTableDataSource} from '@angular/material/table'; import {MatTableDataSource} from '@angular/material/table';
import {MatPaginator} from '@angular/material/paginator'; import {MatPaginator} from '@angular/material/paginator';
import {MatSort} from '@angular/material/sort'; import {MatSort} from '@angular/material/sort';
import {UserService} from '@app/_services'; import {LoggingService, UserService} from '@app/_services';
import {animate, state, style, transition, trigger} from '@angular/animations'; import {animate, state, style, transition, trigger} from '@angular/animations';
import {first} from 'rxjs/operators'; import {first} from 'rxjs/operators';
@ -28,7 +28,8 @@ export class AdminComponent implements OnInit {
@ViewChild(MatSort) sort: MatSort; @ViewChild(MatSort) sort: MatSort;
constructor( constructor(
private userService: UserService private userService: UserService,
private loggingService: LoggingService
) { ) {
this.userService.getActions(); this.userService.getActions();
this.userService.actionsSubject.subscribe(actions => { this.userService.actionsSubject.subscribe(actions => {
@ -50,12 +51,12 @@ export class AdminComponent implements OnInit {
} }
approveAction(action: any): void { approveAction(action: any): void {
this.userService.approveAction(action.id).pipe(first()).subscribe(res => console.log(res.body)); this.userService.approveAction(action.id).pipe(first()).subscribe(res => this.loggingService.sendInfoLevelMessage(res.body));
this.userService.getActions(); this.userService.getActions();
} }
revertAction(action: any): void { revertAction(action: any): void {
this.userService.revokeAction(action.id).pipe(first()).subscribe(res => console.log(res.body)); this.userService.revokeAction(action.id).pipe(first()).subscribe(res => this.loggingService.sendInfoLevelMessage(res.body));
this.userService.getActions(); this.userService.getActions();
} }

View File

@ -1,7 +1,7 @@
import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core'; import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core';
import {Color, Label} from 'ng2-charts'; import {Color, Label} from 'ng2-charts';
import {ChartDataSets, ChartOptions, ChartType} from 'chart.js'; import {ChartDataSets, ChartOptions, ChartType} from 'chart.js';
import {LocationService, UserService} from '@app/_services'; import {LocationService, LoggingService} from '@app/_services';
import {ArraySum} from '@app/_helpers'; import {ArraySum} from '@app/_helpers';
@Component({ @Component({
@ -149,8 +149,8 @@ export class PagesComponent implements OnInit {
]; ];
constructor( constructor(
private userService: UserService, private locationService: LocationService,
private locationService: LocationService private loggingService: LoggingService
) { ) {
this.locationService.getLocations(); this.locationService.getLocations();
this.locationService.locationsSubject.subscribe(locations => { this.locationService.locationsSubject.subscribe(locations => {
@ -177,11 +177,11 @@ export class PagesComponent implements OnInit {
} }
public chartClicked({ event, active}: { event: MouseEvent, active: {}[] }): void { public chartClicked({ event, active}: { event: MouseEvent, active: {}[] }): void {
console.log(event, active); this.loggingService.sendInfoLevelMessage(`Event: ${event}, ${active}`);
} }
public chartHovered({ event, active }: { event: MouseEvent, active: {}[] }): void { public chartHovered({ event, active }: { event: MouseEvent, active: {}[] }): void {
console.log(event, active); this.loggingService.sendInfoLevelMessage(`Event: ${event}, ${active}`);
} }
public trackByName(index, item): string { public trackByName(index, item): string {

View File

@ -1,7 +1,7 @@
import {ChangeDetectionStrategy, Component, OnInit, ViewChild} from '@angular/core'; import {ChangeDetectionStrategy, Component, OnInit, ViewChild} from '@angular/core';
import {MatPaginator} from '@angular/material/paginator'; import {MatPaginator} from '@angular/material/paginator';
import {MatSort} from '@angular/material/sort'; import {MatSort} from '@angular/material/sort';
import {TokenService} from '@app/_services'; import {LoggingService, TokenService} from '@app/_services';
import {MatTableDataSource} from '@angular/material/table'; import {MatTableDataSource} from '@angular/material/table';
import {Router} from '@angular/router'; import {Router} from '@angular/router';
@ -16,15 +16,17 @@ export class TokensComponent implements OnInit {
columnsToDisplay = ['name', 'symbol', 'address', 'supply']; columnsToDisplay = ['name', 'symbol', 'address', 'supply'];
@ViewChild(MatPaginator) paginator: MatPaginator; @ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort; @ViewChild(MatSort) sort: MatSort;
tokens: any;
constructor( constructor(
private tokenService: TokenService, private tokenService: TokenService,
private loggingService: LoggingService,
private router: Router private router: Router
) { ) { }
tokenService.getTokens();
}
ngOnInit(): void { async ngOnInit(): Promise<void> {
this.tokens = await this.tokenService.getTokens();
this.loggingService.sendInfoLevelMessage(this.tokens);
this.tokenService.tokensSubject.subscribe(tokens => { this.tokenService.tokensSubject.subscribe(tokens => {
this.dataSource = new MatTableDataSource(tokens); this.dataSource = new MatTableDataSource(tokens);
this.dataSource.paginator = this.paginator; this.dataSource.paginator = this.paginator;
@ -36,7 +38,7 @@ export class TokensComponent implements OnInit {
this.dataSource.filter = value.trim().toLocaleLowerCase(); this.dataSource.filter = value.trim().toLocaleLowerCase();
} }
viewToken(token): void { async viewToken(token): Promise<void> {
this.router.navigateByUrl(`/tokens/${token.symbol}`).then(); await this.router.navigateByUrl(`/tokens/${token.symbol}`);
} }
} }

View File

@ -19,11 +19,11 @@ export class TransactionDetailsComponent implements OnInit {
this.recipientBloxbergLink = 'https://blockexplorer.bloxberg.org/address/' + this.transaction?.to + '/transactions'; this.recipientBloxbergLink = 'https://blockexplorer.bloxberg.org/address/' + this.transaction?.to + '/transactions';
} }
viewSender(): void { async viewSender(): Promise<void> {
this.router.navigateByUrl(`/accounts/${this.transaction.from}`).then(); await this.router.navigateByUrl(`/accounts/${this.transaction.from}`);
} }
viewRecipient(): void { async viewRecipient(): Promise<void> {
this.router.navigateByUrl(`/accounts/${this.transaction.to}`).then(); await this.router.navigateByUrl(`/accounts/${this.transaction.to}`);
} }
} }