|
1 | 1 | import React from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
2 | 3 | import { Controlled as CodeMirror } from 'react-codemirror2'; |
3 | 4 |
|
4 | 5 | import 'codemirror/lib/codemirror.css'; |
5 | 6 |
|
6 | 7 | import Title from './common/Title'; |
7 | 8 | import Input from './common/Input'; |
8 | 9 | import Syntaxes from './Syntaxes'; |
| 10 | +import * as actions from '../actions'; |
9 | 11 |
|
10 | 12 | import '../styles/NewSnippet.styl'; |
11 | 13 |
|
12 | 14 | class NewSnippet extends React.Component { |
13 | | - constructor() { |
14 | | - super(); |
15 | | - this.state = { code: '' }; |
| 15 | + constructor(props) { |
| 16 | + super(props); |
| 17 | + this.state = { |
| 18 | + content: '', |
| 19 | + title: '', |
| 20 | + tags: [], |
| 21 | + syntax: '', // eslint-disable-line react/no-unused-state |
| 22 | + }; |
| 23 | + this.postSnippet = this.postSnippet.bind(this); |
| 24 | + this.onSyntaxClick = this.onSyntaxClick.bind(this); |
| 25 | + this.onInputChange = this.onInputChange.bind(this); |
16 | 26 | } |
| 27 | + |
| 28 | + onSyntaxClick(syntax) { |
| 29 | + this.setState({ syntax }); // eslint-disable-line react/no-unused-state |
| 30 | + } |
| 31 | + |
| 32 | + onInputChange(e) { |
| 33 | + const { name } = e.target; |
| 34 | + let { value } = e.target; |
| 35 | + |
| 36 | + if (name === 'tags') { |
| 37 | + value = value.split(',').map(item => item.trim()); |
| 38 | + } |
| 39 | + |
| 40 | + this.setState({ [name]: value }); |
| 41 | + } |
| 42 | + |
| 43 | + postSnippet(e) { |
| 44 | + e.preventDefault(); |
| 45 | + const { dispatch } = this.props; |
| 46 | + dispatch(actions.postSnippet(this.state)); |
| 47 | + } |
| 48 | + |
17 | 49 | render() { |
18 | 50 | return ( |
19 | 51 | [ |
20 | 52 | <Title title="New snippet" key="New Snippet Title" />, |
21 | | - <div className="new-snippet" key="New Snippet"> |
| 53 | + <form className="new-snippet" key="New Snippet" onSubmit={this.postSnippet}> |
22 | 54 | <div className="new-snippet-code-wrapper"> |
23 | 55 | <div className="new-snippet-code-header"> |
24 | | - <Input placeholder="Title" /> |
25 | | - <Input placeholder="Tags (separate tags by comma)" /> |
| 56 | + <Input |
| 57 | + placeholder="Title" |
| 58 | + name="title" |
| 59 | + onChangeHandler={this.onInputChange} |
| 60 | + value={this.state.title} |
| 61 | + /> |
| 62 | + <Input |
| 63 | + placeholder="Tags (separate tags by comma)" |
| 64 | + name="tags" |
| 65 | + onChangeHandler={this.onInputChange} |
| 66 | + value={this.state.tags.toString()} |
| 67 | + /> |
26 | 68 | </div> |
27 | 69 | <div className="new-snippet-code"> |
28 | 70 | <CodeMirror |
29 | | - value={this.state.code} |
30 | | - options={{ lineNumbers: false }} |
31 | | - onBeforeChange={(editor, data, code) => { |
32 | | - this.setState({ code }); |
33 | | - }} |
| 71 | + value={this.state.content} |
| 72 | + options={{ lineNumbers: true }} |
| 73 | + onBeforeChange={(editor, data, content) => { this.setState({ content }); }} |
34 | 74 | /> |
35 | 75 | <div className="new-snippet-code-bottom-bar"> |
36 | | - <button>POST</button> |
| 76 | + <input type="submit" value="POST" /> |
37 | 77 | </div> |
38 | 78 | </div> |
39 | 79 | </div> |
40 | 80 | <div className="new-snippet-lang-wrapper"> |
41 | | - <Syntaxes /> |
| 81 | + <Syntaxes onClick={this.onSyntaxClick} /> |
42 | 82 | </div> |
43 | | - </div>, |
| 83 | + </form>, |
44 | 84 | ] |
45 | 85 | ); |
46 | 86 | } |
47 | 87 | } |
48 | 88 |
|
49 | | -export default NewSnippet; |
| 89 | +export default connect()(NewSnippet); |
0 commit comments