Skip to content

Commit 4cd451b

Browse files
author
Matt Calthrop
committed
Add tutorial18-19
Also add check if required properties have been set on CommentForm
1 parent 9fcc27b commit 4cd451b

File tree

3 files changed

+141
-2
lines changed

3 files changed

+141
-2
lines changed

public/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<!--<script type="text/babel" src="scripts/tutorial11-13.js"></script>-->
2424
<!--<script type="text/babel" src="scripts/tutorial14.js"></script>-->
2525
<!--<script type="text/babel" src="scripts/tutorial15-16.js"></script>-->
26-
<script type="text/babel" src="scripts/tutorial17.js"></script>
26+
<!--<script type="text/babel" src="scripts/tutorial17.js"></script>-->
27+
<script type="text/babel" src="scripts/tutorial18-19.js"></script>
2728
</body>
2829
</html>

public/scripts/tutorial17.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global $, React, ReactDOM */
1+
/* global $, _, React, ReactDOM */
22

33
var Comment = React.createClass({
44
rawMarkup: function () {

public/scripts/tutorial18-19.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/* global $, _, React, ReactDOM, marked */
2+
3+
var Comment = React.createClass({
4+
rawMarkup: function () {
5+
var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
6+
7+
return {__html: rawMarkup};
8+
},
9+
render: function () {
10+
return (
11+
<div className="comment">
12+
<h2 className="commentAuthor">
13+
{this.props.author}
14+
</h2>
15+
<span dangerouslySetInnerHTML={this.rawMarkup()}/>
16+
</div>
17+
);
18+
}
19+
});
20+
21+
var CommentList = React.createClass({
22+
render: function () {
23+
var commentNodes = this.props.data.map(function (comment) {
24+
return (
25+
<Comment key={comment.id} author={comment.author}>
26+
{comment.text}
27+
</Comment>
28+
);
29+
});
30+
31+
return (
32+
<div className="commentList">
33+
{commentNodes}
34+
</div>
35+
);
36+
}
37+
});
38+
39+
var CommentForm = React.createClass({
40+
initialState: {
41+
author: '',
42+
text: ''
43+
},
44+
getInitialState: function () {
45+
return _.clone(this.initialState);
46+
},
47+
handleAuthorChange: function (e) {
48+
this.setState({author: e.target.value});
49+
},
50+
handleTextChange: function (e) {
51+
this.setState({text: e.target.value});
52+
},
53+
handleSubmit: function (e) {
54+
e.preventDefault();
55+
var author = this.state.author.trim();
56+
var text = this.state.text.trim();
57+
58+
if (text || author) {
59+
this.props.onCommentSubmit({
60+
author: author,
61+
text: text
62+
});
63+
this.setState(this.getInitialState());
64+
}
65+
},
66+
componentDidMount: function () {
67+
// check if required properties have been set
68+
if (!this.props.onCommentSubmit) {
69+
throw new Error('onCommentSubmit property not set');
70+
}
71+
},
72+
render: function () {
73+
return (
74+
<form
75+
className="commentForm"
76+
onSubmit={this.handleSubmit}>
77+
<input
78+
type="text"
79+
placeholder="Your name"
80+
value={this.state.author}
81+
onChange={this.handleAuthorChange}
82+
/>
83+
<input
84+
type="text"
85+
placeholder="Say something&hellip;"
86+
value={this.state.text}
87+
onChange={this.handleTextChange}
88+
/>
89+
<input
90+
type="submit"
91+
value="Post"
92+
/>
93+
</form>
94+
);
95+
}
96+
});
97+
98+
var CommentBox = React.createClass({
99+
loadCommentsFromServer: function () {
100+
$.ajax({
101+
url: this.props.url,
102+
dataType: 'json',
103+
cache: false,
104+
success: function (data) {
105+
this.setState({data: data});
106+
}.bind(this),
107+
error: function (xhr, status, err) {
108+
console.error(this.props.url, status, err.toString());
109+
}.bind(this)
110+
});
111+
},
112+
handleCommentSubmit: function (comment) {
113+
// TODO: submit to the server and refresh the list
114+
},
115+
getInitialState: function () {
116+
return {
117+
data: []
118+
};
119+
},
120+
componentDidMount: function () {
121+
this.loadCommentsFromServer();
122+
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
123+
},
124+
render: function () {
125+
return (
126+
<div className="commentBox">
127+
<h1>Comments</h1>
128+
<CommentList data={this.state.data}/>
129+
<CommentForm onCommentSubmit={this.handleCommentSubmit}/>
130+
</div>
131+
);
132+
}
133+
});
134+
135+
ReactDOM.render(
136+
<CommentBox url="/api/comments" pollInterval={5000}/>,
137+
document.getElementById('content')
138+
);

0 commit comments

Comments
 (0)