Skip to content

Commit f27353f

Browse files
committed
tutorial complete - time for troubleshooting
1 parent a59fbb4 commit f27353f

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

public/index.html

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@
3131
}.bind(this)
3232
});
3333
},
34+
handleCommentSubmit: function (comment) {
35+
var comments = this.state.data;
36+
var newComments = comments.concat([comment]);
37+
this.setState({data: newComments});
38+
$.ajax({
39+
url: this.props.url,
40+
dataType: 'json',
41+
type: 'POST',
42+
data: comment,
43+
success: function (data) {
44+
this.setState({data: data});
45+
}.bind(this),
46+
error: function (xhr, status, err) {
47+
console.error(this.props.url, status, err.toString());
48+
}.bind(this)
49+
});
50+
},
3451
getInitialState: function () {
3552
return {data: []};
3653
},
@@ -66,16 +83,6 @@ <h1>Comments</h1>
6683
}
6784
});
6885

69-
var CommentForm = React.createClass({
70-
render: function () {
71-
return (
72-
<div className="commentForm">
73-
Hello, world! I am a CommentForm
74-
</div>
75-
);
76-
}
77-
});
78-
7986
var Comment = React.createClass({
8087
rawMarkup: function () {
8188
var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
@@ -94,7 +101,29 @@ <h2 className="commentAuthor">
94101
}
95102
});
96103

97-
104+
var CommentForm = React.createClass({
105+
handleSubmit: function (e) {
106+
e.preventDefault();
107+
var author = React.findDOMNode(this.refs.author).value.trim();
108+
var text = React.findDOMNode(this.refs.text).value.trim();
109+
if (!text || !author) {
110+
return;
111+
}
112+
this.props.onCommentSubmit({author: author, text: text})
113+
React.findDOMNode(this.refs.author).value = '';
114+
React.findDOMNode(this.refs.text).value = '';
115+
return;
116+
}
117+
render: function () {
118+
return (
119+
<form className="commentForm" onSubmit={this.handleSubmit}>
120+
<input type="text" placeholder="Your name" ref="author"/>
121+
<input type="text" placeholder="Say something..." ref="text" />
122+
<input type="submit" value="Post" />
123+
</form>
124+
);
125+
}
126+
});
98127

99128
React.render(
100129
<CommentBox url="/api/comments" pollInterval={2000} />

0 commit comments

Comments
 (0)