You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RTL/src/app/app.component.ts

167 lines
5.9 KiB
TypeScript

import { Component, OnInit, AfterViewInit, OnDestroy, ViewChild, HostListener } from '@angular/core';
import { Router } from '@angular/router';
import { Subject } from 'rxjs';
import { takeUntil, filter } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { Actions } from '@ngrx/effects';
import { UserIdleService } from 'angular-user-idle';
import { LoggerService } from './shared/services/logger.service';
import { Settings, Authentication } from './shared/models/RTLconfig';
import { GetInfo } from './shared/models/lndModels';
import * as RTLActions from './shared/store/rtl.actions';
import * as fromRTLReducer from './shared/store/rtl.reducers';
@Component({
selector: 'rtl-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
@ViewChild('sideNavigation') sideNavigation: any;
@ViewChild('settingSidenav') settingSidenav: any;
public information: GetInfo = {};
public flgLoading: Array<Boolean | 'error'> = [true]; // 0: Info
public flgCopied = false;
public settings: Settings;
public authSettings: Authentication;
public accessKey = '';
public smallScreen = false;
unsubs: Array<Subject<void>> = [new Subject(), new Subject(), new Subject()];
constructor(private logger: LoggerService, private store: Store<fromRTLReducer.State>, private actions$: Actions,
private userIdle: UserIdleService, private router: Router) {}
ngOnInit() {
this.store.dispatch(new RTLActions.FetchSettings());
this.accessKey = this.readAccessKey();
this.store.select('rtlRoot')
.pipe(takeUntil(this.unsubs[0]))
.subscribe(rtlStore => {
this.settings = rtlStore.settings;
this.information = rtlStore.information;
this.authSettings = rtlStore.authSettings;
this.flgLoading[0] = (undefined !== this.information.identity_pubkey) ? false : true;
if (window.innerWidth <= 768) {
this.settings.menu = 'Vertical';
this.settings.flgSidenavOpened = false;
this.settings.flgSidenavPinned = false;
}
if (window.innerWidth <= 414) {
this.smallScreen = true;
}
this.logger.info(this.settings);
if (!sessionStorage.getItem('token')) {
this.flgLoading[0] = false;
}
});
if (sessionStorage.getItem('token')) {
this.store.dispatch(new RTLActions.FetchInfo());
}
this.actions$
.pipe(
takeUntil(this.unsubs[1]),
filter((action) => action.type === RTLActions.INIT_APP_DATA || action.type === RTLActions.SET_SETTINGS || action.type === RTLActions.SET_AUTH_SETTINGS)
).subscribe((actionPayload: (RTLActions.InitAppData | RTLActions.SetSettings | RTLActions.SetAuthSettings)) => {
if (actionPayload.type === RTLActions.SET_AUTH_SETTINGS) {
if (!sessionStorage.getItem('token')) {
if (+actionPayload.payload.rtlSSO) {
this.store.dispatch(new RTLActions.Signin(window.btoa(this.accessKey)));
} else {
this.router.navigate([this.authSettings.logoutRedirectLink]);
}
}
} else if (actionPayload.type === RTLActions.INIT_APP_DATA) {
this.store.dispatch(new RTLActions.FetchInfo());
} else if (actionPayload.type === RTLActions.SET_SETTINGS) {
if (this.settings.menu === 'Horizontal') {
this.settingSidenav.toggle(); // To dynamically update the width to 100% after side nav is closed
setTimeout(() => { this.settingSidenav.toggle(); }, 100);
}
}
});
this.actions$
.pipe(
takeUntil(this.unsubs[1]),
filter((action) => action.type === RTLActions.SET_INFO)
).subscribe((infoData: RTLActions.SetInfo) => {
if (undefined !== infoData.payload.identity_pubkey) {
this.initializeRemainingData();
}
});
this.userIdle.startWatching();
this.userIdle.onTimerStart().subscribe(count => {});
this.userIdle.onTimeout().subscribe(() => {
if (sessionStorage.getItem('token')) {
this.logger.warn('Time limit exceeded for session inactivity! Logging out!');
this.store.dispatch(new RTLActions.OpenAlert({ width: '75%', data: {
type: 'WARN',
titleMessage: 'Time limit exceeded for session inactivity! Logging out!'
}}));
this.store.dispatch(new RTLActions.Signout());
this.userIdle.resetTimer();
}
});
}
private readAccessKey() {
const url = window.location.href;
return url.substring(url.lastIndexOf('/') + 1);
}
initializeRemainingData() {
this.store.dispatch(new RTLActions.FetchPeers());
this.store.dispatch(new RTLActions.FetchBalance('channels'));
this.store.dispatch(new RTLActions.FetchFees());
this.store.dispatch(new RTLActions.FetchNetwork());
this.store.dispatch(new RTLActions.FetchChannels({routeParam: 'all', channelStatus: ''}));
this.store.dispatch(new RTLActions.FetchChannels({routeParam: 'pending', channelStatus: ''}));
this.store.dispatch(new RTLActions.FetchInvoices());
this.store.dispatch(new RTLActions.FetchPayments());
}
ngAfterViewInit() {
if (!this.settings.flgSidenavPinned) {
this.sideNavigation.close();
this.settingSidenav.toggle();
}
if (window.innerWidth <= 768) {
this.sideNavigation.close();
this.settingSidenav.toggle();
}
}
@HostListener('window:resize')
public onWindowResize(): void {
if (window.innerWidth <= 768) {
this.settings.menu = 'Vertical';
this.settings.flgSidenavOpened = false;
this.settings.flgSidenavPinned = false;
}
}
sideNavToggle() {
this.sideNavigation.toggle();
}
onNavigationClicked(event: any) {
if (window.innerWidth <= 414) {
this.sideNavigation.close();
}
}
copiedText(payload) {
this.flgCopied = true;
setTimeout(() => {this.flgCopied = false; }, 5000);
this.logger.info('Copied Text: ' + payload);
}
ngOnDestroy() {
this.unsubs.forEach(unsub => {
unsub.next();
unsub.complete();
});
}
}