31
31
} . bind ( this )
32
32
} ) ;
33
33
} ,
34
+ handleCommentSubmit : function ( comment ) {
35
+ var comments = this . state . data ;
36
+ var newComments = comments . concat ( [ comment ] ) ;
37
+ this . setState ( { data : newComments } ) ;
38
+ $ . ajax ( {
39
+ url : this . props . url ,
40
+ dataType : 'json' ,
41
+ type : 'POST' ,
42
+ data : comment ,
43
+ success : function ( data ) {
44
+ this . setState ( { data : data } ) ;
45
+ } . bind ( this ) ,
46
+ error : function ( xhr , status , err ) {
47
+ console . error ( this . props . url , status , err . toString ( ) ) ;
48
+ } . bind ( this )
49
+ } ) ;
50
+ } ,
34
51
getInitialState : function ( ) {
35
52
return { data : [ ] } ;
36
53
} ,
@@ -66,16 +83,6 @@ <h1>Comments</h1>
66
83
}
67
84
} ) ;
68
85
69
- var CommentForm = React . createClass ( {
70
- render : function ( ) {
71
- return (
72
- < div className = "commentForm" >
73
- Hello, world! I am a CommentForm
74
- </ div >
75
- ) ;
76
- }
77
- } ) ;
78
-
79
86
var Comment = React . createClass ( {
80
87
rawMarkup : function ( ) {
81
88
var rawMarkup = marked ( this . props . children . toString ( ) , { sanitize : true } ) ;
@@ -94,7 +101,29 @@ <h2 className="commentAuthor">
94
101
}
95
102
} ) ;
96
103
97
-
104
+ var CommentForm = React . createClass ( {
105
+ handleSubmit : function ( e ) {
106
+ e . preventDefault ( ) ;
107
+ var author = React . findDOMNode ( this . refs . author ) . value . trim ( ) ;
108
+ var text = React . findDOMNode ( this . refs . text ) . value . trim ( ) ;
109
+ if ( ! text || ! author ) {
110
+ return ;
111
+ }
112
+ this . props . onCommentSubmit ( { author : author , text : text } )
113
+ React . findDOMNode ( this . refs . author ) . value = '' ;
114
+ React . findDOMNode ( this . refs . text ) . value = '' ;
115
+ return ;
116
+ }
117
+ render : function ( ) {
118
+ return (
119
+ < form className = "commentForm" onSubmit = { this . handleSubmit } >
120
+ < input type = "text" placeholder = "Your name" ref = "author" />
121
+ < input type = "text" placeholder = "Say something..." ref = "text" />
122
+ < input type = "submit" value = "Post" />
123
+ </ form >
124
+ ) ;
125
+ }
126
+ } ) ;
98
127
99
128
React . render (
100
129
< CommentBox url = "/api/comments" pollInterval = { 2000 } />
0 commit comments