Refactor data load process.

- Move data loading to app component.
- Move services init calls to app component.
This commit is contained in:
Spencer Ofwiti 2021-06-30 14:51:47 +03:00
parent a070cbe710
commit e1bb7355e3
14 changed files with 30 additions and 70 deletions

View File

@ -4,7 +4,6 @@ import { TransactionHelper } from '@cicnet/cic-client';
import { first } from 'rxjs/operators';
import { TransactionService } from '@app/_services/transaction.service';
import { environment } from '@src/environments/environment';
import { LoggingService } from '@app/_services/logging.service';
import { RegistryService } from '@app/_services/registry.service';
import { Web3Service } from '@app/_services/web3.service';
@ -17,13 +16,8 @@ export class BlockSyncService {
constructor(
private transactionService: TransactionService,
private loggingService: LoggingService
) {}
async init(): Promise<void> {
await this.transactionService.init();
}
async blockSync(address: string = null, offset: number = 0, limit: number = 100): Promise<void> {
this.transactionService.resetTransactionsList();
const settings: Settings = new Settings(this.scan);

View File

@ -42,8 +42,6 @@ export class TransactionService {
}
async init(): Promise<void> {
await this.authService.init();
await this.userService.init();
this.registry = await RegistryService.getRegistry();
}

View File

@ -7,11 +7,9 @@ import { ArgPair, Envelope, Phone, Syncable, User } from 'cic-client-meta';
import { AccountDetails } from '@app/_models';
import { LoggingService } from '@app/_services/logging.service';
import { TokenService } from '@app/_services/token.service';
import { AccountIndex } from '@app/_eth';
import { MutableKeyStore, PGPSigner, Signer } from '@app/_pgp';
import { RegistryService } from '@app/_services/registry.service';
import { CICRegistry } from '@cicnet/cic-client';
import { AuthService } from '@app/_services/auth.service';
import { personValidation, updateSyncable, vcardValidation } from '@app/_helpers';
import { add0x } from '@src/assets/js/ethtx/dist/hex';
import { KeystoreService } from '@app/_services/keystore.service';
@ -44,12 +42,9 @@ export class UserService {
private httpClient: HttpClient,
private loggingService: LoggingService,
private tokenService: TokenService,
private authService: AuthService
) {}
async init(): Promise<void> {
await this.authService.init();
await this.tokenService.init();
this.keystore = await KeystoreService.getKeystore();
this.signer = new PGPSigner(this.keystore);
this.registry = await RegistryService.getRegistry();
@ -204,11 +199,6 @@ export class UserService {
async loadAccounts(limit: number = 100, offset: number = 0): Promise<void> {
this.resetAccountsList();
// const accountIndexAddress: string = await this.registry.getContractAddressByName(
// 'AccountRegistry'
// );
// const accountIndexQuery = new AccountIndex(accountIndexAddress);
// const accountAddresses: Array<string> = await accountIndexQuery.last(limit);
try {
const accountRegistry = await RegistryService.getAccountRegistry();
const accountAddresses: Array<string> = await accountRegistry.last(limit);

View File

@ -1,11 +1,10 @@
import { ChangeDetectionStrategy, Component, HostListener, OnInit } from '@angular/core';
import {
AuthService,
AuthService, BlockSyncService,
ErrorDialogService,
LoggingService,
TransactionService,
LoggingService, TokenService,
TransactionService, UserService,
} from '@app/_services';
import { catchError } from 'rxjs/operators';
import { SwUpdate } from '@angular/service-worker';
@Component({
@ -22,9 +21,12 @@ export class AppComponent implements OnInit {
constructor(
private authService: AuthService,
private transactionService: TransactionService,
private loggingService: LoggingService,
private blockSyncService: BlockSyncService,
private errorDialogService: ErrorDialogService,
private loggingService: LoggingService,
private tokenService: TokenService,
private transactionService: TransactionService,
private userService: UserService,
private swUpdate: SwUpdate
) {
this.mediaQuery.addEventListener('change', this.onResize);
@ -33,7 +35,10 @@ export class AppComponent implements OnInit {
async ngOnInit(): Promise<void> {
await this.authService.init();
await this.tokenService.init();
await this.userService.init();
await this.transactionService.init();
await this.blockSyncService.blockSync();
try {
const publicKeys = await this.authService.getPublicKeys();
await this.authService.mutableKeyStore.importPublicKey(publicKeys);
@ -44,6 +49,12 @@ export class AppComponent implements OnInit {
});
// TODO do something to halt user progress...show a sad cicada page 🦗?
}
try {
// TODO it feels like this should be in the onInit handler
await this.userService.loadAccounts(100);
} catch (error) {
this.loggingService.sendErrorLevelMessage('Failed to load accounts', this, { error });
}
if (!this.swUpdate.isEnabled) {
this.swUpdate.available.subscribe(() => {
if (confirm('New Version available. Load New Version?')) {

View File

@ -3,7 +3,6 @@ import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { CustomErrorStateMatcher } from '@app/_helpers';
import { AuthService } from '@app/_services';
import { ErrorDialogService } from '@app/_services/error-dialog.service';
import { LoggingService } from '@app/_services/logging.service';
import { Router } from '@angular/router';
@Component({
@ -25,7 +24,7 @@ export class AuthComponent implements OnInit {
private errorDialogService: ErrorDialogService
) {}
async ngOnInit(): Promise<void> {
ngOnInit(): void {
this.keyForm = this.formBuilder.group({
key: ['', Validators.required],
});

View File

@ -103,10 +103,6 @@ export class AccountDetailsComponent implements OnInit {
location: ['', Validators.required],
locationType: ['', Validators.required],
});
await this.blockSyncService.init();
await this.tokenService.init();
await this.transactionService.init();
await this.userService.init();
await this.blockSyncService.blockSync(this.accountAddress);
this.userService.resetAccountsList();
(await this.userService.getAccountByAddress(this.accountAddress, 100)).subscribe(
@ -114,7 +110,6 @@ export class AccountDetailsComponent implements OnInit {
if (res !== undefined) {
this.account = res;
this.cdr.detectChanges();
this.loggingService.sendInfoLevelMessage(this.account);
this.locationService.areaNamesSubject.subscribe((response) => {
this.area = this.locationService.getAreaNameByLocation(
this.account.location.area_name,

View File

@ -40,9 +40,7 @@ export class AccountSearchComponent implements OnInit {
});
}
async ngOnInit(): Promise<void> {
await this.userService.init();
}
ngOnInit(): void {}
get nameSearchFormStub(): any {
return this.nameSearchForm.controls;

View File

@ -2,7 +2,7 @@ import { ChangeDetectionStrategy, Component, OnInit, ViewChild } from '@angular/
import { MatTableDataSource } from '@angular/material/table';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { LoggingService, TokenService, UserService } from '@app/_services';
import { TokenService, UserService } from '@app/_services';
import { Router } from '@angular/router';
import { exportCsv } from '@app/_helpers';
import { strip0x } from '@src/assets/js/ethtx/dist/hex';
@ -31,20 +31,11 @@ export class AccountsComponent implements OnInit {
constructor(
private userService: UserService,
private loggingService: LoggingService,
private router: Router,
private tokenService: TokenService
) {}
async ngOnInit(): Promise<void> {
await this.userService.init();
await this.tokenService.init();
try {
// TODO it feels like this should be in the onInit handler
await this.userService.loadAccounts(100);
} catch (error) {
this.loggingService.sendErrorLevelMessage('Failed to load accounts', this, { error });
}
ngOnInit(): void {
this.userService.accountsSubject.subscribe((accounts) => {
this.dataSource = new MatTableDataSource<any>(accounts);
this.dataSource.paginator = this.paginator;

View File

@ -25,8 +25,7 @@ export class CreateAccountComponent implements OnInit {
private userService: UserService
) {}
async ngOnInit(): Promise<void> {
await this.userService.init();
ngOnInit(): void {
this.createForm = this.formBuilder.group({
accountType: ['', Validators.required],
idNumber: ['', Validators.required],

View File

@ -6,7 +6,7 @@ import { LoggingService, UserService } from '@app/_services';
import { animate, state, style, transition, trigger } from '@angular/animations';
import { first } from 'rxjs/operators';
import { exportCsv } from '@app/_helpers';
import { Action } from '../../_models';
import { Action } from '@app/_models';
@Component({
selector: 'app-admin',
@ -32,8 +32,7 @@ export class AdminComponent implements OnInit {
constructor(private userService: UserService, private loggingService: LoggingService) {}
async ngOnInit(): Promise<void> {
await this.userService.init();
ngOnInit(): void {
this.userService.getActions();
this.userService.actionsSubject.subscribe((actions) => {
this.dataSource = new MatTableDataSource<any>(actions);

View File

@ -24,8 +24,7 @@ export class SettingsComponent implements OnInit {
constructor(private authService: AuthService) {}
async ngOnInit(): Promise<void> {
await this.authService.init();
ngOnInit(): void {
this.authService.trustedUsersSubject.subscribe((users) => {
this.dataSource = new MatTableDataSource<any>(users);
this.dataSource.paginator = this.paginator;

View File

@ -1,9 +1,8 @@
import { ChangeDetectionStrategy, Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { LoggingService, TokenService } from '@app/_services';
import { TokenService } from '@app/_services';
import { MatTableDataSource } from '@angular/material/table';
import { Router } from '@angular/router';
import { exportCsv } from '@app/_helpers';
import { Token } from '@app/_models';
@ -23,19 +22,15 @@ export class TokensComponent implements OnInit {
constructor(
private tokenService: TokenService,
private loggingService: LoggingService,
private router: Router
) {}
async ngOnInit(): Promise<void> {
await this.tokenService.init();
ngOnInit(): void {
this.tokenService.load.subscribe(async (status: boolean) => {
if (status) {
await this.tokenService.getTokens();
}
});
this.tokenService.tokensSubject.subscribe((tokens) => {
this.loggingService.sendInfoLevelMessage(tokens);
this.dataSource = new MatTableDataSource(tokens);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;

View File

@ -36,9 +36,7 @@ export class TransactionDetailsComponent implements OnInit {
private tokenService: TokenService
) {}
async ngOnInit(): Promise<void> {
await this.transactionService.init();
await this.tokenService.init();
ngOnInit(): void {
if (this.transaction?.type === 'conversion') {
this.traderBloxbergLink =
'https://blockexplorer.bloxberg.org/address/' + this.transaction?.trader + '/transactions';

View File

@ -5,7 +5,7 @@ import {
OnInit,
ViewChild,
} from '@angular/core';
import { BlockSyncService, TokenService, TransactionService, UserService } from '@app/_services';
import { TokenService, TransactionService, UserService } from '@app/_services';
import { MatTableDataSource } from '@angular/material/table';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
@ -34,24 +34,18 @@ export class TransactionsComponent implements OnInit, AfterViewInit {
@ViewChild(MatSort) sort: MatSort;
constructor(
private blockSyncService: BlockSyncService,
private transactionService: TransactionService,
private userService: UserService,
private tokenService: TokenService
) {}
async ngOnInit(): Promise<void> {
ngOnInit(): void {
this.transactionService.transactionsSubject.subscribe((transactions) => {
this.transactionDataSource = new MatTableDataSource<any>(transactions);
this.transactionDataSource.paginator = this.paginator;
this.transactionDataSource.sort = this.sort;
this.transactions = transactions;
});
await this.blockSyncService.init();
await this.tokenService.init();
await this.transactionService.init();
await this.userService.init();
await this.blockSyncService.blockSync();
this.userService
.getTransactionTypes()
.pipe(first())