-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathCreatePage.js
More file actions
73 lines (67 loc) · 2 KB
/
CreatePage.js
File metadata and controls
73 lines (67 loc) · 2 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
import React, { Component } from 'react'
import { withRouter } from 'react-router-dom'
import { graphql } from 'react-apollo'
import { gql } from 'apollo-boost'
class CreatePage extends Component {
state = {
title: '',
content: '',
}
render() {
return (
<div className="pa4 flex justify-center bg-white">
<form onSubmit={this.handlePost}>
<h1>Create Draft</h1>
<input
autoFocus
className="w-100 pa2 mv2 br2 b--black-20 bw1"
onChange={e => this.setState({ title: e.target.value })}
placeholder="Title"
type="text"
value={this.state.title}
/>
<textarea
className="db w-100 ba bw1 b--black-20 pa2 br2 mb2"
cols={50}
onChange={e => this.setState({ content: e.target.value })}
placeholder="Content"
rows={8}
value={this.state.content}
/>
<input
className={`pa3 bg-black-10 bn ${this.state.content &&
this.state.title &&
'dim pointer'}`}
disabled={!this.state.content || !this.state.title}
type="submit"
value="Create"
/>
<a className="f6 pointer" onClick={this.props.history.goBack}>
or cancel
</a>
</form>
</div>
)
}
handlePost = async e => {
e.preventDefault()
const { title, content } = this.state
await this.props.createDraftMutation({
variables: { title, content },
})
this.props.history.replace('/drafts')
}
}
const CREATE_DRAFT_MUTATION = gql`
mutation CreateDraftMutation($title: String!, $content: String!) {
createDraft(title: $title, content: $content) {
id
title
content
}
}
`
const CreatePageWithMutation = graphql(CREATE_DRAFT_MUTATION, {
name: 'createDraftMutation', // name of the injected prop: this.props.createDraftMutation...
})(CreatePage)
export default withRouter(CreatePageWithMutation)