app.component.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { Component, HostListener, OnInit } from '@angular/core';
  2. import { ActivatedRoute, 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. import { UIAuthActions } from 'angularlib/login/state/login.actions';
  19. import { Platform } from '@angular/cdk/platform';
  20. @Component({
  21. selector: 'app-root',
  22. standalone: true,
  23. imports: [
  24. CommonModule,
  25. RouterOutlet,
  26. MatModule,
  27. RouterModule,
  28. Angularlib,
  29. NotificationModule
  30. ],
  31. providers:[DatePipe],
  32. templateUrl: './app.component.html',
  33. styleUrls: [
  34. './app.component.scss'
  35. ]
  36. })
  37. export class AppComponent extends BaseComponent implements OnInit {
  38. title = 'Financial Information System';
  39. /**current date */
  40. protected currentDate = new Date();
  41. /**session timeout duration in milliseconds*/
  42. private duration: number = config?.sessionTimeoutDuration;
  43. private startTimeout$ = new Subject();
  44. private stopTimeout$ = new Subject();
  45. private timeout = timer(this.duration).pipe(
  46. map(() => {
  47. if(this.loginService.user){
  48. console.warn('session inactive timeout, logging out...');
  49. this.store.dispatch(new UIAuthActions.RedirectAfterLogin(this.route.snapshot));
  50. this.loginService.logout();
  51. }
  52. }),
  53. takeUntil(this.stopTimeout$),
  54. repeat({delay:() => this.startTimeout$})
  55. );
  56. /**current theme of application */
  57. protected theme;
  58. /**number of notifications */
  59. protected notificationCount: number = 0;
  60. constructor(
  61. private router: Router,
  62. private store: Store,
  63. protected loginService: LoginService,
  64. /**Platform Browser title */
  65. protected pbTitle: Title,
  66. protected cs: ComponentService,
  67. private route: ActivatedRoute,
  68. private platform: Platform
  69. ) {
  70. super(store,cs);
  71. }
  72. @HostListener('window:mousedown')
  73. private refreshTimeout() {
  74. this.stopTimeout$.next(null);
  75. this.startTimeout$.next(null);
  76. }
  77. ngOnInit(): void {
  78. this.router.events.pipe(untilDestroy(this),
  79. filter((event) => event instanceof RoutesRecognized),
  80. map((res:any) => {
  81. const data = res.state.root.firstChild.firstChild || res.state.root.firstChild;
  82. return data.data?.title;
  83. })
  84. ).subscribe(title => {
  85. if (typeof title === 'string')
  86. this.title = title;
  87. else this.title = this.getLabel(title?.key,title?.default);
  88. this.pbTitle.setTitle(this.title);
  89. });
  90. /**subsribe user changes and initiate timeout timer */
  91. this.loginService.user$.pipe(untilDestroy(this)).subscribe(user => {
  92. if (user) {
  93. this.timeout.pipe(untilDestroy(this),takeUntil(this.loginService.loggedOut)).subscribe();
  94. }
  95. });
  96. this.store.select(NOTIFICATION_STATE_TOKEN).pipe(untilDestroy(this)).subscribe({
  97. next: (state:any) => {
  98. this.notificationCount = state.notifications.length;
  99. }
  100. });
  101. if (this.platform.ANDROID || this.platform.IOS) this.pwaPrompt();
  102. }
  103. /**
  104. * change application language
  105. * @param {string} language
  106. */
  107. protected changeLanguage(language: string) {
  108. this.store.dispatch(new ChangeLanguage(language));
  109. }
  110. protected addNotification() {
  111. this.store.dispatch(new Notification.Add({
  112. message:{title:generateId(),desc:generateId(),timestamp: new Date()}
  113. }));
  114. }
  115. /**prompt to install PWA App */
  116. private pwaPrompt() {
  117. let installPrompt = null;
  118. const installButton = document.querySelector("#installApp");
  119. const disableInAppInstallPrompt = () => {
  120. installPrompt = null;
  121. installButton.setAttribute("hidden","");
  122. }
  123. window.addEventListener("beforeinstallprompt",(event) => {
  124. event.preventDefault();
  125. installPrompt = event;
  126. installButton.removeAttribute("hidden");
  127. });
  128. installButton.addEventListener("click", async () => {
  129. if (!installPrompt) return;
  130. const result = await installPrompt.prompt();
  131. disableInAppInstallPrompt();
  132. });
  133. window.addEventListener("appinstalled", () => {
  134. disableInAppInstallPrompt();
  135. });
  136. }
  137. }