@@ -10,40 +10,43 @@ export default class Discussions extends React.Component {
1010 super ( props )
1111 this . state = { messages : [ ] } ;
1212 this . _dataValue = this . _dataValue . bind ( this ) ;
13- this . _MessageSend = this . _MessageSend . bind ( this ) ;
13+ this . _Message = this . _Message . bind ( this ) ;
1414 this . user = uid ( 10 ) ;
1515 }
1616
1717 componentDidMount ( ) {
1818
19- this . socket = io ( 'localhost:3000' ) ;
19+ this . socket = io ( 'http:// localhost:3000' ) ;
2020
2121 this . socket . on ( 'message' , ( message ) => {
2222 if ( message . user !== this . user ) {
23- this . _MessageSend ( message ) ;
23+ this . _Message ( message ) ;
2424 }
2525 } )
26- }
27-
28-
29- _dataValue ( ev ) {
3026
31- ev . preventDefault ( ) ;
32-
33- let textValue = document . getElementById ( 'textarea' ) . value ;
27+ const textValue = document . getElementById ( 'textarea' ) ;
28+ textValue . addEventListener ( 'keypress' , event => {
29+ if ( event . keyCode === 13 ) {
30+ this . _dataValue ( ) ;
31+ event . preventDefault ( ) ;
32+ }
33+ } )
34+ }
3435
36+ _dataValue ( ) {
37+ const textValue = document . getElementById ( 'textarea' ) ;
3538 const avatar = this . props . avatar ;
3639 const username = this . props . username ;
37- let message = { avatar : avatar , username : username , message : textValue } ;
40+ let message = { avatar : avatar , username : username , message : textValue . value } ;
3841
39- if ( textValue !== '' ) {
42+ if ( textValue . value !== '' ) {
4043 this . socket . emit ( 'new-message' , message ) ;
4144 Notification ( message ) ;
42- document . getElementById ( 'textarea' ) . value = '' ;
45+ textValue . value = '' ;
4346 }
4447 }
4548
46- _MessageSend ( message ) {
49+ _Message ( message ) {
4750 this . state . messages . push ( message ) ;
4851 let MessageGlobal = this . state . messages ;
4952 this . setState ( { messages : MessageGlobal } ) ;
@@ -52,24 +55,32 @@ export default class Discussions extends React.Component {
5255 render ( ) {
5356 return < div className = "conversationBody" >
5457 < header className = "header-user" >
55- < h2 className = "header-user-username" >
56- { `${ this . props . username } ` }
57- </ h2 >
5858
59- < figure className = "avatar" >
60- < img src = { this . props . avatar } width = "30" height = "30" />
61- </ figure >
59+ < figure className = "avatar" >
60+ < img src = { this . props . avatar } width = "30" height = "30" />
61+ </ figure >
62+
63+ < h2 className = "header-user-username" >
64+ { `${ this . props . username } ` }
65+ </ h2 >
66+
6267 </ header >
6368
6469 < section className = "Message-list" >
65- < MessageList messages = { this . state . messages } />
66- </ section >
67-
70+ < MessageList
71+ messages = { this . state . messages } />
72+
73+ < form className = "Message-form" method = "POST" >
74+ < textarea
75+ id = "textarea"
76+ rows = "1"
77+ placeholder = "Write message..."
78+ autoComplete = "false"
79+ autoFocus = "true" > </ textarea >
80+ < button type = "submit" > Send</ button >
81+ </ form >
6882
69- < form className = "Message-form" method = "POST" >
70- < textarea id = "textarea" placeholder = "Write message..." > </ textarea >
71- < button type = "submit" onClick = { this . _dataValue } > Send</ button >
72- </ form >
83+ </ section >
7384
7485 </ div >
7586 }
0 commit comments