Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit 9e83ddf

Browse files
author
Dave Martinez
committed
Done with the tutorial
1 parent f541e4f commit 9e83ddf

File tree

2 files changed

+164
-1
lines changed

2 files changed

+164
-1
lines changed

comments.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,20 @@
88
"id": 1420070400000,
99
"author": "Paul O’Shannessy",
1010
"text": "React is *great*!"
11+
},
12+
{
13+
"id": 1464386432512,
14+
"author": "test",
15+
"text": "cool"
16+
},
17+
{
18+
"id": 1464386437963,
19+
"author": "wait",
20+
"text": "what"
21+
},
22+
{
23+
"id": 1464386618159,
24+
"author": "final",
25+
"text": "test"
1126
}
12-
]
27+
]

public/scripts/daves-code.js

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

0 commit comments

Comments
 (0)