Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const routes: Routes = [
children: feedRoutes,
data: {feedType: 'jobs'}
},
{
path: 'weekly',
children: feedRoutes,
data: {feedType: 'weekly'}
},
{path: 'item', loadChildren: () => import('./item-details/item-details.module').then(m => m.ItemDetailsModule)},
{path: 'user', loadChildren: () => import('./user/user.module').then(m => m.UserModule)}
];
Expand Down
2 changes: 2 additions & 0 deletions src/app/core/header/header.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<a routerLink="/ask/1" routerLinkActive="active" (click)="scrollTop()">ask</a>
|
<a routerLink="/jobs/1" routerLinkActive="active" (click)="scrollTop()">jobs</a>
|
<a routerLink="/weekly/1" routerLinkActive="active" (click)="scrollTop()">weekly</a>
</span>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/app/feeds/feed/feed.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
These are jobs at startups that were funded by Y Combinator.
You can also get a job at a YC startup through <a href="https://triplebyte.com/?ref=yc_jobs">Triplebyte</a>.
</p>
<p class="job-header" *ngIf="feedType === 'weekly'">Top stories from the past week, ranked by points.</p>
<ol *ngIf="feedType !== 'new'" [class.list-margin]="feedType !== 'jobs'" start="{{ listStart }}">
<li *ngFor="let item of items" class="post">
<item class="item-block" [item]="item"></item>
</li>
</ol>
<div class="nav">
<div class="nav" *ngIf="feedType !== 'weekly'">
<a *ngIf="listStart !== 1" [routerLink]="['/' + feedType, pageNum - 1]" routerLinkActive="active" class="prev">
‹ Prev
</a>
Expand Down
24 changes: 13 additions & 11 deletions src/app/feeds/feed/feed.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class FeedComponent implements OnInit {
errorMessage = '';

constructor(
private _hackerNewsAPIService: HackerNewsAPIService,
private hackerNewsAPIService: HackerNewsAPIService,
private route: ActivatedRoute
) { }

Expand All @@ -34,16 +34,18 @@ export class FeedComponent implements OnInit {
});

this.pageSub = this.route.params.subscribe(params => {
this.pageNum = params['page'] ? +params['page'] : 1;
this._hackerNewsAPIService.fetchFeed(this.feedType, this.pageNum)
.subscribe(
items => this.items = items,
error => this.errorMessage = 'Could not load ' + this.feedType + ' stories.',
() => {
this.listStart = ((this.pageNum - 1) * 30) + 1;
window.scrollTo(0, 0);
}
);
this.pageNum = params.page ? +params.page : 1;
const feed$ = this.feedType === 'weekly'
? this.hackerNewsAPIService.fetchWeeklyTop()
: this.hackerNewsAPIService.fetchFeed(this.feedType, this.pageNum);
feed$.subscribe(
items => this.items = items,
error => this.errorMessage = 'Could not load ' + this.feedType + ' stories.',
() => {
this.listStart = ((this.pageNum - 1) * 30) + 1;
window.scrollTo(0, 0);
}
);
});
}
}
14 changes: 12 additions & 2 deletions src/app/shared/services/hackernews-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { forkJoin } from 'rxjs';
import fetch from 'unfetch';
import {map } from 'rxjs/operators';

Expand All @@ -20,10 +21,20 @@ export class HackerNewsAPIService {
return lazyFetch(`${this.baseUrl}/${feedType}?page=${page}`);
}

fetchWeeklyTop(): Observable<Story[]> {
const pages = [1, 2, 3, 4, 5].map(page => lazyFetch<Story[]>(`${this.baseUrl}/news?page=${page}`));
const weekAgo = Date.now() / 1000 - 7 * 24 * 3600;
return forkJoin(pages).pipe(
map(results => ([] as Story[]).concat(...results)
.filter(story => story.time >= weekAgo)
.sort((a, b) => b.points - a.points))
);
}

fetchItemContent(id: number): Observable<Story> {
return lazyFetch(`${this.baseUrl}/item/${id}`).pipe(map((story: Story) => {
if (story.type === 'poll') {
let numberOfPollOptions = story.poll.length;
const numberOfPollOptions = story.poll.length;
story.poll_votes_count = 0;
for (let i = 1; i <= numberOfPollOptions; i++) {
this.fetchPollContent(story.id + i).subscribe(pollResults => {
Expand Down Expand Up @@ -63,4 +74,3 @@ function lazyFetch<T>(url, options?) {
};
});
}