-
Notifications
You must be signed in to change notification settings - Fork 16
[ENG-8776] Created maintenance banner component #343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bp-cos
merged 21 commits into
CenterForOpenScience:main
from
bodintsov:feat/add-maintenance-banners
Sep 12, 2025
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4026407
Update develop from main (#322)
bp-cos 52c5896
Feat(8653): Implement view tracking for registrations and preprints (…
opaduchak 6ff10ff
[ENG-8624] feat(registries): add context to registration submission +…
adlius 0fed748
[ENG-8504] Show Osf introduction video and Collections,Institutions, …
mkovalua 5bbf4b8
Fix/develop conflicts (#332)
nsemets c75d7b7
[ENG-8639] add <meta> tags to files detail (#324)
aaxelb 4b9b2cb
feat(ci): separate linting from tests (#345)
aaxelb 74d04f8
Feat(datacite-tracker): implemented file view and download tracking …
opaduchak d371a91
Feat(ENG-8778): Implement Cookie consent message (#353)
opaduchak 4bebc78
[eng-8741] Added Sentry to the app (#340)
bp-cos 58bae1f
Fix/dev to main (#368)
nsemets c54da35
feat(maintenance-banner): create maintenance banner component
bodintsov b629eab
fix(maintenance-banner): fix import
bodintsov f6cfe2c
fix(maintenance-banner): Use PrimeNG for banner
bodintsov b445293
fix(maintenance-banner): Created service and model for Maintenance ba…
bodintsov 302910a
fix(maintenance-banner): Made banner show on all pages
bodintsov e72bc39
fix(maintenance-banner): Created new model, cleaned code
bodintsov 38b2bbe
Merge remote-tracking branch 'angular-osf/main' into feat/add-mainten…
bodintsov 061f81f
fix(maintenance-banner): Removed litter
bodintsov e7cb354
fix(maintenance-banner): Changed ApiURL, added type
bodintsov f64cecc
fix(maintenance-banner): Fix fetch when cookies
bodintsov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/app/core/components/maintenance-banner/maintenance-banner.component.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@if (maintenance && !dismissed) { | ||
<p-message | ||
styleClass="w-full" | ||
[severity]="maintenance.severity" | ||
[text]="maintenance.message" | ||
[closable]="true" | ||
(onClose)="dismiss()" | ||
class="block p-3" | ||
icon="pi pi-info-circle" | ||
> | ||
</p-message> | ||
} |
nsemets marked this conversation as resolved.
Show resolved
Hide resolved
|
Empty file.
150 changes: 150 additions & 0 deletions
150
src/app/core/components/maintenance-banner/maintenance-banner.component.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { CookieService } from 'ngx-cookie-service'; | ||
|
||
import { MessageModule } from 'primeng/message'; | ||
|
||
import { of } from 'rxjs'; | ||
|
||
import { HttpClient } from '@angular/common/http'; | ||
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
import { NoopAnimationsModule } from '@angular/platform-browser/animations'; | ||
|
||
import { MaintenanceBannerComponent } from './maintenance-banner.component'; | ||
|
||
describe('MaintenanceBannerComponent', () => { | ||
let fixture: ComponentFixture<MaintenanceBannerComponent>; | ||
let httpClient: { get: jest.Mock }; | ||
let cookieService: jest.Mocked<CookieService>; | ||
|
||
beforeEach(async () => { | ||
cookieService = { | ||
check: jest.fn(), | ||
set: jest.fn(), | ||
} as any; | ||
httpClient = { get: jest.fn() } as any; | ||
await TestBed.configureTestingModule({ | ||
imports: [MaintenanceBannerComponent, NoopAnimationsModule, MessageModule], | ||
providers: [ | ||
{ provide: CookieService, useValue: cookieService }, | ||
{ provide: HttpClient, useValue: httpClient }, | ||
], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(MaintenanceBannerComponent); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should render info banner when maintenance data is present', fakeAsync(() => { | ||
cookieService.check.mockReturnValue(false); | ||
const now = new Date(); | ||
const start = new Date(now.getTime() - 24 * 60 * 60 * 1000).toISOString(); | ||
const end = new Date(now.getTime() + 24 * 60 * 60 * 1000).toISOString(); | ||
httpClient.get.mockReturnValueOnce( | ||
of({ | ||
maintenance: { level: 1, message: 'Info message', start, end }, | ||
}) | ||
); | ||
fixture.detectChanges(); | ||
tick(); | ||
fixture.detectChanges(); | ||
const banner = fixture.debugElement.query(By.css('p-message')); | ||
expect(banner).toBeTruthy(); | ||
expect(banner.componentInstance.severity).toBe('info'); | ||
expect(banner.nativeElement.textContent).toContain('Info message'); | ||
})); | ||
|
||
it('should render warning banner when level is 2', fakeAsync(() => { | ||
cookieService.check.mockReturnValue(false); | ||
const now = new Date(); | ||
const start = new Date(now.getTime() - 24 * 60 * 60 * 1000).toISOString(); | ||
const end = new Date(now.getTime() + 24 * 60 * 60 * 1000).toISOString(); | ||
httpClient.get.mockReturnValueOnce( | ||
of({ | ||
maintenance: { | ||
level: 2, | ||
message: 'Warning message', | ||
start, | ||
end, | ||
}, | ||
}) | ||
); | ||
fixture.detectChanges(); | ||
tick(); | ||
fixture.detectChanges(); | ||
const banner = fixture.debugElement.query(By.css('p-message')); | ||
expect(banner).toBeTruthy(); | ||
expect(banner.componentInstance.severity).toBe('warn'); | ||
expect(banner.nativeElement.textContent).toContain('Warning message'); | ||
})); | ||
|
||
it('should render danger banner when level is 3', fakeAsync(() => { | ||
cookieService.check.mockReturnValue(false); | ||
const now = new Date(); | ||
const start = new Date(now.getTime() - 24 * 60 * 60 * 1000).toISOString(); | ||
const end = new Date(now.getTime() + 24 * 60 * 60 * 1000).toISOString(); | ||
httpClient.get.mockReturnValueOnce( | ||
of({ | ||
maintenance: { | ||
level: 3, | ||
message: 'Danger message', | ||
start, | ||
end, | ||
}, | ||
}) | ||
); | ||
fixture.detectChanges(); | ||
tick(); | ||
fixture.detectChanges(); | ||
const banner = fixture.debugElement.query(By.css('p-message')); | ||
expect(banner).toBeTruthy(); | ||
expect(banner.componentInstance.severity).toBe('error'); | ||
expect(banner.nativeElement.textContent).toContain('Danger message'); | ||
})); | ||
|
||
it('should not render banner if cookie is set', fakeAsync(() => { | ||
cookieService.check.mockReturnValue(true); | ||
fixture.detectChanges(); | ||
expect(httpClient.get).not.toHaveBeenCalled(); | ||
fixture.detectChanges(); | ||
const banner = fixture.debugElement.query(By.css('p-message')); | ||
expect(banner).toBeFalsy(); | ||
})); | ||
|
||
it('should not render banner if outside maintenance window', fakeAsync(() => { | ||
cookieService.check.mockReturnValue(false); | ||
httpClient.get.mockReturnValueOnce( | ||
of({ | ||
maintenance: { level: 1, message: 'Old message', start: '2020-01-01T00:00:00Z', end: '2020-01-02T00:00:00Z' }, | ||
}) | ||
); | ||
fixture.detectChanges(); | ||
tick(); | ||
fixture.detectChanges(); | ||
const banner = fixture.debugElement.query(By.css('p-message')); | ||
expect(banner).toBeFalsy(); | ||
})); | ||
|
||
it('should dismiss banner when close button is clicked', fakeAsync(() => { | ||
cookieService.check.mockReturnValue(false); | ||
const now = new Date(); | ||
const start = new Date(now.getTime() - 24 * 60 * 60 * 1000).toISOString(); | ||
const end = new Date(now.getTime() + 24 * 60 * 60 * 1000).toISOString(); | ||
httpClient.get.mockReturnValueOnce( | ||
of({ | ||
maintenance: { level: 1, message: 'Dismiss me', start, end }, | ||
}) | ||
); | ||
fixture.detectChanges(); | ||
tick(); | ||
fixture.detectChanges(); | ||
const banner = fixture.debugElement.query(By.css('p-message')); | ||
expect(banner).toBeTruthy(); | ||
banner.triggerEventHandler('onClose', {}); | ||
fixture.detectChanges(); | ||
expect(fixture.debugElement.query(By.css('p-message'))).toBeFalsy(); | ||
expect(cookieService.set).toHaveBeenCalled(); | ||
})); | ||
}); |
48 changes: 48 additions & 0 deletions
48
src/app/core/components/maintenance-banner/maintenance-banner.component.ts
nsemets marked this conversation as resolved.
Show resolved
Hide resolved
nsemets marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { CookieService } from 'ngx-cookie-service'; | ||
|
||
import { MessageModule } from 'primeng/message'; | ||
|
||
import { CommonModule } from '@angular/common'; | ||
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, inject, OnInit } from '@angular/core'; | ||
|
||
import { Maintenance } from '../../models/maintenance.model'; | ||
import { MaintenanceService } from '../../services/maintenance.service'; | ||
|
||
@Component({ | ||
selector: 'osf-maintenance-banner', | ||
imports: [CommonModule, MessageModule], | ||
templateUrl: './maintenance-banner.component.html', | ||
styleUrls: ['./maintenance-banner.component.scss'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class MaintenanceBannerComponent implements OnInit { | ||
private readonly maintenanceService = inject(MaintenanceService); | ||
private readonly cookies = inject(CookieService); | ||
private readonly cdr = inject(ChangeDetectorRef); | ||
|
||
dismissed = false; | ||
readonly cookieName = 'osf-maintenance-dismissed'; | ||
readonly cookieDurationHours = 24; | ||
|
||
maintenance: Maintenance | null = null; | ||
|
||
ngOnInit(): void { | ||
this.dismissed = this.cookies.check(this.cookieName); | ||
if (!this.dismissed) { | ||
this.fetchMaintenanceStatus(); | ||
} | ||
} | ||
|
||
private fetchMaintenanceStatus(): void { | ||
this.maintenanceService.fetchMaintenanceStatus().subscribe((maintenance) => { | ||
this.maintenance = maintenance; | ||
this.cdr.markForCheck(); | ||
}); | ||
} | ||
|
||
dismiss(): void { | ||
this.cookies.set(this.cookieName, '1', this.cookieDurationHours, '/'); | ||
this.dismissed = true; | ||
this.maintenance = null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface Maintenance { | ||
level: number; | ||
message: string; | ||
start: string; | ||
end: string; | ||
severity?: MaintenanceSeverity; | ||
} | ||
|
||
export type MaintenanceSeverity = 'info' | 'warn' | 'error'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { catchError, Observable, of } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
import { HttpClient } from '@angular/common/http'; | ||
import { inject, Injectable } from '@angular/core'; | ||
|
||
import { Maintenance, MaintenanceSeverity } from '../models/maintenance.model'; | ||
|
||
import { environment } from 'src/environments/environment'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class MaintenanceService { | ||
private readonly http = inject(HttpClient); | ||
private readonly apiUrl = `${environment.apiDomainUrl}/v2`; | ||
|
||
fetchMaintenanceStatus(): Observable<Maintenance | null> { | ||
return this.http.get<{ maintenance?: Maintenance }>(`${this.apiUrl}/status/`).pipe( | ||
map((data) => { | ||
const maintenance = data.maintenance; | ||
if (maintenance && this.isWithinMaintenanceWindow(maintenance)) { | ||
return { ...maintenance, severity: this.getSeverity(maintenance.level) }; | ||
} | ||
return null; | ||
}), | ||
catchError(() => of(null)) | ||
); | ||
} | ||
|
||
getSeverity(level: number): MaintenanceSeverity { | ||
const map: Record<number, MaintenanceSeverity> = { 1: 'info', 2: 'warn', 3: 'error' }; | ||
return map[level] ?? 'info'; | ||
} | ||
|
||
private isWithinMaintenanceWindow(maintenance: Maintenance): boolean { | ||
if (!maintenance.start || !maintenance.end) return false; | ||
const now = new Date(); | ||
return now >= new Date(maintenance.start) && now <= new Date(maintenance.end); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.