-
Notifications
You must be signed in to change notification settings - Fork 0
/
react.js
159 lines (132 loc) · 4.3 KB
/
react.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var Comment = React.createClass({
rawMarkup: function(){
var rawMarkup = marked( this.props.children.toString(), {santize:true} );
return { __html: rawMarkup };
},
render: function(){
return(
<div className = "comment">
<h2 className= "commentAuthor"> {this.props.author}</h2>
<span dangerouslySetInnerHTML = {this.rawMarkup()} />
</div>
);
}
});
var CommentList = React.createClass({
render: function(){
var commentNodes = this.props.data.map(function(comment){
return (
<Comment author= {comment.name} key = {comment.id}>
{comment.msg}
</Comment>
);
});
return(
<div className = "commentList">
{commentNodes}
</div>
);
}
});
var CommentForm = React.createClass({
getInitialState :function(){
return {author: '', text: ''};
},
handleAuthorChange :function(e){
console.log("okay");
this.setState({author: e.target.value});
},
handleTextChange:function(e){
this.setState({ text: e.target.value});
},
handleSubmit:function(e)
{
e.preventDefault();
var author = this.state.author.trim();
var text = this.state.text.trim();
if(!text || !author)return;
this.props.onCommentSubmit({name: author, msg: text });
this.setState({author:'', text:''});
},
render:function() {
return (
<form className = "commentForm" >
<input type = "text"
placeholder = "Your name"
value= {this.state.author}
onChange = {this.handleAuthorChange} />
<input type = "text"
placeholder = "Your Comment"
value = {this.state.text}
onChange = {this.handleTextChange} />
<input type = "submit" onClick = {this.handleSubmit}/>
</form>
);
}
});
var CommentBox = React.createClass({
handleCommentSubmit:function(comment)
{
var tmp = this.state.data;
comment.id = this.state.data[tmp.length].id;
tmp.push(comment);
var res = {
cmd : "insertData",
data : comment
};
//console.log(JSON.stringify(res));
$.ajax({
url : this.props.url,
dataType : "json",
type : "POST",
data : JSON.stringify(res),
success :function(e){
console.log(e);
}.bind(this),
error:function(error){
}.bind(this)
});
this.setState({data:tmp});
//console.log(JSON.stringify(this.state.data));
},
loadCommentsFromServer:function(){
var command = {
cmd : "load"
};
$.ajax({
url : this.props.url,
dataType : 'json',
cache : false,
data : JSON.stringify(command),
success :function(data){
//console.log(JSON.stringify(data));
//var data = JSON.parse(JSON.stringify(data));
this.setState({data:data});
console.log(JSON.toString(data));
}.bind(this),
error:function(error){
console.error(this.props.url, status, error.toString());
}.bind(this)
});
},
componentDidMount:function(){
this.loadCommentsFromServer();
setInterval(this.loadCommentsFromServer,this.props.pollInterval);
},
getInitialState:function(){
return {data:[]};
},
render: function() {
return (
<div className="commentBox">
<h2> Comments </h2>
<CommentList data = {this.state.data} />
<CommentForm onCommentSubmit= {this.handleCommentSubmit}/>
</div>
);
}
});
ReactDOM.render(
<CommentBox url = "comments.json" pollInterval = {2000} />,
document.getElementById('content')
);