-
Notifications
You must be signed in to change notification settings - Fork 556
/
Copy pathindex.js
61 lines (56 loc) · 1.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React from 'react';
import { graphql, gql } from 'react-apollo';
import PropTypes from 'prop-types';
import Main from '../layouts/Main';
import NewsFeed from '../components/presentational/NewsFeed';
import NewsFeedApolloHOC from '../components/container/NewsFeedWithApolloRenderer';
import withData from '../helpers/withData';
import registerServiceWorker from '../helpers/registerServiceWorker';
const POSTS_PER_PAGE = 30;
const query = gql`
query topNewsItems($type: FeedType!, $first: Int!, $skip: Int!) {
feed(type: $type, first: $first, skip: $skip) {
...NewsFeed
}
}
${NewsFeed.fragments.newsItem}
`;
const TopNewsFeed = graphql(query, {
options: ({ options: { first, skip } }) => ({
variables: {
type: 'TOP',
first,
skip,
},
}),
props: ({ data }) => ({
data,
}),
loadMorePosts: data => data.fetchMore({
variables: {
skip: data.allNewsItems.length,
},
updateQuery: (previousResult, { fetchMoreResult }) => {
if (!fetchMoreResult) {
return previousResult;
}
return Object.assign({}, previousResult, {
// Append the new posts results to the old one
allNewsItems: [...previousResult.allNewsItems, ...fetchMoreResult.allNewsItems],
});
},
}),
})(NewsFeedApolloHOC);
export default withData(registerServiceWorker((props) => {
const pageNumber = (props.url.query && +props.url.query.p) || 0;
return (
<Main currentURL={props.url.pathname}>
<TopNewsFeed options={{
currentURL: props.url.pathname,
first: POSTS_PER_PAGE,
skip: POSTS_PER_PAGE * pageNumber,
}}
/>
</Main>
);
}));