Skip to content

Commit 3e6fdd5

Browse files
FrancescoMolinarovins01-4science
authored andcommitted
Merged in task/dspace-cris-2024_02_x/DSC-2556 (pull request DSpace#3706)
[DSC-2556] prevent double notification for eula page Approved-by: Andrea Barbasso
2 parents 0f40279 + ac67365 commit 3e6fdd5

2 files changed

Lines changed: 50 additions & 5 deletions

File tree

src/app/info/end-user-agreement/end-user-agreement.component.spec.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { NO_ERRORS_SCHEMA } from '@angular/core';
1+
import {
2+
NO_ERRORS_SCHEMA,
3+
PLATFORM_ID,
4+
} from '@angular/core';
25
import {
36
ComponentFixture,
47
TestBed,
@@ -74,6 +77,7 @@ describe('EndUserAgreementComponent', () => {
7477
{ provide: Store, useValue: store },
7578
{ provide: Router, useValue: router },
7679
{ provide: ActivatedRoute, useValue: route },
80+
{ provide: PLATFORM_ID, useValue: 'browser' },
7781
],
7882
schemas: [NO_ERRORS_SCHEMA],
7983
})
@@ -198,4 +202,37 @@ describe('EndUserAgreementComponent', () => {
198202
});
199203
});
200204
});
205+
206+
describe('warning notification logic', () => {
207+
beforeEach(() => {
208+
(endUserAgreementService.hasCurrentUserOrCookieAcceptedAgreement as jasmine.Spy)
209+
.and.returnValue(observableOf(false));
210+
fixture.detectChanges();
211+
});
212+
213+
it('should show warning once when not accepted and in browser', () => {
214+
component.ngOnInit();
215+
expect(notificationsService.warning).toHaveBeenCalledTimes(1);
216+
component.initAccepted(); // call again
217+
expect(notificationsService.warning).toHaveBeenCalledTimes(1);
218+
});
219+
220+
it('should not show warning when already accepted', () => {
221+
component.ngOnInit();
222+
(notificationsService.warning as jasmine.Spy).calls.reset();
223+
(endUserAgreementService.hasCurrentUserOrCookieAcceptedAgreement as jasmine.Spy)
224+
.and.returnValue(observableOf(true));
225+
component.initAccepted();
226+
expect(notificationsService.warning).not.toHaveBeenCalled();
227+
});
228+
229+
it('should not show warning when not in browser', () => {
230+
(component as any).platformId = 'server';
231+
fixture.detectChanges();
232+
(notificationsService.warning as jasmine.Spy).calls.reset();
233+
component.ngOnInit();
234+
component.initAccepted();
235+
expect(notificationsService.warning).toHaveBeenCalledTimes(0);
236+
});
237+
});
201238
});

src/app/info/end-user-agreement/end-user-agreement.component.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { NgIf } from '@angular/common';
1+
import {
2+
isPlatformBrowser,
3+
NgIf,
4+
} from '@angular/common';
25
import {
36
Component,
7+
Inject,
48
OnDestroy,
59
OnInit,
10+
PLATFORM_ID,
611
} from '@angular/core';
712
import { FormsModule } from '@angular/forms';
813
import {
@@ -59,14 +64,16 @@ export class EndUserAgreementComponent implements OnInit, OnDestroy {
5964
alreadyAccepted = false;
6065

6166
private subscription: Subscription = new Subscription();
67+
private notifiedOnce = false;
6268

6369
constructor(protected endUserAgreementService: EndUserAgreementService,
6470
protected notificationsService: NotificationsService,
6571
protected translate: TranslateService,
6672
protected authService: AuthService,
6773
protected store: Store<AppState>,
6874
protected router: Router,
69-
protected route: ActivatedRoute) {
75+
protected route: ActivatedRoute,
76+
@Inject(PLATFORM_ID) private platformId: string) {
7077
}
7178

7279
/**
@@ -89,8 +96,9 @@ export class EndUserAgreementComponent implements OnInit, OnDestroy {
8996
.subscribe(([accepted, authorized]) => {
9097
if (authorized) {
9198

92-
if (!accepted) {
93-
this.notificationsService.warning(this.translate.instant('info.end-user-agreement.accept.warning'), {});
99+
if (!accepted && isPlatformBrowser(this.platformId) && !this.notifiedOnce) {
100+
this.notificationsService.warning(this.translate.instant('info.end-user-agreement.accept.warning'));
101+
this.notifiedOnce = true;
94102
}
95103
this.accepted = accepted;
96104
this.alreadyAccepted = accepted;

0 commit comments

Comments
 (0)