Skip to content

Commit 39190b3

Browse files
author
Matt Calthrop
committed
Add tutorial15
1 parent 5e46137 commit 39190b3

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

public/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<!--<script type="text/babel" src="scripts/tutorial6-7.js"></script>-->
2121
<!--<script type="text/babel" src="scripts/tutorial8-10.js"></script>-->
2222
<!--<script type="text/babel" src="scripts/tutorial11-13.js"></script>-->
23-
<script type="text/babel" src="scripts/tutorial14.js"></script>
23+
<!--<script type="text/babel" src="scripts/tutorial14.js"></script>-->
24+
<script type="text/babel" src="scripts/tutorial15.js"></script>
2425
</body>
2526
</html>

public/scripts/tutorial15.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/* global $, React, ReactDOM */
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+
getInitialState: function () {
41+
return {
42+
author: '',
43+
text: ''
44+
};
45+
},
46+
handleAuthorChange: function (e) {
47+
this.setState({author: e.target.value});
48+
},
49+
handleTextChange: function (e) {
50+
this.setState({text: e.target.value});
51+
},
52+
render: function () {
53+
return (
54+
<form className="commentForm">
55+
<input
56+
type="text"
57+
placeholder="Your name"
58+
value={this.state.author}
59+
onChange={this.handleAuthorChange}
60+
/>
61+
<input
62+
type="text"
63+
placeholder="Say something&hellip;"
64+
value={this.state.text}
65+
onChange={this.handleTextChange}
66+
/>
67+
<input
68+
type="submit"
69+
value="Post"
70+
/>
71+
</form>
72+
);
73+
}
74+
});
75+
76+
var CommentBox = React.createClass({
77+
loadCommentsFromServer: function () {
78+
$.ajax({
79+
url: this.props.url,
80+
dataType: 'json',
81+
cache: false,
82+
success: function (data) {
83+
this.setState({data: data});
84+
}.bind(this),
85+
error: function (xhr, status, err) {
86+
console.error(this.props.url, status, err.toString());
87+
}.bind(this)
88+
});
89+
},
90+
getInitialState: function () {
91+
return {
92+
data: []
93+
};
94+
},
95+
componentDidMount: function () {
96+
this.loadCommentsFromServer();
97+
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
98+
},
99+
render: function () {
100+
return (
101+
<div className="commentBox">
102+
<h1>Comments</h1>
103+
<CommentList data={this.state.data}/>
104+
<CommentForm />
105+
</div>
106+
);
107+
}
108+
});
109+
110+
ReactDOM.render(
111+
<CommentBox url="/api/comments" pollInterval={5000}/>,
112+
document.getElementById('content')
113+
);

0 commit comments

Comments
 (0)