Skip to content

Commit ce1de85

Browse files
committed
Added a MessageForm for postMessage mutation
1 parent ca24269 commit ce1de85

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { Component } from "react";
2+
import gql from "graphql-tag";
3+
import { Mutation } from "react-apollo";
4+
import {
5+
Card,
6+
CardBody,
7+
Button,
8+
Form,
9+
FormGroup,
10+
Input,
11+
FormText
12+
} from "reactstrap";
13+
14+
const POST_MESSAGE = gql`
15+
mutation postMessage($text: String!) {
16+
postMessage(text: $text) {
17+
text
18+
}
19+
}
20+
`;
21+
22+
class MessageForm extends Component {
23+
render() {
24+
return (
25+
<Mutation mutation={POST_MESSAGE}>
26+
{(postMessage, { data, loading, error }) => {
27+
const handleSubmit = evt => {
28+
evt.preventDefault();
29+
const text = evt.target.text.value;
30+
if (!text) return;
31+
postMessage({
32+
variables: {
33+
text
34+
}
35+
});
36+
evt.target.text.value = "";
37+
};
38+
return (
39+
<Card>
40+
<CardBody>
41+
<Form onSubmit={handleSubmit}>
42+
<FormGroup>
43+
<Input
44+
type="text"
45+
name="text"
46+
placeholder="type a message"
47+
/>
48+
</FormGroup>
49+
<Button type="submit" color="primary" block>
50+
Submit
51+
</Button>
52+
</Form>
53+
</CardBody>
54+
</Card>
55+
);
56+
}}
57+
</Mutation>
58+
);
59+
}
60+
}
61+
62+
export default MessageForm;

client/components/Protected.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { Component } from "react";
1+
import { Component, Fragment } from "react";
2+
import MessageForm from "./Messages/MessageForm";
3+
import { Row, Col } from "reactstrap";
24

35
class Protected extends Component {
46
render() {
57
return (
6-
<div>
7-
<h1>Protected</h1>
8-
</div>
8+
<Row style={{ paddingTop: "30px" }}>
9+
<Col>
10+
<MessageForm />
11+
</Col>
12+
</Row>
913
);
1014
}
1115
}

0 commit comments

Comments
 (0)