From 29fcf48b657c0b2060e9f9b08011a7a8807493bb Mon Sep 17 00:00:00 2001 From: guillermo2519 Date: Wed, 8 Apr 2026 13:58:56 -0600 Subject: [PATCH] Fix #5336: General system alert visible to users in real time without reloading the page. --- ...system-wide-alert-banner.component.spec.ts | 16 +++++++++-- .../system-wide-alert-banner.component.ts | 28 +++++++++++++------ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/app/system-wide-alert/alert-banner/system-wide-alert-banner.component.spec.ts b/src/app/system-wide-alert/alert-banner/system-wide-alert-banner.component.spec.ts index 9514bb5987d..c9297ce4fbe 100644 --- a/src/app/system-wide-alert/alert-banner/system-wide-alert-banner.component.spec.ts +++ b/src/app/system-wide-alert/alert-banner/system-wide-alert-banner.component.spec.ts @@ -7,6 +7,10 @@ import { waitForAsync, } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; +import { + NavigationEnd, + Router, +} from '@angular/router'; import { SystemWideAlertDataService } from '@dspace/core/data/system-wide-alert-data.service'; import { NotificationsService } from '@dspace/core/notification-system/notifications.service'; import { SystemWideAlert } from '@dspace/core/shared/system-wide-alert.model'; @@ -16,11 +20,11 @@ import { createSuccessfulRemoteDataObject$ } from '@dspace/core/utilities/remote import { TranslateModule } from '@ngx-translate/core'; import { utcToZonedTime } from 'date-fns-tz'; import { getTestScheduler } from 'jasmine-marbles'; +import { of } from 'rxjs'; import { TestScheduler } from 'rxjs/testing'; import { SystemWideAlertBannerComponent } from './system-wide-alert-banner.component'; - describe('SystemWideAlertBannerComponent', () => { let comp: SystemWideAlertBannerComponent; let fixture: ComponentFixture; @@ -48,11 +52,16 @@ describe('SystemWideAlertBannerComponent', () => { searchBy: createSuccessfulRemoteDataObject$(createPaginatedList([systemWideAlert])), }); + const routerStub = { + events: of(new NavigationEnd(0, '/test', '/test')), + }; + TestBed.configureTestingModule({ imports: [TranslateModule.forRoot(), SystemWideAlertBannerComponent], providers: [ { provide: SystemWideAlertDataService, useValue: systemWideAlertDataService }, { provide: NotificationsService, useValue: new NotificationsServiceStub() }, + { provide: Router, useValue: routerStub }, // 👈 AQUÍ ], }).compileComponents(); })); @@ -67,17 +76,20 @@ describe('SystemWideAlertBannerComponent', () => { it('should init the comp', () => { expect(comp).toBeTruthy(); }); + it('should set the time countdown parts in their respective behaviour subjects', fakeAsync(() => { spyOn(comp.countDownDays, 'next'); spyOn(comp.countDownHours, 'next'); spyOn(comp.countDownMinutes, 'next'); + comp.ngOnInit(); tick(2000); + expect(comp.countDownDays.next).toHaveBeenCalled(); expect(comp.countDownHours.next).toHaveBeenCalled(); expect(comp.countDownMinutes.next).toHaveBeenCalled(); - discardPeriodicTasks(); + discardPeriodicTasks(); })); }); diff --git a/src/app/system-wide-alert/alert-banner/system-wide-alert-banner.component.ts b/src/app/system-wide-alert/alert-banner/system-wide-alert-banner.component.ts index 2ead50be7ed..74b353ab189 100644 --- a/src/app/system-wide-alert/alert-banner/system-wide-alert-banner.component.ts +++ b/src/app/system-wide-alert/alert-banner/system-wide-alert-banner.component.ts @@ -9,6 +9,10 @@ import { OnInit, PLATFORM_ID, } from '@angular/core'; +import { + NavigationEnd, + Router, +} from '@angular/router'; import { PaginatedList } from '@dspace/core/data/paginated-list.model'; import { SystemWideAlertDataService } from '@dspace/core/data/system-wide-alert-data.service'; import { NotificationsService } from '@dspace/core/notification-system/notifications.service'; @@ -29,6 +33,7 @@ import { import { filter, map, + startWith, switchMap, } from 'rxjs/operators'; @@ -75,18 +80,25 @@ export class SystemWideAlertBannerComponent implements OnInit, OnDestroy { @Inject(PLATFORM_ID) protected platformId: any, protected systemWideAlertDataService: SystemWideAlertDataService, protected notificationsService: NotificationsService, + protected router: Router, ) { } ngOnInit() { - this.subscriptions.push(this.systemWideAlertDataService.searchBy('active').pipe( - getAllSucceededRemoteDataPayload(), - map((payload: PaginatedList) => payload.page), - filter((page) => isNotEmpty(page)), - map((page) => page[0]), - ).subscribe((alert: SystemWideAlert) => { - this.systemWideAlert$.next(alert); - })); + this.subscriptions.push( + this.router.events.pipe( + filter(event => event instanceof NavigationEnd), + startWith(null), + switchMap(() => + this.systemWideAlertDataService.searchBy('active', null, false, true), + ), + getAllSucceededRemoteDataPayload(), + map((payload: PaginatedList) => payload.page), + map((page) => isNotEmpty(page) ? page[0] : null), + ).subscribe((alert: SystemWideAlert) => { + this.systemWideAlert$.next(alert); + }), + ); this.subscriptions.push(this.systemWideAlert$.pipe( switchMap((alert: SystemWideAlert) => {