app.component.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 { Store } from '@ngxs/store'
  5. import { Angularlib } from 'angularlib/angularlib.module';
  6. import { BaseComponent, untilDestroy } from 'angularlib/base.component';
  7. import { Subject, filter, map, repeat, takeUntil, timer } from 'rxjs';
  8. import { Title } from '@angular/platform-browser';
  9. import { LoginService } from 'angularlib/login/login.service';
  10. import { CommonModule, DatePipe } from '@angular/common';
  11. import { ChangeTheme } from 'angularlib/ui.state/ui.state.actions';
  12. import { ChangeLanguage } from 'angularlib/labels/label.actions';
  13. import { ComponentService } from 'angularlib/component.service';
  14. import { NotificationModule } from 'angularlib/notification/notification.module';
  15. import { NotificationState } from 'angularlib/notification/notification.state';
  16. import { Notification } from 'angularlib/notification/notification.actions';
  17. import { generateId } from 'angularlib/base.service';
  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 = 'FIS App';
  37. /**current date */
  38. protected currentDate = new Date();
  39. /**login timeout duration in milliseconds
  40. * @default 5 minutes
  41. */
  42. private duration: number = 300000;
  43. private startTimeout$ = new Subject();
  44. private stopTimeout$ = new Subject();
  45. private timeout = timer(this.duration).pipe(
  46. map(() => {if(this.loginService.user)this.loginService.logout();console.warn('session inactive timeout, logging out...');}),
  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.setDefaultTheme();
  71. this.router.events.pipe(untilDestroy(this),
  72. filter((event) => event instanceof RoutesRecognized),
  73. map((res:any) => {
  74. const data = res.state.root.firstChild.firstChild || res.state.root.firstChild;
  75. return data.data?.title;
  76. })
  77. ).subscribe(title => {
  78. if (typeof title === 'string')
  79. this.title = title;
  80. else this.title = this.getLabel(title.key,title.default);
  81. this.pbTitle.setTitle(this.title);
  82. });
  83. /**subsribe user changes and initiate timeout timer */
  84. this.loginService.user$.pipe(untilDestroy(this)).subscribe(user => {
  85. if (user) {
  86. this.timeout.pipe(untilDestroy(this),takeUntil(this.loginService.loggedOut)).subscribe();
  87. }
  88. });
  89. this.store.select(NotificationState).pipe(untilDestroy(this)).subscribe({
  90. next: (state:any) => {
  91. this.notificationCount = state.notifications.length;
  92. }
  93. });
  94. }
  95. /**
  96. * change application language
  97. * @param {string} language
  98. */
  99. protected changeLanguage(language: string) {
  100. this.store.dispatch(new ChangeLanguage(language));
  101. }
  102. protected setDefaultTheme() {
  103. if (localStorage.getItem('theme')) {
  104. this.theme = localStorage.getItem('theme') as string;
  105. const body = document.getElementsByTagName('body')[0];
  106. body.classList.add(this.theme);
  107. }
  108. }
  109. /**
  110. * change application theme
  111. * @param {string} theme theme name
  112. */
  113. protected changeTheme(theme: string) {
  114. const body = document.getElementsByTagName('body')[0];
  115. body.classList.remove(this.theme);
  116. this.theme = theme;
  117. body.classList.add(this.theme);
  118. localStorage.setItem('theme',this.theme);
  119. this.store.dispatch(new ChangeTheme(this.theme));
  120. }
  121. protected addNotification() {
  122. this.store.dispatch(new Notification.Add({
  123. message:{title:generateId(),desc:generateId(),timestamp: new Date()}
  124. }));
  125. }
  126. }