Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<SystemWideAlertBannerComponent>;
Expand Down Expand Up @@ -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();
}));
Expand All @@ -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();
}));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -29,6 +33,7 @@ import {
import {
filter,
map,
startWith,
switchMap,
} from 'rxjs/operators';

Expand Down Expand Up @@ -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<SystemWideAlert>) => 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),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to spam that endpoint a lot, instead I would only trigger it every certain amount of minutes. We could make this interval configurable as well similar to the auth.ui.timeUntilIdle configuration

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @alexandrevryghem, thanks for the feedback! I've addressed this by adding a throttleTime operator to limit how often the endpoint is called during navigation. The interval is now configurable via appConfig.systemWideAlert.refreshIntervalMs, similar to auth.ui.timeUntilIdle, with a default of 5 minutes.

startWith(null),
switchMap(() =>
this.systemWideAlertDataService.searchBy('active', null, false, true),
),
getAllSucceededRemoteDataPayload(),
map((payload: PaginatedList<SystemWideAlert>) => 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) => {
Expand Down
Loading