cic-staff-client/src/app/app.component.ts

128 lines
4.3 KiB
TypeScript
Raw Normal View History

2021-05-12 16:10:42 +02:00
import { ChangeDetectionStrategy, Component, HostListener, OnInit } from '@angular/core';
2021-05-10 18:15:25 +02:00
import {
AuthService,
BlockSyncService,
2021-05-10 18:15:25 +02:00
ErrorDialogService,
LoggingService,
TokenService,
2021-05-10 18:15:25 +02:00
TransactionService,
UserService,
2021-05-10 18:15:25 +02:00
} from '@app/_services';
2021-05-11 19:09:25 +02:00
import { SwUpdate } from '@angular/service-worker';
import { NavigationEnd, Router } from '@angular/router';
import { filter } from 'rxjs/operators';
2020-10-30 16:16:05 +01:00
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
2021-05-10 18:15:25 +02:00
changeDetection: ChangeDetectionStrategy.OnPush,
2020-10-30 16:16:05 +01:00
})
2021-03-22 20:13:11 +01:00
export class AppComponent implements OnInit {
title = 'CICADA';
2021-04-30 14:50:16 +02:00
mediaQuery: MediaQueryList = window.matchMedia('(max-width: 768px)');
url: string;
accountDetailsRegex = '/accounts/[a-z,A-Z,0-9]{40}';
2020-11-08 07:31:52 +01:00
constructor(
private authService: AuthService,
private blockSyncService: BlockSyncService,
private errorDialogService: ErrorDialogService,
private loggingService: LoggingService,
private tokenService: TokenService,
private transactionService: TransactionService,
private userService: UserService,
private swUpdate: SwUpdate,
private router: Router
) {
2021-05-10 18:15:25 +02:00
this.mediaQuery.addEventListener('change', this.onResize);
this.onResize(this.mediaQuery);
2020-11-08 07:31:52 +01:00
}
2021-05-19 18:57:10 +02:00
async ngOnInit(): Promise<void> {
await this.authService.init();
await this.tokenService.init();
await this.userService.init();
2021-05-19 18:57:10 +02:00
await this.transactionService.init();
await this.router.events
.pipe(filter((e) => e instanceof NavigationEnd))
.forEach(async (routeInfo) => {
if (routeInfo instanceof NavigationEnd) {
this.url = routeInfo.url;
if (!this.url.match(this.accountDetailsRegex) || !this.url.includes('tx')) {
await this.blockSyncService.blockSync();
}
if (!this.url.includes('accounts')) {
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.url.includes('tokens')) {
this.tokenService.load.subscribe(async (status: boolean) => {
if (status) {
await this.tokenService.getTokens();
}
});
}
}
});
2021-05-19 18:57:10 +02:00
try {
const publicKeys = await this.authService.getPublicKeys();
await this.authService.mutableKeyStore.importPublicKey(publicKeys);
2021-06-02 09:48:02 +02:00
this.authService.getTrustedUsers();
2021-05-19 18:57:10 +02:00
} catch (error) {
this.errorDialogService.openDialog({
message: 'Trusted keys endpoint cannot be reached. Please try again later.',
});
// TODO do something to halt user progress...show a sad cicada page 🦗?
}
2021-03-22 20:13:11 +01:00
if (!this.swUpdate.isEnabled) {
this.swUpdate.available.subscribe(() => {
if (confirm('New Version available. Load New Version?')) {
window.location.reload();
}
});
}
}
// Load resize
onResize(e): void {
2021-04-30 14:50:16 +02:00
const sidebar: HTMLElement = document.getElementById('sidebar');
const content: HTMLElement = document.getElementById('content');
const sidebarCollapse: HTMLElement = document.getElementById('sidebarCollapse');
if (sidebarCollapse?.classList.contains('active')) {
sidebarCollapse?.classList.remove('active');
}
if (e.matches) {
if (!sidebar?.classList.contains('active')) {
sidebar?.classList.add('active');
}
if (!content?.classList.contains('active')) {
content?.classList.add('active');
}
} else {
if (sidebar?.classList.contains('active')) {
sidebar?.classList.remove('active');
}
if (content?.classList.contains('active')) {
content?.classList.remove('active');
}
2020-11-08 07:31:52 +01:00
}
}
@HostListener('window:cic_transfer', ['$event'])
async cicTransfer(event: CustomEvent): Promise<void> {
2021-04-30 14:50:16 +02:00
const transaction: any = event.detail.tx;
await this.transactionService.setTransaction(transaction, 100);
2020-11-08 07:31:52 +01:00
}
@HostListener('window:cic_convert', ['$event'])
async cicConvert(event: CustomEvent): Promise<void> {
2021-04-30 14:50:16 +02:00
const conversion: any = event.detail.tx;
await this.transactionService.setConversion(conversion, 100);
2020-11-08 07:31:52 +01:00
}
2020-10-30 16:16:05 +01:00
}