2017-11-23 13:05:51 +00:00
|
|
|
import React from 'react'
|
2017-12-27 18:57:57 +00:00
|
|
|
import { withRouter } from 'next/router'
|
|
|
|
import { graphql } from 'react-apollo'
|
|
|
|
import gql from 'graphql-tag'
|
2017-11-23 13:05:51 +00:00
|
|
|
|
2017-12-27 18:57:57 +00:00
|
|
|
import ErrorMessage from './ErrorMessage'
|
|
|
|
import PostVoteUp from './PostVoteUp'
|
|
|
|
import PostVoteDown from './PostVoteDown'
|
|
|
|
import PostVoteCount from './PostVoteCount'
|
|
|
|
|
|
|
|
function Post ({ id, data: { error, Post } }) {
|
|
|
|
if (error) return <ErrorMessage message='Error loading blog post.' />
|
|
|
|
if (Post) {
|
|
|
|
return (
|
|
|
|
<section>
|
|
|
|
<div key={Post.id}>
|
|
|
|
<h1>{Post.title}</h1>
|
|
|
|
<p>ID: {Post.id}<br />URL: {Post.url}</p>
|
|
|
|
<span>
|
|
|
|
<PostVoteUp id={Post.id} votes={Post.votes} />
|
|
|
|
<PostVoteCount votes={Post.votes} />
|
|
|
|
<PostVoteDown id={Post.id} votes={Post.votes} />
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<style jsx>{`
|
|
|
|
span {
|
|
|
|
display: flex;
|
|
|
|
font-size: 14px;
|
|
|
|
margin-right: 5px;
|
|
|
|
}
|
|
|
|
`}</style>
|
|
|
|
</section>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return <div>Loading</div>
|
2017-11-23 13:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const post = gql`
|
2017-12-27 18:57:57 +00:00
|
|
|
query post($id: ID!) {
|
|
|
|
Post(id: $id) {
|
|
|
|
id
|
|
|
|
title
|
|
|
|
votes
|
|
|
|
url
|
|
|
|
createdAt
|
2017-11-23 13:05:51 +00:00
|
|
|
}
|
2017-12-27 18:57:57 +00:00
|
|
|
}
|
2017-11-23 13:05:51 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
// The `graphql` wrapper executes a GraphQL query and makes the results
|
2017-12-27 18:57:57 +00:00
|
|
|
// available on the `data` prop of the wrapped component (Post)
|
|
|
|
const ComponentWithMutation = graphql(post, {
|
|
|
|
options: ({ router: { query } }) => ({
|
|
|
|
variables: {
|
|
|
|
id: query.id
|
2017-11-23 13:05:51 +00:00
|
|
|
}
|
2017-12-27 18:57:57 +00:00
|
|
|
}),
|
|
|
|
props: ({ data }) => ({
|
|
|
|
data
|
|
|
|
})
|
2017-11-23 13:05:51 +00:00
|
|
|
})(Post)
|
2017-12-27 18:57:57 +00:00
|
|
|
|
|
|
|
export default withRouter(ComponentWithMutation)
|