|
| 1 | +import gql from 'graphql-tag' |
| 2 | +import { graphql } from 'react-apollo' |
| 3 | +import PostUpvoter from './PostUpvoter' |
| 4 | + |
| 5 | +const POSTS_PER_PAGE = 10 |
| 6 | + |
| 7 | +function PostList ({ data: { allPosts, loading, _allPostsMeta }, loadMorePosts }) { |
| 8 | + if (loading) { |
| 9 | + return <div>Loading</div> |
| 10 | + } |
| 11 | + |
| 12 | + const areMorePosts = allPosts.length < _allPostsMeta.count |
| 13 | + |
| 14 | + return ( |
| 15 | + <section> |
| 16 | + <ul> |
| 17 | + {allPosts |
| 18 | + .sort((x, y) => new Date(y.createdAt) - new Date(x.createdAt)) |
| 19 | + .map((post, index) => |
| 20 | + <li key={post.id}> |
| 21 | + <div> |
| 22 | + <span>{index + 1}. </span> |
| 23 | + <a href={post.url}>{post.title}</a> |
| 24 | + <PostUpvoter id={post.id} votes={post.votes} /> |
| 25 | + </div> |
| 26 | + </li> |
| 27 | + )} |
| 28 | + </ul> |
| 29 | + {areMorePosts ? <button onClick={() => loadMorePosts()}><span />Show More</button> : ''} |
| 30 | + <style jsx>{` |
| 31 | + section { |
| 32 | + padding-bottom: 20px; |
| 33 | + } |
| 34 | + li { |
| 35 | + display: block; |
| 36 | + margin-bottom: 10px; |
| 37 | + } |
| 38 | + div { |
| 39 | + align-items: center; |
| 40 | + display: flex; |
| 41 | + } |
| 42 | + a { |
| 43 | + font-size: 14px; |
| 44 | + margin-right: 10px; |
| 45 | + text-decoration: none; |
| 46 | + padding-bottom: 0; |
| 47 | + border: 0; |
| 48 | + } |
| 49 | + span { |
| 50 | + font-size: 14px; |
| 51 | + margin-right: 5px; |
| 52 | + } |
| 53 | + ul { |
| 54 | + margin: 0; |
| 55 | + padding: 0; |
| 56 | + } |
| 57 | + button:before { |
| 58 | + align-self: center; |
| 59 | + border-style: solid; |
| 60 | + border-width: 6px 4px 0 4px; |
| 61 | + border-color: #ffffff transparent transparent transparent; |
| 62 | + content: ""; |
| 63 | + height: 0; |
| 64 | + width: 0; |
| 65 | + } |
| 66 | + `}</style> |
| 67 | + </section> |
| 68 | + ) |
| 69 | +} |
| 70 | + |
| 71 | +const allPosts = gql` |
| 72 | + query allPosts($first: Int!, $skip: Int!) { |
| 73 | + allPosts(orderBy: createdAt_DESC, first: $first, skip: $skip) { |
| 74 | + id |
| 75 | + title |
| 76 | + votes |
| 77 | + url |
| 78 | + createdAt |
| 79 | + }, |
| 80 | + _allPostsMeta { |
| 81 | + count |
| 82 | + } |
| 83 | + } |
| 84 | +` |
| 85 | + |
| 86 | +// The `graphql` wrapper executes a GraphQL query and makes the results |
| 87 | +// available on the `data` prop of the wrapped component (PostList) |
| 88 | +export default graphql(allPosts, { |
| 89 | + options: { |
| 90 | + variables: { |
| 91 | + skip: 0, |
| 92 | + first: POSTS_PER_PAGE |
| 93 | + } |
| 94 | + }, |
| 95 | + props: ({ data }) => ({ |
| 96 | + data, |
| 97 | + loadMorePosts: () => { |
| 98 | + return data.fetchMore({ |
| 99 | + variables: { |
| 100 | + skip: data.allPosts.length |
| 101 | + }, |
| 102 | + updateQuery: (previousResult, { fetchMoreResult }) => { |
| 103 | + if (!fetchMoreResult.data) { |
| 104 | + return previousResult |
| 105 | + } |
| 106 | + return Object.assign({}, previousResult, { |
| 107 | + // Append the new posts results to the old one |
| 108 | + allPosts: [...previousResult.allPosts, ...fetchMoreResult.data.allPosts] |
| 109 | + }) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | + }) |
| 114 | +})(PostList) |
0 commit comments