-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathCreateLink.js
66 lines (62 loc) · 1.73 KB
/
CreateLink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import React, { useState } from 'react'
import { Mutation } from 'react-apollo'
import gql from 'graphql-tag'
import { FEED_QUERY } from './LinkList'
import { LINKS_PER_PAGE } from '../constants'
const POST_MUTATION = gql`
mutation PostMutation($description: String!, $url: String!) {
post(description: $description, url: $url) {
id
createdAt
url
description
}
}
`
function CreateLink() {
const [description, setDescription] = useState()
const [url, setUrl] = useState()
return (
<div>
<div className="flex flex-column mt3">
<input
className="mb2"
value={description}
onChange={e => setDescription(e.target.value)}
type="text"
placeholder="A description for the link"
/>
<input
className="mb2"
value={url}
onChange={e => setUrl(e.target.value)}
type="text"
placeholder="The URL for the link"
/>
</div>
<Mutation
mutation={POST_MUTATION}
variables={{ description, url }}
onCompleted={() => this.props.history.push('/new/1')}
update={(store, { data: { post } }) => {
const first = LINKS_PER_PAGE
const skip = 0
const orderBy = 'createdAt_DESC'
const data = store.readQuery({
query: FEED_QUERY,
variables: { first, skip, orderBy },
})
data.feed.links.unshift(post)
store.writeQuery({
query: FEED_QUERY,
data,
variables: { first, skip, orderBy },
})
}}
>
{postMutation => <button onClick={postMutation}>Submit</button>}
</Mutation>
</div>
)
}
export default CreateLink