@@ -5,6 +5,8 @@ import { TranslateService } from '@ngx-translate/core';
55import { hasValue } from '../empty.util' ;
66import { environment } from '../../../environments/environment' ;
77import { AlertType } from '../alert/alert-type' ;
8+ import { Router } from '@angular/router' ;
9+ import { Location } from '@angular/common' ;
810
911enum MessageType {
1012 LOADING = 'loading' ,
@@ -19,6 +21,8 @@ enum MessageType {
1921} )
2022export class LoadingComponent implements OnDestroy , OnInit {
2123
24+ readonly QUERY_PARAM_RELOAD_COUNT = 'reloadCount' ;
25+
2226 @Input ( ) message : string ;
2327 @Input ( ) showMessage = true ;
2428
@@ -27,6 +31,10 @@ export class LoadingComponent implements OnDestroy, OnInit {
2731 @Input ( ) warningMessageDelay = environment . loader . warningMessageDelay ;
2832 @Input ( ) errorMessage : string ;
2933 @Input ( ) errorMessageDelay = environment . loader . errorMessageDelay ;
34+ errorTimeoutWithRetriesDelay = environment . loader . errorMessageDelay ;
35+
36+
37+ @Input ( ) numberOfAutomaticPageReloads = environment . loader . numberOfAutomaticPageReloads || 0 ;
3038
3139 /**
3240 * Show a more compact spinner animation instead of the default one
@@ -39,30 +47,70 @@ export class LoadingComponent implements OnDestroy, OnInit {
3947 warningTimeout : any ;
4048 errorTimeout : any ;
4149
50+ pageReloadCount = 0 ;
51+
4252 readonly AlertTypeEnum = AlertType ;
4353
44- constructor ( private translate : TranslateService , private changeDetectorRef : ChangeDetectorRef ) {
54+ constructor (
55+ private router : Router ,
56+ private location : Location ,
57+ private translate : TranslateService ,
58+ private changeDetectorRef : ChangeDetectorRef ) {
4559
4660 }
4761
4862 ngOnInit ( ) {
63+ // saving current url and query params for the upcoming router trick
64+ let currentUrl = this . router . url . split ( '?' ) [ 0 ] ;
65+ const queryParams = new URLSearchParams ( this . router . url . split ( '?' ) [ 1 ] ) ;
66+
67+ // get reload count from state
68+ const reloadCount = ( this . location . getState ( ) as any ) ?. [ this . QUERY_PARAM_RELOAD_COUNT ] ;
69+ if ( hasValue ( reloadCount ) ) {
70+ this . pageReloadCount = + reloadCount ;
71+ }
72+
73+ // calculate the delay for the error message with retries
74+ this . errorTimeoutWithRetriesDelay = this . errorMessageDelay + this . pageReloadCount * ( this . errorMessageDelay - this . warningMessageDelay ) ;
75+
4976 if ( this . showMessage ) {
5077 this . message = this . message || this . translate . instant ( 'loading.default' ) ;
5178 }
79+
5280 if ( this . showFallbackMessages ) {
5381 this . warningMessage = this . warningMessage || this . translate . instant ( 'loading.warning' ) ;
5482 this . errorMessage = this . errorMessage || this . translate . instant ( 'loading.error' ) ;
83+
5584 if ( this . warningMessageDelay > 0 ) {
5685 this . warningTimeout = setTimeout ( ( ) => {
5786 this . messageToShow = MessageType . WARNING ;
5887 this . changeDetectorRef . detectChanges ( ) ;
5988 } , this . warningMessageDelay ) ;
6089 }
90+
6191 if ( this . errorMessageDelay > 0 ) {
6292 this . errorTimeout = setTimeout ( ( ) => {
63- this . messageToShow = MessageType . ERROR ;
64- this . changeDetectorRef . detectChanges ( ) ;
65- } , this . errorMessageDelay ) ;
93+ // if the page has been reloaded less than the maximum number of retries
94+ if ( this . pageReloadCount < this . numberOfAutomaticPageReloads ) {
95+ this . pageReloadCount ++ ;
96+ // navigate to a fake url to trigger a reload of the current page
97+ // this is needed because the router does not reload the page if the url is the same,
98+ // even if the state changes and the onSameUrlNavigation property is set to 'reload'
99+ this . router . navigate ( [ '/fake-url' ] , { queryParams, queryParamsHandling : 'merge' , skipLocationChange : true } ) . then ( ( ) => {
100+ // navigate back to the current url
101+ this . router . navigate ( [ currentUrl ] , {
102+ queryParams,
103+ queryParamsHandling : 'merge' ,
104+ onSameUrlNavigation : 'reload' ,
105+ state : { [ this . QUERY_PARAM_RELOAD_COUNT ] : this . pageReloadCount }
106+ } ) ;
107+ } ) ;
108+ } else {
109+ // if the page has been reloaded the maximum number of retries
110+ this . messageToShow = MessageType . ERROR ;
111+ this . changeDetectorRef . detectChanges ( ) ;
112+ }
113+ } , this . errorTimeoutWithRetriesDelay ) ;
66114 }
67115 }
68116 }
0 commit comments