2017-12-27 18:57:57 +00:00
|
|
|
import { graphql } from 'react-apollo'
|
|
|
|
import gql from 'graphql-tag'
|
|
|
|
import { allPosts, allPostsQueryVars } from './PostList'
|
2017-03-30 18:21:13 +00:00
|
|
|
|
|
|
|
function Submit ({ createPost }) {
|
2017-12-27 18:57:57 +00:00
|
|
|
function handleSubmit (event) {
|
|
|
|
event.preventDefault()
|
2017-03-30 18:21:13 +00:00
|
|
|
|
2017-12-27 18:57:57 +00:00
|
|
|
const form = event.target
|
2017-03-30 18:21:13 +00:00
|
|
|
|
2017-12-27 18:57:57 +00:00
|
|
|
const formData = new window.FormData(form)
|
|
|
|
createPost(formData.get('title'), formData.get('url'))
|
2017-03-30 18:21:13 +00:00
|
|
|
|
2017-12-27 18:57:57 +00:00
|
|
|
form.reset()
|
2017-03-30 18:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit}>
|
2018-01-31 09:40:32 +00:00
|
|
|
<h1>Apollo: Submit</h1>
|
2017-12-27 18:57:57 +00:00
|
|
|
<input placeholder='title' name='title' type='text' required />
|
|
|
|
<input placeholder='url' name='url' type='url' required />
|
2017-03-30 18:21:13 +00:00
|
|
|
<button type='submit'>Submit</button>
|
|
|
|
<style jsx>{`
|
|
|
|
form {
|
|
|
|
border-bottom: 1px solid #ececec;
|
|
|
|
padding-bottom: 20px;
|
|
|
|
margin-bottom: 20px;
|
|
|
|
}
|
|
|
|
h1 {
|
|
|
|
font-size: 20px;
|
|
|
|
}
|
|
|
|
input {
|
|
|
|
display: block;
|
|
|
|
margin-bottom: 10px;
|
|
|
|
}
|
|
|
|
`}</style>
|
|
|
|
</form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const createPost = gql`
|
|
|
|
mutation createPost($title: String!, $url: String!) {
|
|
|
|
createPost(title: $title, url: $url) {
|
|
|
|
id
|
|
|
|
title
|
|
|
|
votes
|
|
|
|
url
|
|
|
|
createdAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
export default graphql(createPost, {
|
|
|
|
props: ({ mutate }) => ({
|
2017-12-27 18:57:57 +00:00
|
|
|
createPost: (title, url) =>
|
|
|
|
mutate({
|
|
|
|
variables: { title, url },
|
|
|
|
update: (proxy, { data: { createPost } }) => {
|
|
|
|
const data = proxy.readQuery({
|
|
|
|
query: allPosts,
|
|
|
|
variables: allPostsQueryVars
|
|
|
|
})
|
|
|
|
proxy.writeQuery({
|
|
|
|
query: allPosts,
|
|
|
|
data: {
|
|
|
|
...data,
|
|
|
|
allPosts: [createPost, ...data.allPosts]
|
|
|
|
},
|
|
|
|
variables: allPostsQueryVars
|
2017-03-30 18:21:13 +00:00
|
|
|
})
|
|
|
|
}
|
2017-12-27 18:57:57 +00:00
|
|
|
})
|
2017-03-30 18:21:13 +00:00
|
|
|
})
|
|
|
|
})(Submit)
|