2017-01-22 12:27:06 +00:00
|
|
|
import gql from 'graphql-tag'
|
|
|
|
import { graphql } from 'react-apollo'
|
|
|
|
import PostUpvoter from './PostUpvoter'
|
|
|
|
|
|
|
|
const POSTS_PER_PAGE = 10
|
|
|
|
|
|
|
|
function PostList ({ data: { allPosts, loading, _allPostsMeta }, loadMorePosts }) {
|
|
|
|
if (loading) {
|
|
|
|
return <div>Loading</div>
|
|
|
|
}
|
|
|
|
|
|
|
|
const areMorePosts = allPosts.length < _allPostsMeta.count
|
|
|
|
|
|
|
|
return (
|
|
|
|
<section>
|
|
|
|
<ul>
|
2017-01-27 16:06:17 +00:00
|
|
|
{allPosts.map((post, index) =>
|
2017-01-22 12:27:06 +00:00
|
|
|
<li key={post.id}>
|
|
|
|
<div>
|
|
|
|
<span>{index + 1}. </span>
|
|
|
|
<a href={post.url}>{post.title}</a>
|
|
|
|
<PostUpvoter id={post.id} votes={post.votes} />
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
)}
|
|
|
|
</ul>
|
|
|
|
{areMorePosts ? <button onClick={() => loadMorePosts()}><span />Show More</button> : ''}
|
|
|
|
<style jsx>{`
|
|
|
|
section {
|
|
|
|
padding-bottom: 20px;
|
|
|
|
}
|
|
|
|
li {
|
|
|
|
display: block;
|
|
|
|
margin-bottom: 10px;
|
|
|
|
}
|
|
|
|
div {
|
|
|
|
align-items: center;
|
|
|
|
display: flex;
|
|
|
|
}
|
|
|
|
a {
|
|
|
|
font-size: 14px;
|
|
|
|
margin-right: 10px;
|
|
|
|
text-decoration: none;
|
|
|
|
padding-bottom: 0;
|
|
|
|
border: 0;
|
|
|
|
}
|
|
|
|
span {
|
|
|
|
font-size: 14px;
|
|
|
|
margin-right: 5px;
|
|
|
|
}
|
|
|
|
ul {
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
button:before {
|
|
|
|
align-self: center;
|
|
|
|
border-style: solid;
|
|
|
|
border-width: 6px 4px 0 4px;
|
|
|
|
border-color: #ffffff transparent transparent transparent;
|
|
|
|
content: "";
|
|
|
|
height: 0;
|
|
|
|
width: 0;
|
|
|
|
}
|
|
|
|
`}</style>
|
|
|
|
</section>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const allPosts = gql`
|
|
|
|
query allPosts($first: Int!, $skip: Int!) {
|
|
|
|
allPosts(orderBy: createdAt_DESC, first: $first, skip: $skip) {
|
|
|
|
id
|
|
|
|
title
|
|
|
|
votes
|
|
|
|
url
|
|
|
|
createdAt
|
|
|
|
},
|
|
|
|
_allPostsMeta {
|
|
|
|
count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
// The `graphql` wrapper executes a GraphQL query and makes the results
|
|
|
|
// available on the `data` prop of the wrapped component (PostList)
|
|
|
|
export default graphql(allPosts, {
|
|
|
|
options: {
|
|
|
|
variables: {
|
|
|
|
skip: 0,
|
|
|
|
first: POSTS_PER_PAGE
|
|
|
|
}
|
|
|
|
},
|
|
|
|
props: ({ data }) => ({
|
|
|
|
data,
|
|
|
|
loadMorePosts: () => {
|
|
|
|
return data.fetchMore({
|
|
|
|
variables: {
|
|
|
|
skip: data.allPosts.length
|
|
|
|
},
|
|
|
|
updateQuery: (previousResult, { fetchMoreResult }) => {
|
|
|
|
if (!fetchMoreResult.data) {
|
|
|
|
return previousResult
|
|
|
|
}
|
|
|
|
return Object.assign({}, previousResult, {
|
|
|
|
// Append the new posts results to the old one
|
|
|
|
allPosts: [...previousResult.allPosts, ...fetchMoreResult.data.allPosts]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})(PostList)
|