-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment.js
131 lines (124 loc) · 3.21 KB
/
comment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"use strict";
var Timer = React.createClass({
getInitialState: function(){
return { now: moment() };
},
componentDidMount:function(){
setInterval(this.tictac, 1000);
},
tictac: function(){
this.setState({
now : moment()
});
},
render:function(){
return(
<span>
{this.props.start.from(this.state.now)}
</span>
);
}
});
var Comment = React.createClass({
render:function(){
return(
<div className="comment">
<b>{this.props.author}</b> wrote
<Timer start={this.props.createDate}/>
<p>{this.props.text}</p>
</div>
);
}
});
var CommentList = React.createClass({
getInitialState: function(){
return {
comments : [
{
author: "John Doe",
text:"Hello Mary, how are you ?",
createDate: moment().subtract(15, 'minutes')
},{
author: "Mary ",
text:"I'm fine thank you, and you ?",
createDate: moment().subtract(8, 'minutes')
},{
author: "John Doe ",
text:"I'm fine too. See you later",
createDate: moment().subtract(2, 'minutes')
},{
author: "Mary",
text:"See you :-)",
createDate: moment().subtract(43, 'seconds')
}
]
}
},
addComment: function(text, author){
var newComment = [{
author: author,
text: text,
createDate: moment()
}];
this.setState({
comments: this.state.comments.concat(newComment)})
},
render:function(){
var commentNodes = this.state.comments.map(function (comment, index) {
return (
<Comment
author={comment.author}
text={comment.text}
createDate={comment.createDate}
key={index}
/>
);
});
return (
<div className="commentList">
{commentNodes}
<CommentForm onAddComment={this.addComment}/>
</div>
);
}
});
var CommentForm = React.createClass({
getInitialState: function(){
return {
author : "",
text : ""
}
},
changeText: function(e){
this.setState({
text : e.target.value
});
},
changeAuthor: function(e){
this.setState({
author : e.target.value
});
},
addComment: function(e){
this.props.onAddComment(this.state.text, this.state.author);
this.setState({
author : "",
text: ""
});
},
render:function(){
return (
<div>
<input type="text" placeholder="Name" onChange={this.changeAuthor} value={this.state.author}/>
<input id="comment" type="text" placeholder="Message" onChange={this.changeText} value={this.state.text} />
<input type="submit" value="Envoyer" onClick={this.addComment}/>
</div>
);
}
});
ReactDOM.render(
<div>
<CommentList />
</div>,
document.getElementById('container')
);