src/modules/session-ui/pages/preloading/preloading-page.component.ts
Preloading Page smart component.
This page will be shown when the application starts and will fetch the user profile (via the StarkUserService) to perform the login of the user. It will redirect to the target page (via the StarkRoutingService) as soon as the user profile is loaded and logged in.
OnInit
changeDetection | ChangeDetectionStrategy.OnPush |
encapsulation | ViewEncapsulation.None |
host | { |
selector | stark-preloading-page |
templateUrl | ./preloading-page.component.html |
Properties |
|
Methods |
Inputs |
Public
constructor(logger: StarkLoggingService, userService: StarkUserService, sessionService: StarkSessionService, routingService: StarkRoutingService)
|
||||||||||||||||||||
Class constructor
Parameters :
|
targetState | |
Type : string
|
|
Target page to navigate to after the user profile is loaded and automatically logged in. |
targetStateParams | |
Type : RawParams
|
|
Params to pass to the target page (if any). |
Public ngOnInit |
ngOnInit()
|
Component lifecycle hook
Returns :
void
|
Public reload |
reload()
|
Reload the page through the routingService.
Returns :
void
|
Public correlationId |
Type : string
|
Default value : "undefined"
|
The current correlation Id of the application (useful when troubleshooting errors). |
Public logger |
Type : StarkLoggingService
|
Decorators :
@Inject(STARK_LOGGING_SERVICE)
|
- The `StarkLoggingService` instance of the application.
|
Public routingService |
Type : StarkRoutingService
|
Decorators :
@Inject(STARK_ROUTING_SERVICE)
|
- The `StarkRoutingService` instance of the application.
|
Public sessionService |
Type : StarkSessionService
|
Decorators :
@Inject(STARK_SESSION_SERVICE)
|
- The `StarkSessionService` instance of the application.
|
Public userFetchingFailed |
Default value : false
|
Whether the fetching of the user profile failed |
Public userService |
Type : StarkUserService
|
Decorators :
@Inject(STARK_USER_SERVICE)
|
- The `StarkUserService` instance of the application.
|
import { ChangeDetectionStrategy, Component, Inject, Input, OnInit, ViewEncapsulation } from "@angular/core";
import { RawParams } from "@uirouter/core";
import { delay, take } from "rxjs/operators";
import {
STARK_LOGGING_SERVICE,
STARK_ROUTING_SERVICE,
STARK_SESSION_SERVICE,
STARK_USER_SERVICE,
StarkLoggingService,
StarkRoutingService,
StarkSessionService,
StarkUser,
StarkUserService
} from "@nationalbankbelgium/stark-core";
/**
* @ignore
*/
const componentName = "stark-preloading-page";
/**
* Preloading Page smart component.
*
* This page will be shown when the application starts and will fetch the user profile (via the {@link StarkUserService}) to perform the login of the user.
* It will redirect to the target page (via the {@link StarkRoutingService}) as soon as the user profile is loaded and logged in.
*/
@Component({
selector: "stark-preloading-page",
templateUrl: "./preloading-page.component.html",
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
class: componentName
}
})
export class StarkPreloadingPageComponent implements OnInit {
/**
* Target page to navigate to after the user profile is loaded and automatically logged in.
*/
@Input()
public targetState?: string;
/**
* Params to pass to the target page (if any).
*/
@Input()
public targetStateParams?: RawParams;
/**
* Whether the fetching of the user profile failed
*/
public userFetchingFailed = false;
/**
* The current correlation Id of the application (useful when troubleshooting errors).
*/
public correlationId = "undefined";
/**
* @ignore
*/
public loginDelay = 200;
/**
* Class constructor
* @param logger - The `StarkLoggingService` instance of the application.
* @param userService - The `StarkUserService` instance of the application.
* @param sessionService - The `StarkSessionService` instance of the application.
* @param routingService - The `StarkRoutingService` instance of the application.
*/
public constructor(
@Inject(STARK_LOGGING_SERVICE) public logger: StarkLoggingService,
@Inject(STARK_USER_SERVICE) public userService: StarkUserService,
@Inject(STARK_SESSION_SERVICE) public sessionService: StarkSessionService,
@Inject(STARK_ROUTING_SERVICE) public routingService: StarkRoutingService
) {}
/**
* Component lifecycle hook
*/
public ngOnInit(): void {
// the result is delayed for some milliseconds,
// otherwise the page will show an ugly flickering (if the profile is fetched immediately)
this.userService
.fetchUserProfile()
.pipe(
take(1), // this ensures that the observable will be automatically unsubscribed after emitting the value
delay(this.loginDelay)
)
.subscribe(
(user: StarkUser) => {
this.sessionService.login(user);
if (this.targetState) {
this.routingService.navigateTo(this.targetState, this.targetStateParams);
} else {
this.routingService.navigateToHome();
}
},
() => {
this.correlationId = this.logger.correlationId;
this.userFetchingFailed = true;
}
);
this.logger.debug(componentName + ": component initialized");
}
/**
* Reload the page through the routingService.
*/
public reload(): void {
this.routingService.reload();
}
}
<stark-session-card
[cardTitle]="!!userFetchingFailed ? 'STARK.PRELOADING.FETCHING_USER_PROFILE_FAILURE' : ''"
aria-busy="true"
aria-live="polite"
>
<ng-container *ngIf="!userFetchingFailed">
<div class="stark-loading-icon"></div>
<h1 translate>STARK.PRELOADING.FETCHING_USER_PROFILE</h1>
</ng-container>
<ng-container *ngIf="userFetchingFailed">
<h2 translate>STARK.PRELOADING.CONTACT_IT_SUPPORT</h2>
<h3><span translate>STARK.PRELOADING.CORRELATION_ID</span>: {{ correlationId }}</h3>
<button mat-button mat-raised-button color="primary" (click)="reload()" [attr.aria-label]="'STARK.PRELOADING.RELOAD' | translate">
<span translate>STARK.PRELOADING.RELOAD</span>
</button>
</ng-container>
</stark-session-card>