Skip to content

Commit cfd98a2

Browse files
author
James Robson
committed
Hooked up comment form
1 parent 1829d6c commit cfd98a2

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

.eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"react"
2222
],
2323
"rules": {
24+
"no-var": "error",
2425
"indent": [
2526
"error",
2627
2

comments.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"text": "React is *great*!"
1111
},
1212
{
13-
"id": 1469536026617,
14-
"author": "ioahspefpi",
15-
"text": "poiasdfpoijasdf"
13+
"id": 1469622378109,
14+
"author": "asef",
15+
"text": "aesfas"
1616
}
1717
]

public/scripts/tutorial.js

+58-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
(function() {
22
'use strict';
33

4-
let data = [
5-
{id: 1, author: 'Pete Hunt', text: 'This is one comment'},
6-
{id: 2, author: 'Jordan Walke', text: 'This is *another* comment'}
7-
];
84
let Comment = React.createClass({
95
rawMarkup: function() {
106
let md = new Remarkable();
@@ -37,7 +33,28 @@
3733
}.bind(this)
3834
});
3935
},
40-
36+
handleCommentSubmit: function(comment) {
37+
let comments = this.state.data;
38+
// Optimistically set an id on the new comment. It will be replaced by an
39+
// id generated by the server. In a production application you would likely
40+
// not use Date.now() for this and would have a more robust system in place.
41+
comment.id = Date.now();
42+
let newComments = comments.concat([comment]);
43+
this.setState({data: newComments});
44+
$.ajax({
45+
url: this.props.url,
46+
dataType: 'json',
47+
type: 'POST',
48+
data: comment,
49+
success: function(data) {
50+
this.setState({data: data});
51+
}.bind(this),
52+
error: function(xhr, status, err) {
53+
this.setState({data: comments});
54+
console.error(this.props.url, status, err.toString());
55+
}.bind(this)
56+
});
57+
},
4158
getInitialState: function() {
4259
return {data: []};
4360
},
@@ -52,7 +69,7 @@
5269
<div className="commentBox">
5370
<h1>Comments</h1>
5471
<CommentList data={this.state.data} />
55-
<CommentForm />
72+
<CommentForm onCommentSubmit={this.handleCommentSubmit} />
5673
</div>
5774
);
5875
}
@@ -76,16 +93,47 @@
7693
});
7794

7895
let CommentForm = React.createClass({
96+
getInitialState: function() {
97+
return {author: '', text: ''};
98+
},
99+
handleAuthorChange: function(e) {
100+
this.setState({author: e.target.value});
101+
},
102+
handleTextChange: function(e) {
103+
this.setState({text: e.target.value});
104+
},
105+
handleSubmit: function(e) {
106+
e.preventDefault();
107+
let author = this.state.author.trim();
108+
let text = this.state.text.trim();
109+
if (!text || !author) {
110+
return;
111+
}
112+
this.props.onCommentSubmit({author: author, text: text});
113+
this.setState({author: '', text: ''});
114+
},
79115
render: function() {
80116
return (
81-
<div className="commentForm">
82-
Hello, world! I am a CommentForm.
83-
</div>
117+
<form className="commentForm" onSubmit={this.handleSubmit}>
118+
<input
119+
type="text"
120+
placeholder="Your name"
121+
value={this.state.author}
122+
onChange={this.handleAuthorChange}
123+
/>
124+
<input
125+
type="text"
126+
placeholder="Say something..."
127+
value={this.state.text}
128+
onChange={this.handleTextChange}
129+
/>
130+
<input type="submit" value="Post" />
131+
</form>
84132
);
85133
}
86134
});
87135
ReactDOM.render(
88-
<CommentBox url="/api/comments" pollInterval={2000}/>,
136+
<CommentBox url="/api/comments" pollInterval={10000}/>,
89137
document.getElementById('content')
90138
);
91139
})();

0 commit comments

Comments
 (0)