|
1 | | -import React, { Component } from 'react' |
2 | | -import { Mutation } from 'react-apollo' |
3 | | -import gql from 'graphql-tag' |
4 | | -import { FEED_QUERY } from './LinkList' |
5 | | -import { LINKS_PER_PAGE } from '../constants' |
| 1 | +import React, { Component, useState } from 'react'; |
| 2 | +import gql from 'graphql-tag'; |
| 3 | +import { FEED_QUERY } from './LinkList'; |
| 4 | +import { LINKS_PER_PAGE } from '../constants'; |
| 5 | +import { useMutation } from '@apollo/client'; |
| 6 | +import { useHistory } from 'react-router'; |
6 | 7 |
|
7 | 8 | const POST_MUTATION = gql` |
8 | | - mutation PostMutation($description: String!, $url: String!) { |
9 | | - post(description: $description, url: $url) { |
| 9 | + mutation PostMutation( |
| 10 | + $description: String! |
| 11 | + $url: String! |
| 12 | + ) { |
| 13 | + createPost(description: $description, url: $url) { |
10 | 14 | id |
11 | 15 | createdAt |
12 | 16 | url |
13 | 17 | description |
14 | 18 | } |
15 | 19 | } |
16 | | -` |
| 20 | +`; |
17 | 21 |
|
18 | | -class CreateLink extends Component { |
19 | | - state = { |
| 22 | +const CreateLink = () => { |
| 23 | + const history = useHistory(); |
| 24 | + const [formState, setFormState] = useState({ |
20 | 25 | description: '', |
21 | | - url: '', |
22 | | - } |
23 | | - |
24 | | - render() { |
25 | | - const { description, url } = this.state |
26 | | - return ( |
27 | | - <div> |
| 26 | + url: '' |
| 27 | + }); |
| 28 | + const [createLink] = useMutation(POST_MUTATION, { |
| 29 | + variables: { |
| 30 | + description: formState.description, |
| 31 | + url: formState.url |
| 32 | + }, |
| 33 | + onCompleted: () => history.push('/new/1') |
| 34 | + }); |
| 35 | + return ( |
| 36 | + <div> |
| 37 | + <form |
| 38 | + onSubmit={(e) => { |
| 39 | + e.preventDefault(); |
| 40 | + createLink(); |
| 41 | + }} |
| 42 | + > |
28 | 43 | <div className="flex flex-column mt3"> |
29 | 44 | <input |
30 | 45 | className="mb2" |
31 | | - value={description} |
32 | | - onChange={e => this.setState({ description: e.target.value })} |
| 46 | + value={formState.description} |
| 47 | + onChange={(e) => |
| 48 | + setFormState({ |
| 49 | + ...formState, |
| 50 | + description: e.target.value |
| 51 | + }) |
| 52 | + } |
33 | 53 | type="text" |
34 | 54 | placeholder="A description for the link" |
35 | 55 | /> |
36 | 56 | <input |
37 | 57 | className="mb2" |
38 | | - value={url} |
39 | | - onChange={e => this.setState({ url: e.target.value })} |
| 58 | + value={formState.url} |
| 59 | + onChange={(e) => |
| 60 | + setFormState({ |
| 61 | + ...formState, |
| 62 | + url: e.target.value |
| 63 | + }) |
| 64 | + } |
40 | 65 | type="text" |
41 | 66 | placeholder="The URL for the link" |
42 | 67 | /> |
43 | 68 | </div> |
44 | | - <Mutation |
45 | | - mutation={POST_MUTATION} |
46 | | - variables={{ description, url }} |
47 | | - onCompleted={() => this.props.history.push('/new/1')} |
48 | | - update={(store, { data: { post } }) => { |
49 | | - const first = LINKS_PER_PAGE |
50 | | - const skip = 0 |
51 | | - const orderBy = 'createdAt_DESC' |
52 | | - const data = store.readQuery({ |
53 | | - query: FEED_QUERY, |
54 | | - variables: { first, skip, orderBy }, |
55 | | - }) |
56 | | - data.feed.links.unshift(post) |
57 | | - store.writeQuery({ |
58 | | - query: FEED_QUERY, |
59 | | - data, |
60 | | - variables: { first, skip, orderBy }, |
61 | | - }) |
62 | | - }} |
63 | | - > |
64 | | - {postMutation => <button onClick={postMutation}>Submit</button>} |
65 | | - </Mutation> |
66 | | - </div> |
67 | | - ) |
68 | | - } |
69 | | -} |
| 69 | + <button type="submit">Submit</button> |
| 70 | + </form> |
| 71 | + </div> |
| 72 | + ); |
| 73 | +}; |
70 | 74 |
|
71 | | -export default CreateLink |
| 75 | +export default CreateLink; |
0 commit comments