agmission/Development/client/src/app/shared/base/base.component.ts

141 lines
4.2 KiB
TypeScript

import { Component, OnDestroy, ChangeDetectorRef, HostListener } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, Subscription } from 'rxjs';
import { AppInjector } from '@app/app-injector';
import { AuthService } from '@app/domain/services/auth.service';
import { Store } from '@ngrx/store';
import cloneDeep from 'clone-deep';
import * as L from 'leaflet';
import { globals, locales } from '../global';
import { ConfirmationService } from 'primeng/api';
import { AppMessageService } from '../app-message.service';
import { AppActions } from '@app/app-actions';
import { GAService } from '../ga.service';
import { GAAnalyticsHelpersService } from '../ga.analytics-helpers.service';
import { AppConfigService } from '@app/domain/services/app-config.service';
import { IAppConfig } from '@app/domain/models/appconfig.model';
import { environment } from '@environments/environment';
@Component({ template: '' })
export class BaseComp implements OnDestroy {
readonly globals = globals;
protected appConf: AppConfigService;
settings: IAppConfig;
protected leaving: boolean = false;
protected authSvc: AuthService;
protected store: Store;
protected router: Router;
protected confirmSvc: ConfirmationService;
protected msgSvc: AppMessageService;
protected appActions: AppActions;
protected gaSvc: GAService;
protected gaHelpers: GAAnalyticsHelpersService;
sub$: Subscription = new Subscription();
locale;
canDeactivate(): Observable<boolean> | Promise<boolean> | boolean {
this.leaving = true;
return true;
}
get isDebug(): boolean {
return !environment.production;
}
get isMobile(): boolean {
return L.Browser.mobile;
}
get isClientUser(): boolean {
return this.authSvc != null && this.authSvc.isClientUser;
}
get isPilotUser(): boolean {
return this.authSvc != null && this.authSvc.isPilotUser;
}
get isPlanner(): boolean {
return this.authSvc != null && this.authSvc.isPlanner;
}
get isInspector(): boolean {
return this.authSvc != null && this.authSvc.isInspector;
}
get isApplicator(): boolean {
return this.authSvc != null && this.authSvc.isApplicator;
}
get isUS(): boolean {
return this.settings?.measureUnit;
}
/**
* @description cdRef must set from the leaf child component, using this because of Angular apply ChangeDetectorRef instance to a specific compoment instance
*/
constructor(protected readonly cdRef?: ChangeDetectorRef) {
const injector = AppInjector.getInjector();
this.authSvc = injector.get(AuthService);
this.store = injector.get(Store);
this.router = injector.get(Router);
this.confirmSvc = injector.get(ConfirmationService);
this.msgSvc = injector.get(AppMessageService);
this.appActions = injector.get(AppActions);
this.gaSvc = injector.get(GAService);
this.gaHelpers = injector.get(GAAnalyticsHelpersService);
this.locale = locales[this.authSvc.locale];
this.appConf = injector.get(AppConfigService);
this.settings = cloneDeep(this.appConf.settings);
}
@HostListener('window:beforeunload', ['$event'])
unloadNotification(event: any) {
// Check to skip reload confirmation in some components
if (this['name'] && ["LoginComp", "AppComp", "AppPasswordResetComp"].includes(this['name'])) {
return true;
}
else if (window['langChanged']) {
window['langChanged'] = false;
return true;
}
// Ask for confirmation (F5)
event.returnValue = true;
event.preventDefault();
return '';
}
ngOnDestroy(): void {
if (this.sub$) this.sub$.unsubscribe();
}
/**
* Convenience method to get standardized user role for GA4 analytics
* Uses the shared analytics helpers service
*/
protected getAnalyticsUserRole(): 'admin' | 'applicator' | 'office_admin' | 'client' | 'officer' | 'pilot' | 'inspector' | 'aircraft' {
return this.gaHelpers.getUserRole(this.authSvc.user?.roles || []);
}
/**
* Convenience method to get current user ID for analytics
*/
protected getAnalyticsUserId(): string {
return this.authSvc.user?._id || 'anonymous';
}
/**
* Convenience method to get platform value for analytics
*/
protected getAnalyticsPlatform(): 'web' | 'mobile' | 'api' {
return 'web';
}
}