1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-relay-modern-server-express/components/BlogPosts.js
Tim Neutkens 9c4eefcdbf
Add prettier for examples directory (#5909)
* Add prettier for examples directory

* Fix files

* Fix linting

* Add prettier script in case it has to be ran again
2018-12-17 17:34:32 +01:00

60 lines
1.6 KiB
JavaScript

import React from 'react'
import { createFragmentContainer, graphql } from 'react-relay'
import BlogPostPreview from './BlogPostPreview'
import Router from 'next/router'
const BlogPosts = props => {
let afterParam = props.viewer.allBlogPosts.pageInfo.endCursor
afterParam = afterParam ? `&after=${afterParam}` : ''
let hasNextPage = props.viewer.allBlogPosts.pageInfo.hasNextPage
hasNextPage = hasNextPage || props.relayVariables.before
let hasPrevPage = props.viewer.allBlogPosts.pageInfo.hasPreviousPage
hasPrevPage = hasPrevPage || props.relayVariables.after
let beforeParam = props.viewer.allBlogPosts.pageInfo.startCursor
beforeParam = beforeParam ? `&before=${beforeParam}` : ''
const nextOnClick = () => Router.push(`/?first=2${afterParam}`)
const prevOnClick = () => Router.push(`/?last=2${beforeParam}`)
return (
<div>
<h1>Blog posts</h1>
{props.viewer.allBlogPosts.edges.map(({ node }) => (
<BlogPostPreview key={node.id} post={node} />
))}
<br />
<button disabled={!hasPrevPage} onClick={prevOnClick}>
Previous Page
</button>
&nbsp;
<button disabled={!hasNextPage} onClick={nextOnClick}>
Next Page
</button>
</div>
)
}
export default createFragmentContainer(BlogPosts, {
viewer: graphql`
fragment BlogPosts_viewer on Viewer {
allBlogPosts {
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
edges {
node {
...BlogPostPreview_post
id
}
}
}
}
`
})