Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aio): enable data driven homepage announcements (#22043)
PR Close #22043
- Loading branch information
1 parent
aa456ed
commit fbef94a
Showing
7 changed files
with
219 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
[ | ||
{ | ||
"startDate": "2018-01-01", | ||
"endDate": "2018-02-02", | ||
"message": "Join us in Atlanta for ngATL<br/>Jan 30 - Feb 2, 2018", | ||
"imageUrl": "generated/images/marketing/home/ng-atl.png", | ||
"linkUrl": "http://ng-atl.org/" | ||
} | ||
] |
This file contains 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 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 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
109 changes: 109 additions & 0 deletions
109
aio/src/app/embedded/announcement-bar/announcement-bar.component.spec.ts
This file contains 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,109 @@ | ||
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { Logger } from 'app/shared/logger.service'; | ||
import { MockLogger } from 'testing/logger.service'; | ||
import { AnnouncementBarComponent } from './announcement-bar.component'; | ||
|
||
const today = new Date(); | ||
const lastWeek = changeDays(today, -7); | ||
const yesterday = changeDays(today, -1); | ||
const tomorrow = changeDays(today, 1); | ||
const nextWeek = changeDays(today, 7); | ||
|
||
describe('AnnouncementBarComponent', () => { | ||
|
||
let element: HTMLElement; | ||
let fixture: ComponentFixture<AnnouncementBarComponent>; | ||
let component: AnnouncementBarComponent; | ||
let httpMock: HttpTestingController; | ||
let mockLogger: MockLogger; | ||
|
||
beforeEach(() => { | ||
const injector = TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
declarations: [AnnouncementBarComponent], | ||
providers: [{ provide: Logger, useClass: MockLogger }] | ||
}); | ||
|
||
httpMock = injector.get(HttpTestingController); | ||
mockLogger = injector.get(Logger); | ||
fixture = TestBed.createComponent(AnnouncementBarComponent); | ||
component = fixture.componentInstance; | ||
element = fixture.nativeElement; | ||
}); | ||
|
||
it('should have no announcement when first created', () => { | ||
expect(component.announcement).toBeUndefined(); | ||
}); | ||
|
||
describe('ngOnInit', () => { | ||
it('should make a single request to the server', () => { | ||
component.ngOnInit(); | ||
httpMock.expectOne('generated/announcements.json'); | ||
}); | ||
|
||
it('should set the announcement to the first "live" one in the list loaded from `announcements.json`', () => { | ||
component.ngOnInit(); | ||
const request = httpMock.expectOne('generated/announcements.json'); | ||
request.flush([ | ||
{ startDate: lastWeek, endDate: yesterday, message: 'Test Announcement 0' }, | ||
{ startDate: tomorrow, endDate: nextWeek, message: 'Test Announcement 1' }, | ||
{ startDate: yesterday, endDate: tomorrow, message: 'Test Announcement 2' }, | ||
{ startDate: yesterday, endDate: tomorrow, message: 'Test Announcement 3' } | ||
]); | ||
expect(component.announcement.message).toEqual('Test Announcement 2'); | ||
}); | ||
|
||
it('should set the announcement to `undefined` if there are no announcements in `announcements.json`', () => { | ||
component.ngOnInit(); | ||
const request = httpMock.expectOne('generated/announcements.json'); | ||
request.flush([]); | ||
expect(component.announcement).toBeUndefined(); | ||
}); | ||
|
||
it('should handle invalid data in `announcements.json`', () => { | ||
component.ngOnInit(); | ||
const request = httpMock.expectOne('generated/announcements.json'); | ||
request.flush('some random response'); | ||
expect(component.announcement).toBeUndefined(); | ||
expect(mockLogger.output.error[0][0]).toContain('generated/announcements.json contains invalid data:'); | ||
}); | ||
|
||
it('should handle a failed request for `announcements.json`', () => { | ||
component.ngOnInit(); | ||
const request = httpMock.expectOne('generated/announcements.json'); | ||
request.error(new ErrorEvent('404')); | ||
expect(component.announcement).toBeUndefined(); | ||
expect(mockLogger.output.error[0][0]).toContain('generated/announcements.json request failed:'); | ||
}); | ||
}); | ||
|
||
describe('rendering', () => { | ||
beforeEach(() => { | ||
component.announcement = { | ||
imageUrl: 'link/to/image', | ||
linkUrl: 'link/to/website', | ||
message: 'this is an <b>important</b> message', | ||
endDate: '2018-03-01', | ||
startDate: '2018-02-01' | ||
}; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should display the message as HTML', () => { | ||
expect(element.innerHTML).toContain('this is an <b>important</b> message'); | ||
}); | ||
|
||
it('should display an image', () => { | ||
expect(element.querySelector('img')!.src).toContain('link/to/image'); | ||
}); | ||
|
||
it('should display a link', () => { | ||
expect(element.querySelector('a')!.href).toContain('link/to/website'); | ||
}); | ||
}); | ||
}); | ||
|
||
function changeDays(initial: Date, days: number) { | ||
return (new Date(initial.valueOf()).setDate(initial.getDate() + days)); | ||
} |
82 changes: 82 additions & 0 deletions
82
aio/src/app/embedded/announcement-bar/announcement-bar.component.ts
This file contains 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,82 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Logger } from 'app/shared/logger.service'; | ||
import { CONTENT_URL_PREFIX } from 'app/documents/document.service'; | ||
const announcementsPath = CONTENT_URL_PREFIX + 'announcements.json'; | ||
|
||
export interface Announcement { | ||
imageUrl: string; | ||
message: string; | ||
linkUrl: string; | ||
startDate: string; | ||
endDate: string; | ||
} | ||
|
||
/** | ||
* Display the latest live announcement. This is used on the homepage. | ||
* | ||
* The data for the announcements is kept in `aio/content/marketing/announcements.json`. | ||
* | ||
* The format for that data file looks like: | ||
* | ||
* ``` | ||
* [ | ||
* { | ||
* "startDate": "2018-02-01", | ||
* "endDate": "2018-03-01", | ||
* "message": "This is an <b>important</b> announcement", | ||
* "imageUrl": "url/to/image", | ||
* "linkUrl": "url/to/website" | ||
* }, | ||
* ... | ||
* ] | ||
* ``` | ||
* | ||
* Only one announcement will be shown at any time. This is determined as the first "live" | ||
* announcement in the file, where "live" means that its start date is before today, and its | ||
* end date is after today. | ||
* | ||
* **Security Note:** | ||
* The `message` field can contain unsanitized HTML but this field should only updated by | ||
* verified members of the Angular team. | ||
*/ | ||
@Component({ | ||
selector: 'aio-announcement-bar', | ||
template: ` | ||
<div class="homepage-container" *ngIf="announcement"> | ||
<div class="announcement-bar"> | ||
<img [src]="announcement.imageUrl"> | ||
<p [innerHTML]="announcement.message"></p> | ||
<a class="button" [href]="announcement.linkUrl">Learn More</a> | ||
</div> | ||
</div>` | ||
}) | ||
export class AnnouncementBarComponent implements OnInit { | ||
announcement: Announcement; | ||
|
||
constructor(private http: HttpClient, private logger: Logger) {} | ||
|
||
ngOnInit() { | ||
this.http.get<Announcement[]>(announcementsPath) | ||
.catch(error => { | ||
this.logger.error(`${announcementsPath} request failed: ${error.message}`); | ||
return []; | ||
}) | ||
.map(announcements => this.findCurrentAnnouncement(announcements)) | ||
.catch(error => { | ||
this.logger.error(`${announcementsPath} contains invalid data: ${error.message}`); | ||
return []; | ||
}) | ||
.subscribe(announcement => this.announcement = announcement); | ||
} | ||
|
||
/** | ||
* Get the first date in the list that is "live" now | ||
*/ | ||
private findCurrentAnnouncement(announcements: Announcement[]) { | ||
return announcements | ||
.filter(announcement => new Date(announcement.startDate).valueOf() < Date.now()) | ||
.filter(announcement => new Date(announcement.endDate).valueOf() > Date.now()) | ||
[0]; | ||
} | ||
} |
This file contains 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