@@ -4,16 +4,17 @@ import {BASE_URL} from './constants';
44
55
66class AddComment extends React . Component {
7-
7+
88 state = {
99 active : false ,
1010 body : '' ,
1111 }
12-
13- submitComment = ( ) => {
12+
13+ submitComment = ( event ) => {
14+ event . preventDefault ( ) ;
1415 const { _id} = this . props ;
1516 const { body} = this . state ;
16-
17+
1718 fetch ( `${ BASE_URL } /articles/${ _id } /comments` ,
1819 {
1920 method : 'POST' ,
@@ -34,50 +35,56 @@ class AddComment extends React.Component{
3435 this . setState ( { body :'' } ) ;
3536 } ) ;
3637 }
37-
38+
3839 handleKeys = event => {
3940 if ( event . key === 'Enter' ) this . submitComment ( ) ;
4041 }
41-
42+
4243 render ( ) {
43- if ( this . state . active ) return (
44+ const { body, active} = this . state ;
45+ if ( active ) return (
4446 < form >
4547 < input
4648 type = "text"
4749 className = "input field"
4850 autoFocus
4951 onChange = { ( { target :{ value} } ) => this . setState ( { body :value } ) }
5052 onKeyPress = { this . handleKeys }
51- value = { this . state . body }
53+ value = { body }
5254 />
5355 < div className = "field is-grouped" >
5456 < button
57+ type = "button"
5558 className = "button control"
5659 onClick = { this . submitComment }
60+ disabled = { ! body }
5761 >
5862 Submit Comment
5963 </ button >
64+
6065 < button
66+ type = "button"
6167 className = "button control"
6268 onClick = { ( ) => this . setState ( { active :false , body :'' } ) }
6369 >
6470 Cancel
6571 </ button >
6672 </ div >
67-
73+
6874 </ form >
6975 ) ;
70-
76+
7177 else return (
72-
78+
7379 < button
74- className = "button control"
75- onClick = { ( ) => this . setState ( { active :true } ) } >
76- { /* <FontAwesomeIcon icon={faPlus}/> */ } New Comment
80+ className = "button control"
81+ onClick = { ( ) => this . setState ( { active :true } ) }
82+ >
83+ New Comment
7784 </ button >
78-
85+
7986 ) ;
80-
87+
8188 }
8289}
8390
0 commit comments