app.component.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { Component, HostListener, OnInit } from '@angular/core';
  2. import { Router, RouterModule, RouterOutlet, RoutesRecognized } from '@angular/router';
  3. import { MatModule } from '../dependencies/angularlib/mat.module';
  4. import { Angularlib } from 'angularlib/angularlib.module';
  5. import { BaseComponent, untilDestroy } from 'angularlib/base.component';
  6. import { Subject, filter, map, repeat, takeUntil, timer } from 'rxjs';
  7. import { Title } from '@angular/platform-browser';
  8. import { LoginService } from 'angularlib/login/login.service';
  9. import { CommonModule, DatePipe } from '@angular/common';
  10. import { ChangeLanguage } from 'angularlib/labels/label.actions';
  11. import { ComponentService } from 'angularlib/component.service';
  12. import { NotificationModule } from 'angularlib/notification/notification.module';
  13. import { NOTIFICATION_STATE_TOKEN, NotificationState } from 'angularlib/notification/notification.state';
  14. import { Notification } from 'angularlib/notification/notification.actions';
  15. import { generateId } from 'angularlib/base.service';
  16. import { Store } from '@ngxs/store';
  17. import config from '../config/config.json';
  18. @Component({
  19. selector: 'app-root',
  20. standalone: true,
  21. imports: [
  22. CommonModule,
  23. RouterOutlet,
  24. MatModule,
  25. RouterModule,
  26. Angularlib,
  27. NotificationModule
  28. ],
  29. providers:[DatePipe],
  30. templateUrl: './app.component.html',
  31. styleUrls: [
  32. './app.component.scss'
  33. ]
  34. })
  35. export class AppComponent extends BaseComponent implements OnInit {
  36. title = 'Financial Information System';
  37. /**current date */
  38. protected currentDate = new Date();
  39. /**session timeout duration in milliseconds*/
  40. private duration: number = config?.sessionTimeoutDuration;
  41. private startTimeout$ = new Subject();
  42. private stopTimeout$ = new Subject();
  43. private timeout = timer(this.duration).pipe(
  44. map(() => {
  45. if(this.loginService.user)this.loginService.logout();console.warn('session inactive timeout, logging out...');
  46. }),
  47. takeUntil(this.stopTimeout$),
  48. repeat({delay:() => this.startTimeout$})
  49. );
  50. /**current theme of application */
  51. protected theme;
  52. /**number of notifications */
  53. protected notificationCount: number = 0;
  54. constructor(
  55. private router: Router,
  56. private store: Store,
  57. protected loginService: LoginService,
  58. /**Platform Browser title */
  59. protected pbTitle: Title,
  60. protected cs: ComponentService
  61. ) {
  62. super(store,cs);
  63. }
  64. @HostListener('window:mousedown')
  65. private refreshTimeout() {
  66. this.stopTimeout$.next(null);
  67. this.startTimeout$.next(null);
  68. }
  69. ngOnInit(): void {
  70. this.router.events.pipe(untilDestroy(this),
  71. filter((event) => event instanceof RoutesRecognized),
  72. map((res:any) => {
  73. const data = res.state.root.firstChild.firstChild || res.state.root.firstChild;
  74. return data.data?.title;
  75. })
  76. ).subscribe(title => {
  77. if (typeof title === 'string')
  78. this.title = title;
  79. else this.title = this.getLabel(title?.key,title?.default);
  80. this.pbTitle.setTitle(this.title);
  81. });
  82. /**subsribe user changes and initiate timeout timer */
  83. this.loginService.user$.pipe(untilDestroy(this)).subscribe(user => {
  84. if (user) {
  85. this.timeout.pipe(untilDestroy(this),takeUntil(this.loginService.loggedOut)).subscribe();
  86. }
  87. });
  88. this.store.select(NOTIFICATION_STATE_TOKEN).pipe(untilDestroy(this)).subscribe({
  89. next: (state:any) => {
  90. this.notificationCount = state.notifications.length;
  91. }
  92. });
  93. }
  94. /**
  95. * change application language
  96. * @param {string} language
  97. */
  98. protected changeLanguage(language: string) {
  99. this.store.dispatch(new ChangeLanguage(language));
  100. }
  101. protected addNotification() {
  102. this.store.dispatch(new Notification.Add({
  103. message:{title:generateId(),desc:generateId(),timestamp: new Date()}
  104. }));
  105. }
  106. }