|
1 | 1 | (function() {
|
2 | 2 | 'use strict';
|
3 | 3 |
|
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 |
| - ]; |
8 | 4 | let Comment = React.createClass({
|
9 | 5 | rawMarkup: function() {
|
10 | 6 | let md = new Remarkable();
|
|
37 | 33 | }.bind(this)
|
38 | 34 | });
|
39 | 35 | },
|
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 | + }, |
41 | 58 | getInitialState: function() {
|
42 | 59 | return {data: []};
|
43 | 60 | },
|
|
52 | 69 | <div className="commentBox">
|
53 | 70 | <h1>Comments</h1>
|
54 | 71 | <CommentList data={this.state.data} />
|
55 |
| - <CommentForm /> |
| 72 | + <CommentForm onCommentSubmit={this.handleCommentSubmit} /> |
56 | 73 | </div>
|
57 | 74 | );
|
58 | 75 | }
|
|
76 | 93 | });
|
77 | 94 |
|
78 | 95 | 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 | + }, |
79 | 115 | render: function() {
|
80 | 116 | 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> |
84 | 132 | );
|
85 | 133 | }
|
86 | 134 | });
|
87 | 135 | ReactDOM.render(
|
88 |
| - <CommentBox url="/api/comments" pollInterval={2000}/>, |
| 136 | + <CommentBox url="/api/comments" pollInterval={10000}/>, |
89 | 137 | document.getElementById('content')
|
90 | 138 | );
|
91 | 139 | })();
|
0 commit comments