-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathapp.component.ts
More file actions
91 lines (79 loc) · 3.43 KB
/
app.component.ts
File metadata and controls
91 lines (79 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { createDispatchMap, select } from '@ngxs/store';
import { switchMap, timer } from 'rxjs';
import { isPlatformBrowser } from '@angular/common';
import { ChangeDetectionStrategy, Component, DestroyRef, effect, inject, OnInit, PLATFORM_ID } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { NavigationCancel, NavigationEnd, NavigationError, ResolveStart, Router, RouterOutlet } from '@angular/router';
import { ENVIRONMENT } from '@core/provider/environment.provider';
import { GetCurrentUser } from '@core/store/user';
import { GetEmails, UserEmailsSelectors } from '@core/store/user-emails';
import { ConfirmEmailComponent } from './shared/components/confirm-email/confirm-email.component';
import { FullScreenLoaderComponent } from './shared/components/full-screen-loader/full-screen-loader.component';
import { ToastComponent } from './shared/components/toast/toast.component';
import { CustomDialogService } from './shared/services/custom-dialog.service';
import { LoaderService } from './shared/services/loader.service';
import { GoogleTagManagerService } from 'angular-google-tag-manager';
@Component({
selector: 'osf-root',
imports: [RouterOutlet, ToastComponent, FullScreenLoaderComponent],
templateUrl: './app.component.html',
styleUrl: './app.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent implements OnInit {
private readonly googleTagManagerService = inject(GoogleTagManagerService);
private readonly destroyRef = inject(DestroyRef);
private readonly customDialogService = inject(CustomDialogService);
private readonly router = inject(Router);
private readonly environment = inject(ENVIRONMENT);
private readonly actions = createDispatchMap({ getCurrentUser: GetCurrentUser, getEmails: GetEmails });
private readonly platformId = inject(PLATFORM_ID);
private readonly isBrowser = isPlatformBrowser(this.platformId);
private readonly loaderService = inject(LoaderService);
unverifiedEmails = select(UserEmailsSelectors.getUnverifiedEmails);
constructor() {
effect(() => {
if (this.unverifiedEmails().length) {
this.showEmailDialog();
}
});
}
ngOnInit(): void {
this.actions
.getCurrentUser()
.pipe(
takeUntilDestroyed(this.destroyRef),
switchMap(() => this.actions.getEmails())
)
.subscribe();
if (this.isBrowser) {
this.router.events.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((event) => {
if (event instanceof ResolveStart) {
this.loaderService.show();
} else if (
event instanceof NavigationEnd ||
event instanceof NavigationCancel ||
event instanceof NavigationError
) {
timer(500)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => this.loaderService.hide());
}
if (this.environment.googleTagManagerId && event instanceof NavigationEnd) {
this.googleTagManagerService.pushTag({
event: 'page',
pageName: event.urlAfterRedirects,
});
}
});
}
}
private showEmailDialog() {
const unverifiedEmailsData = this.unverifiedEmails();
this.customDialogService.open(ConfirmEmailComponent, {
header: unverifiedEmailsData[0].isMerge ? 'home.confirmEmail.merge.title' : 'home.confirmEmail.add.title',
width: '448px',
data: unverifiedEmailsData,
});
}
}