Skip to content

Commit c391aea

Browse files
author
Clinton D'Annolfo
authored
Merge pull request #9 from matthiaskern/add-real-best-data
Add best feed type
2 parents 270fe41 + 24fa28d commit c391aea

File tree

6 files changed

+59
-10
lines changed

6 files changed

+59
-10
lines changed

src/components/presentational/HeaderNav.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ const HeaderNav = props => (
4343
<Link prefetch href="/submit">
4444
<a className={props.currentURL === '/submit' ? 'topsel' : ''}>submit</a>
4545
</Link>
46+
{
47+
props.currentURL === '/best' && ' | '
48+
}
49+
{
50+
props.currentURL === '/best' && (
51+
<Link prefetch href="/best">
52+
<a className="topsel">best</a>
53+
</Link>
54+
)
55+
}
4656
</span>
4757
:
4858
<span className="pagetop">

src/data/HNDataAPI.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export function seedCache(delay) {
129129
logger(`Waiting ${delay} ms before seeding the app with data.`);
130130
setTimeout(() => {
131131
logger('Seeding cache');
132-
['top', 'new', 'show', 'ask', 'job'].forEach((feedType) => {
132+
['top', 'new', 'best', 'show', 'ask', 'job'].forEach((feedType) => {
133133
rebuildFeed(feedType);
134134
});
135135
}, delay);

src/data/Schema.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ const typeDefs = `
5656
# Newest entries first
5757
NEW
5858
59+
# Sort by score
60+
BEST
61+
5962
# SHOW HN articles
6063
SHOW
6164

src/data/models/Feed.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class Feed {
2121
return Promise.all(
2222
this.new.slice(skip, first + skip)
2323
.map(id => cache.getNewsItem(id) || HNDB.fetchNewsItem(id)));
24+
case 'BEST':
25+
return Promise.all(
26+
this.best.slice(skip, first + skip)
27+
.map(id => cache.getNewsItem(id) || HNDB.fetchNewsItem(id)));
2428
case 'SHOW':
2529
return this.showNewsItems.slice(skip, first + skip);
2630
case 'ASK':
@@ -35,13 +39,15 @@ class Feed {
3539
/* Arrays of post ids in descending rank order */
3640
top = sampleData.top;
3741
new = sampleData.new;
42+
best = [];
3843
show = [];
3944
ask = [];
4045
job = [];
4146

4247
/* A pre constructed cache of news feeds */
4348
topNewsItems = sampleData.topStoriesCache;
4449
newNewsItems = sampleData.topStoriesCache;
50+
bestNewsItems = sampleData.topStoriesCache;
4551
showNewsItems = sampleData.topStoriesCache;
4652
askNewsItems = sampleData.topStoriesCache;
4753
jobNewsItems = sampleData.topStoriesCache;

src/pages/__tests__/best.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { shallow } from 'enzyme';
44
import Page from '../best';
55

66

7-
describe('Newest Posts Page', () => {
7+
describe('Best Posts Page', () => {
88
it('is defined', () => {
99
const app = shallow(<Page serverState={{}} />);
1010
expect(app).toBeDefined();

src/pages/best.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
11
import React from 'react';
2+
import { graphql, gql } from 'react-apollo';
23

34
import Main from '../layouts/Main';
45
import NewsFeed from '../components/presentational/NewsFeed';
6+
import NewsFeedApolloHOC from '../components/container/NewsFeedWithApolloRenderer';
57
import withData from '../helpers/withData';
68

7-
import data from '../data/SampleData';
9+
const POSTS_PER_PAGE = 30;
810

9-
export default withData(props => (
10-
<Main currentURL={props.url.pathname}>
11-
<NewsFeed
12-
newsItems={data.newsItems}
13-
/>
14-
</Main>
15-
));
11+
const query = gql`
12+
query bestNewsItems($type: FeedType!, $first: Int!, $skip: Int!) {
13+
feed(type: $type, first: $first, skip: $skip) {
14+
...NewsFeed
15+
}
16+
}
17+
${NewsFeed.fragments.newsItem}
18+
`;
19+
20+
const BestNewsFeed = graphql(query, {
21+
options: ({ options: { first, skip } }) => ({
22+
variables: {
23+
type: 'BEST',
24+
first,
25+
skip
26+
}
27+
}),
28+
props: ({ data }) => ({
29+
data,
30+
}),
31+
})(NewsFeedApolloHOC);
32+
33+
export default withData(props => {
34+
const pageNumber = (props.url.query && +props.url.query.p) || 0;
35+
return (
36+
<Main currentURL={props.url.pathname}>
37+
<BestNewsFeed options={{
38+
currentURL: props.url.pathname,
39+
first: POSTS_PER_PAGE,
40+
skip: POSTS_PER_PAGE * pageNumber
41+
}}
42+
/>
43+
</Main>
44+
);
45+
});

0 commit comments

Comments
 (0)