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
feat(aio): enable data driven homepage announcements #22043
Closed
petebacondarwin
wants to merge
2
commits into
angular:master
from
petebacondarwin:aio-announcement-bar
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
[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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ngATL banner will never be shown as it has expired, but I am leaving it here as an example.
I have not yet added the ng-conf banner because the logo doesn't look right.