Skip to content

Commit f03456b

Browse files
author
chunwei
committed
day 3
1 parent e95d75f commit f03456b

File tree

12 files changed

+138
-26
lines changed

12 files changed

+138
-26
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*~
2-
node_modules
2+
node_modules/*
33
.DS_Store
4+
.idea/*

.idea/encodings.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,22 @@ This is the React comment box example from [the React tutorial](http://facebook.
1414
There are several simple server implementations included. They all serve static files from `public/` and handle requests to `comments.json` to fetch or add data. Start a server with one of the following:
1515

1616
### Node
17-
17+
>
18+
#### Development
19+
>
20+
```sh
21+
npm install
22+
npm run dev-server ---index.html script: http://localhost:8080/app.js
23+
or
24+
npm run build ---index.html script: public/build/app.js
25+
npm start
26+
```
27+
>
28+
#### Production
29+
>
1830
```sh
1931
npm install
20-
npm run build
32+
npm run deploy
2133
npm start
2234
```
2335

comments.json

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,48 @@
11
[
22
{
3+
"id": 1,
34
"author": "Pete Hunt",
45
"text": "Hey there!",
56
"likedCount": 0
67
},
78
{
9+
"id": 2,
810
"author": "Paul O’Shannessy",
911
"text": "React is *great*!",
10-
"likedCount": 15
12+
"likedCount": 15,
13+
"replys": [
14+
{
15+
"author": "BBB",
16+
"text": "回复2:BBB",
17+
"likedCount": "0",
18+
"parentId": "2"
19+
}
20+
]
1121
},
1222
{
23+
"id": 3,
1324
"author": "小明",
1425
"text": "我是晓明,come on baby。我是晓明,come on baby。我是晓明,come on baby。我是晓明,come on baby。",
15-
"likedCount": 15
26+
"likedCount": 15,
27+
"replys": [
28+
{
29+
"id": 4,
30+
"author": "Baby",
31+
"text": "滚粗!",
32+
"likedCount": 10
33+
},
34+
{
35+
"id": 5,
36+
"author": "晓明",
37+
"text": "谁敢冒充哥?!",
38+
"likedCount": 5
39+
},
40+
{
41+
"author": "AAA",
42+
"text": "回复3:AAA",
43+
"likedCount": "0",
44+
"parentId": "3"
45+
}
46+
]
1647
}
17-
]
48+
]

server.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,22 @@ app.get('/comments.json', function(req, res) {
3232
app.post('/comments.json', function(req, res) {
3333
fs.readFile('comments.json', function(err, data) {
3434
var comments = JSON.parse(data);
35-
comments.push(req.body);
35+
var comment=req.body;
36+
var pId=comment.parentId;
37+
if(pId&&pId!=0){//这是回复评论
38+
var parentComment;
39+
for(var item of comments){
40+
if(item.id==pId) {
41+
parentComment=item;
42+
break;
43+
}
44+
}
45+
if(parentComment.replys==undefined)parentComment.replys=[];
46+
parentComment.replys.push(comment);
47+
}else{
48+
comments.push(comment);
49+
}
50+
//comments.push(req.body);
3651
fs.writeFile('comments.json', JSON.stringify(comments, null, 4), function(err) {
3752
res.setHeader('Cache-Control', 'no-cache');
3853
res.json(comments);

src/components/comment/Comment.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ var CommentContent = React.createClass({
3131
});
3232
var CommentFooter = React.createClass({
3333
render: function () {
34+
let comment_id=this.props.comment_id;
35+
let replyUrl='/reply/'+comment_id;
36+
3437
return (
3538
<div className="commentFooter">
3639
<span className="time">25分钟前</span>
37-
<span className="reply"><Link to='/reply/1'>回复</Link></span>
40+
<span className="reply"><Link to={replyUrl}>回复</Link></span>
3841
</div>
3942
);
4043
}
@@ -46,7 +49,8 @@ var Comment = React.createClass({
4649
<div className="comment">
4750
<CommentHeader author={this.props.comment.author} likedCount={this.props.comment.likedCount}/>
4851
<CommentContent>{this.props.comment.text}</CommentContent>
49-
<CommentFooter/>
52+
<CommentFooter comment_id={this.props.comment.id}/>
53+
{this.props.children}
5054
</div>
5155
);
5256
}

src/components/comment/CommentBar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var CommentBar = React.createClass({
99
render: function () {
1010
return (
1111
<div className="commentBar">
12-
<Link to='/post' className="BtnOpenForm">我也来说说</Link>
12+
<Link to='/reply/0' className="BtnOpenForm">我也来说说</Link>
1313
</div>
1414
)
1515
}

src/components/comment/CommentBox.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,22 @@ var CommentBox = React.createClass({
2525
},
2626
handleCommentSubmit: function (comment) {
2727
var comments = this.state.data;
28-
var newComments = comments.concat([comment]);
29-
this.setState({data: newComments});
28+
let pId=comment.parentId;
29+
if(pId&&pId!=0){//这是回复评论
30+
var parentComment;
31+
for(var item of comments){
32+
if(item.id==pId) {
33+
parentComment=item;
34+
break;
35+
}
36+
}
37+
if(parentComment.replys==undefined)parentComment.replys=[];
38+
parentComment.replys.push(comment);
39+
}else{
40+
comments.push(comment);
41+
}
42+
//var newComments = comments.concat([comment]);
43+
this.setState({data: comments});
3044
$.ajax({
3145
url: this.props.url,
3246
dataType: 'json',
@@ -58,10 +72,16 @@ var CommentBox = React.createClass({
5872
},
5973
componentDidMount: function () {
6074
this.loadCommentsFromServer();
61-
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
75+
//setInterval(this.loadCommentsFromServer, this.props.pollInterval);
6276
},
6377
render: function () {
64-
78+
let children=this.props.children;
79+
//console.log('Children.count=',React.Children.count(children));
80+
React.Children.forEach(children,function (child,i) {
81+
//console.log('child=',child);
82+
//console.log('thisArg=',this);
83+
child.props.onCommentSubmit=this.handleCommentSubmit;
84+
},this);
6585
return (
6686
<div className="commentBox">
6787
<h1>Comments - 评论</h1>

src/components/comment/CommentForm.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,27 @@ var CommentForm = React.createClass({
1212
if (!text || !author) {
1313
return;
1414
}
15-
this.props.onCommentSubmit({author: author, text: text, likedCount: 0});
15+
console.log(author,text);
16+
var pid =parseInt(this.props.params.id);
17+
this.props.onCommentSubmit({author: author, text: text, likedCount: 0,parentId:pid});
1618
React.findDOMNode(this.refs.author).value = '';
1719
React.findDOMNode(this.refs.text).value = '';
20+
},
21+
getCommentById: function (id) {
22+
return comment;
23+
},
24+
componentDidMount: function(){
25+
1826
},
1927
render: function () {
20-
var id = this.props.params.id;
21-
console.log('key=', id);
28+
var pid = this.props.params.id;
29+
var replyTo=pid?`回复${pid}:`:'';
2230
return (
2331
<div className="commentFormHolder">
2432
<form className="commentForm" onSubmit={this.handleSubmit}>
25-
<input ref="author" type="text" placeholder="Your name"/>
33+
<input ref="author" type="text" placeholder="Your name"/>
2634

27-
<p><textarea ref="text" className="content-textarea" rows="5" placeholder="你有什么看法呢?"></textarea></p>
35+
<p><textarea ref="text" defaultValue={replyTo} className="content-textarea" rows="5" placeholder="我有话说"></textarea></p>
2836
<input type="submit" value="发表评论" className="submit"/>
2937
</form>
3038
</div>

src/components/comment/CommentList.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@ import Comment from './Comment.js';
77

88
var CommentList = React.createClass({
99
render: function () {
10-
var commentNodes = this.props.data.map(function (comment, index) {
10+
var comments = this.props.data.map(function (comment, index) {
11+
var replys=!!!comment.replys?'':comment.replys.map(function(reply,index){
12+
return(
13+
<Comment author={reply.author} key={index} comment={reply} />
14+
);
15+
});
1116
return (
1217
// `key` is a React-specific concept and is not mandatory for the
1318
// purpose of this tutorial. if you're curious, see more here:
1419
// http://facebook.github.io/react/docs/multiple-components.html#dynamic-children
1520
<Comment author={comment.author} key={index} comment={comment}>
16-
{comment.text}
21+
<div className="replys">
22+
{replys}
23+
</div>
1724
</Comment>
25+
1826
);
1927
});
2028
return (
2129
<div className="commentList">
22-
{commentNodes}
30+
{comments}
2331
</div>
2432
);
2533
}

src/components/comment/comment.css

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ button, html input[type="button"], input[type="reset"], input[type="submit"] {
77
margin-top: 20px;
88
border-bottom: solid 1px #eee;
99
}
10-
10+
.comment .replys{
11+
margin-left:50px;
12+
}
1113
.commentHeader, .commentContent, .commentFooter {
1214
position: relative;
1315
overflow: hidden;
@@ -100,7 +102,9 @@ button, html input[type="button"], input[type="reset"], input[type="submit"] {
100102
border-top: solid 1px #ccc;
101103
background-color: #fff;
102104
}
103-
105+
.commentBar a{
106+
text-decoration: none;
107+
}
104108
.commentBar .BtnOpenForm {
105109
display: inline-block;
106110
text-align: center;

src/pages/index/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ var App = React.createClass({
1111
render: function () {
1212
return (
1313
<div>
14-
<CommentBox url="comments.json" pollInterval={2000}/>
14+
<CommentBox url="comments.json" pollInterval={2000}>
1515
{this.props.children}
16+
</CommentBox>
1617
</div>
1718
);
1819
}
@@ -22,7 +23,6 @@ var App = React.createClass({
2223
React.render((
2324
<Router>
2425
<Route path="/" component={App}>
25-
<Route path="post" component={CommentForm}/>
2626
<Route path="reply/:id" component={CommentForm}/>
2727
</Route>
2828
</Router>

0 commit comments

Comments
 (0)