Skip to content

Commit

Permalink
Enable posting chat messages
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-nagy committed Dec 12, 2016
1 parent 3d3592d commit 99b9f60
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
42 changes: 15 additions & 27 deletions src/app/components/ChatBox.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
import React from 'react';
import MessageList from './MessageList';
import PostMessageForm from './PostMessageForm';
import ClearButton from './ClearButton';

class ChatBox extends React.Component {
constructor( props, context ) {
super( props, context );
this.state = {};
this.state.messages = [
{
id: 1,
timestamp: 1478859071000,
owner: 'Chuck',
text: 'Hello Mark, how is it going?'
},
{
id: 2,
timestamp: 1478859115000,
owner: 'Mark',
text: 'What do you want, Chuck?'
},
{
id: 3,
timestamp: 1478859131000,
owner: 'Chuck',
text: ':D'
},
{
id: 4,
timestamp: 1478859165000,
owner: 'Chuck',
text: 'I forgot you were not into small talk.'
}
];
this.state = {
messages: []
};

this.appendChatMessage = this.appendChatMessage.bind( this );
this.clearMessages = this.clearMessages.bind( this );
}
appendChatMessage( owner, text ) {
let newMessage = {
id: this.state.messages.length + 1,
timestamp: new Date().getTime(),
owner: owner,
text: text
};
this.setState({ messages: [ ...this.state.messages, newMessage ] });
}
clearMessages() {
this.setState( { messages: [] } );
}
render() {
return (
<div>
<MessageList messages={this.state.messages} />
<PostMessageForm appendChatMessage={this.appendChatMessage} />
<ClearButton clearMessages={this.clearMessages} />
</div>
);
Expand Down
30 changes: 30 additions & 0 deletions src/app/components/PostMessageForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';

class PostMessageForm extends React.Component {
constructor( props, context ) {
super( props, context );

this.handleSubmit = this.handleSubmit.bind( this );
}
handleSubmit( event ) {
event.preventDefault();
this.props.appendChatMessage( this.nameInput.value, this.messageInput.value );
this.nameInput.value = '';
this.messageInput.value = '';
}
render() {
return (
<form onSubmit={this.handleSubmit.bind(this)}>

This comment has been minimized.

Copy link
@zsolt-nagy

zsolt-nagy Dec 21, 2016

Author Owner

Line 17 is redundant: .bind( this ) is not required, as context binding is performed in the constructor (Line 7).

Generally, it is better not to do any context binding in the render method for performance reasons, as a new handler is created on each render.

<input type="text"
ref={name => this.nameInput = name}
placeholder="Name" />
<input type="text"
ref={message => this.messageInput = message}
placeholder="Message" />
<input type="submit" value="Send" />
</form>
);
}
}

export default PostMessageForm;

0 comments on commit 99b9f60

Please sign in to comment.