Now that we have the ability to receive messages from our anonymous users, we need to be able to respond to them.
In order to respond to messages, we're going to need an input and a way to submit that message. We're going to use a controlled input to handle our message data. We will cover controlled inputs in greater detail in a future lesson, but for now you can think of them as a way to store values in state rather than in the DOM element itself.
The first function we're adding is handleMessageSubmit, which we will attach to an event (e) on onKeyDown on our input. If the key that was pressed happens to have the keyCode of 13, then it's the return key. If the return key is pressed, we are currently in a room, and there is currently a value in the input, we push a message to the room channel with the current room, the current value of the input, and a timestamp. Then we reset the input's value.
The second function is handleChange, which listens for a change to the input field and updates this.state.input with the new value.
The last function we're adding creates the input element.
...export class Chat extends React.Component { constructor(props) { super(props) this.state = { presences: {}, messages: [], input: "", currentRoom: "" } this.changeChatroom = this.changeChatroom.bind(this) this.handleMessageSubmit = this.handleMessageSubmit.bind(this) this.handleChange = this.handleChange.bind(this) } ... handleChange(e) { this.setState({ input: e.target.value }) } handleMessageSubmit(e) { if (e.keyCode === 13 && this.state.currentRoom && this.state.input) { this.channel.push("message", { room: this.state.currentRoom, body: this.state.input, timestamp: (new Date()).getTime() }) this.setState({ input: "" }) } } ...}Then we need to pass along the handler functions to our ChatRoom component so we can connect them to the onKeyDown and onChange events of our input.
...export class Chat extends React.Component { ... render() { return ( <div> <Sidebar presences={this.state.presences} onRoomClick={this.changeChatroom} /> <ChatRoom input={this.state.input} currentRoom={this.state.currentRoom} handleChange={this.handleChange} handleMessageSubmit={this.handleMessageSubmit} messages={this.state.messages} /> { this.props.children } </div> ) }}..The next step is to add a basic label to each of our messages so we can tell difference between messages sent from our anonymous users and our admin.
import React from "react"import cssModules from "react-css-modules"import style from "./style.css"export class ChatRoom extends React.Component { ... renderMessages() { return this.props.messages.map(({ body, id, user_id, anonymous_user_id }, i) => { const from = user_id ? 'Me' : anonymous_user_id.substring(0,10) const msg = `${from}: ${body}` return ( <div ref={ref => { this[`chatMessage:${i}`] = ref }} key={id}> { msg } </div> ) }) } ...}...Now we should add an input box so our admin can respond to messages sent by the anonymous users. We're going to take the event handlers we passed in from our Chat component and attach them to events in the input box. Recall that when an onKeyDown event is triggered with the return key, the message is submitted.
...export class ChatRoom extends React.Component { ... renderInput() { if (!this.props.currentRoom) return null return ( <div className={style.inputWrapper}> <input value={this.props.input} onKeyDown={this.props.handleMessageSubmit} onChange={this.props.handleChange} className={style.input} /> </div> ) } render() { return ( <div className={style.container}> <div ref={ref => { this.chatContainer = ref }} className={style.chatWrapper}> { this.renderMessages(this.props) } </div> { this.renderInput(this.props) } </div> ) }}...Let's also add some basic styling to make our input easier to use.
.container { position: relative; height: 100vh; margin-left: 300px;}.chatWrapper { position: relative; height: calc(100vh - 60px); padding-left: 20px; padding-top: 10px; padding-bottom: 10px; overflow-y: scroll;}.input { position: absolute; bottom: 20px; left: 20px; width: calc(100% - 40px); line-height: 40px; font-size: 20px; outline: none; border: 1px solid #ccc; border-radius: 3px; padding-left: 10px; color: #333;}Now when you enter information into the input and press return, you can send messages back to the anonymous user.