Skip to content

Commit 04afcba

Browse files
committed
Working through tutorial
1 parent 6590ed4 commit 04afcba

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed

comments.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
33
"author": "Pete Hunt",
4-
"text": "Hey there!"
4+
"text": "Hey tccchere!"
55
}
66
]

server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var express = require('express');
1616
var bodyParser = require('body-parser');
1717
var app = express();
1818

19-
app.use('/', express.static(path.join(__dirname, 'public')));
19+
app.use('/', express.static(path.join(__dirname, 'tutorial')));
2020
app.use(bodyParser.json());
2121
app.use(bodyParser.urlencoded({extended: true}));
2222

tutorial/index.html

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<!-- index.html -->
2+
<html>
3+
<head>
4+
<title>Hello React</title>
5+
<script src="http://fb.me/react-0.12.2.js"></script>
6+
<script src="http://fb.me/JSXTransformer-0.12.2.js"></script>
7+
<script src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
8+
<script src="http://cdnjs.cloudflare.com/ajax/libs/showdown/0.3.1/showdown.min.js"></script>
9+
</head>
10+
<body>
11+
<div id="content"></div>
12+
<script type="text/jsx">
13+
var converter = new Showdown.converter();
14+
15+
var CommentList = React.createClass({
16+
render: function() {
17+
var commentNodes = this.props.data.map(function (comment) {
18+
return (
19+
<Comment author={comment.author}>
20+
{comment.text}
21+
</Comment>
22+
);
23+
});
24+
return (
25+
<div className="commentList">
26+
{commentNodes}
27+
</div>
28+
);
29+
}
30+
});
31+
32+
var CommentForm = React.createClass({
33+
handleSubmit: function(e) {
34+
e.preventDefault();
35+
var author = this.refs.author.getDOMNode().value.trim();
36+
var text = this.refs.text.getDOMNode().value.trim();
37+
if (!text || !author) {
38+
return;
39+
}
40+
// TODO: send request to the server
41+
this.refs.author.getDOMNode().value = '';
42+
this.refs.text.getDOMNode().value = '';
43+
},
44+
render: function() {
45+
return (
46+
<form className="commentForm" onSubmit={this.handleSubmit}>
47+
<input type="text" placeholder="Your name" ref="author" />
48+
<input type="text" placeholder="Say something..." ref="text" />
49+
<input type="submit" value="Post" />
50+
</form>
51+
);
52+
}
53+
});
54+
55+
var CommentBox = React.createClass({
56+
loadCommentsFromServer: function() {
57+
$.ajax({
58+
url: this.props.url,
59+
dataType: 'json',
60+
success: function(data) {
61+
this.setState({data: data});
62+
}.bind(this),
63+
error: function(xhr, status, err) {
64+
console.error(this.props.url, status, err.toString());
65+
}.bind(this)
66+
});
67+
},
68+
handleCommentSubmit: function(comment) {
69+
// TODO: submit to the server and refresh the list
70+
},
71+
getInitialState: function() {
72+
return {data: []};
73+
},
74+
componentDidMount: function() {
75+
this.loadCommentsFromServer();
76+
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
77+
},
78+
render: function() {
79+
return (
80+
<div className="commentBox">
81+
<h1>Comments</h1>
82+
<CommentList data={this.state.data} />
83+
<CommentForm onCommentSubmit={this.handleCommentSubmit} />
84+
</div>
85+
);
86+
}
87+
});
88+
89+
var Comment = React.createClass({
90+
render: function() {
91+
var rawMarkup = converter.makeHtml(this.props.children.toString());
92+
return (
93+
<div className="comment">
94+
<h2 className="commentAuthor">
95+
{this.props.author}
96+
</h2>
97+
<span dangerouslySetInnerHTML={{__html: rawMarkup}} />
98+
</div>
99+
);
100+
}
101+
});
102+
103+
var data = [
104+
{author: "Pete Hunt", text: "This is one comment"},
105+
{author: "Jordan Walke", text: "This is *another* comment"}
106+
];
107+
108+
React.render(
109+
<CommentBox url="comments.json" pollInterval={2000} />,
110+
document.getElementById('content')
111+
);
112+
</script>
113+
</body>
114+
</html>

0 commit comments

Comments
 (0)