Skip to content

Commit 76089f5

Browse files
Andrea BarbassoFrancescoMolinaro
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-1665 (pull request DSpace#1640)
Task/dspace cris 2023 02 x/DSC-1665 Approved-by: Francesco Molinaro
2 parents 230cf23 + 2589c75 commit 76089f5

6 files changed

Lines changed: 74 additions & 5 deletions

File tree

src/app/shared/loading/loading.component.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { TranslateLoader, TranslateModule, TranslateService } from '@ngx-transla
77
import { TranslateLoaderMock } from '../mocks/translate-loader.mock';
88

99
import { LoadingComponent } from './loading.component';
10+
import { Router } from '@angular/router';
11+
import { RouterMock } from '../mocks/router.mock';
1012

1113
describe('LoadingComponent (inline template)', () => {
1214

@@ -26,8 +28,12 @@ describe('LoadingComponent (inline template)', () => {
2628
}),
2729
],
2830
declarations: [LoadingComponent], // declare the test component
29-
providers: [TranslateService]
31+
providers: [
32+
TranslateService,
33+
{provide: Router, useValue: new RouterMock()},
34+
]
3035
}).compileComponents(); // compile template and css
36+
3137
}));
3238

3339
beforeEach(() => {
@@ -75,4 +81,14 @@ describe('LoadingComponent (inline template)', () => {
7581
expect(de).toBeTruthy();
7682
});
7783

84+
it('should add time if the page has been automatically reloaded', () => {
85+
comp.pageReloadCount = 1;
86+
comp.errorMessageDelay = 1000;
87+
comp.warningMessageDelay = 500;
88+
comp.numberOfAutomaticPageReloads = 2;
89+
comp.ngOnInit();
90+
91+
expect(comp.errorTimeoutWithRetriesDelay).toBe(1500);
92+
});
93+
7894
});

src/app/shared/loading/loading.component.ts

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { TranslateService } from '@ngx-translate/core';
55
import { hasValue } from '../empty.util';
66
import { environment } from '../../../environments/environment';
77
import { AlertType } from '../alert/alert-type';
8+
import { Router } from '@angular/router';
9+
import { Location } from '@angular/common';
810

911
enum MessageType {
1012
LOADING = 'loading',
@@ -19,6 +21,8 @@ enum MessageType {
1921
})
2022
export 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
}

src/app/shared/loading/themed-loading.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class ThemedLoadingComponent extends ThemedComponent<LoadingComponent> {
1717
@Input() showMessage: boolean;
1818
@Input() spinner: boolean;
1919
@Input() showFallbackMessages: boolean;
20+
@Input() numberOfAutomaticPageReloads: number;
2021
@Input() warningMessage: string;
2122
@Input() warningMessageDelay: number;
2223
@Input() errorMessage: string;
@@ -27,6 +28,7 @@ export class ThemedLoadingComponent extends ThemedComponent<LoadingComponent> {
2728
'showMessage',
2829
'spinner',
2930
'showFallbackMessages',
31+
'numberOfAutomaticPageReloads',
3032
'warningMessage',
3133
'warningMessageDelay',
3234
'errorMessage',

src/config/default-app-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ export class DefaultAppConfig implements AppConfig {
795795
showFallbackMessagesByDefault: false,
796796
warningMessageDelay: 5000, // 5 seconds
797797
errorMessageDelay: 15000, // 15 seconds
798+
numberOfAutomaticPageReloads: 2,
798799
};
799800

800801
metaTags: MetaTagsConfig = {

src/config/loader-config.interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export interface LoaderConfig extends Config {
44
showFallbackMessagesByDefault: boolean;
55
warningMessageDelay: number;
66
errorMessageDelay: number;
7+
numberOfAutomaticPageReloads: number;
78
}

src/environments/environment.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ export const environment: BuildConfig = {
582582
showFallbackMessagesByDefault: true,
583583
warningMessageDelay: 1000,
584584
errorMessageDelay: 2000,
585+
numberOfAutomaticPageReloads: 2,
585586
},
586587

587588
metaTags: {

0 commit comments

Comments
 (0)