PostForm.js
1.52 KB
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
67
68
69
70
71
72
73
74
75
import React from "react";
import { Form, Button } from "semantic-ui-react";
import { useForm } from "../utils/hook";
import { useMutation } from "@apollo/react-hooks";
import gql from "graphql-tag";
function PostForm() {
const { onChange, onSubmit, values } = useForm(createPostCallback, {
body: ""
});
const [createPost, { error }] = useMutation(CREATE_POST_MUTATION, {
update(_, result) {
values.body = "";
},
variables: values
});
console.dir(error);
function createPostCallback() {
createPost();
}
return (
<>
<Form onSubmit={onSubmit}>
<h2>Create a post:</h2>
<Form.Field>
<Form.Input
placeholder="Hi World!"
value={values.body}
name="body"
error={error ? true : false}
onChange={onChange}
/>
<Button type="submit" color="teal">
Submit
</Button>
</Form.Field>
</Form>
{error && (
<div className="ui error message" style={{ marginBottom: 20 }}>
<ul className="list">{error.graphQLErrors[0].message}</ul>
</div>
)}
</>
);
}
const CREATE_POST_MUTATION = gql`
mutation createPost($body: String!) {
createPost(body: $body) {
id
body
createdAt
username
likes {
id
username
createdAt
}
likeCount
comments {
id
body
username
createdAt
}
commentCount
}
}
`;
export default PostForm;