Now that we have a list of all active presences (we will eventually expand this to include all users, not just currently active presences), we need to give our admin the ability to select one and respond. The UI for this will be clicking on a uuid on the left that represents a user.
This will subscribe the admin to the topic and allow her to respond to the user. It will also grab the most recent 10 messages from the chatroom and use that to populate our Chat component with messages.
Our Chat component is already starting to get complicated, so we're going to create a new component called ChatRoom to handle the actual rendering of messages and we'll leave the socket logic in Chat for now.
$ mkdir app/components/ChatRoom$ touch app/components/ChatRoom/{index.js,style.css,README.md,spec.js}Our ChatRoom component is going to receive a list of messages as props, which it will then render to the page. Our renderMessages function maps over each of these messages and returns a jsx component for each.
import React from "react"import cssModules from "react-css-modules"import style from "./style.css"export class ChatRoom extends React.Component { componentDidUpdate() { if (this.props.messages.length > 0) { const lastMessage = this[`chatMessage:${this.props.messages.length - 1}`] this.chatContainer.scrollTop = lastMessage.offsetTop } } render() { return ( <div ref={ref => { this.chatContainer = ref }} className={style.chatWrapper} > {this.renderMessages(this.props)} </div> ) }}export default cssModules(ChatRoom, style)Then we should add a function to map over each of our messages and render them.
...export class ChatRoom extends React.Component { ... renderMessages() { return this.props.messages.map(({ body, id }, i) => { return ( <div ref={ref => { this[`chatMessage:${i}`] = ref }} key={id}> { body } </div> ) }) } ...}...Then we should add a wrapper to keep this component visible next to the Sidebar. Go ahead and remove the .chatWrapper from ../Chat/style.css as well.
.chatWrapper { margin-left: 300px;}The next step is to connect our Chat component to our room channel and render our new ChatRoom component.
Now we need to connect to our room channel, create a changeRoom function that we can pass to our Sidebar that will allow us to change rooms, and render our ChatRoom component with the list of messages we get from our socket.
...import { default as ChatRoom } from "../ChatRoom"export class Chat extends React.Component { constructor(props) { super(props) this.state = { presences: {}, messages: [], currentRoom: "" } this.changeChatroom = this.changeChatroom.bind(this) } ... changeChatroom(room) { this.channel = this.socket.channel(`room:${room}`) this.setState({ messages: [] }) this.configureRoomChannel(room) } ...}Now we need to configure our room channel to take in this new value.
...export class Chat extends React.Component { ... configureRoomChannel(room) { this.channel.join() .receive("ok", ({ messages }) => { console.log(`Succesfully joined the ${room} chat room.`, messages) this.setState({ messages, currentRoom: room }) }) .receive("error", () => { console.log(`Unable to join the ${room} chat room.`) }) this.channel.on("message", payload => { this.setState({ messages: this.state.messages.concat([payload]) }) }) } ...}...And finally we need to pass our click handler to our Sidebar and pass our messages to our ChatRoom component.
...export class Chat extends React.Component { ... render() { return ( <div> <Sidebar presences={this.state.presences} onRoomClick={this.changeChatroom} /> <ChatRoom messages={this.state.messages} /> { this.props.children } </div> ) } ...}...The last change we need to make is to add an onClick to each of our presence components in the Sidebar list.
...const renderList = props => { return Presence.list(props.presences, listBy) .map(({ id }) => { return ( <div onClick={() => { props.onRoomClick(id) }} key={id}> { id } </div> ) })}...At this point, our admin can receive messages. You should see an anonymous user's id appear in the Sidebar and you should be able to click on that and see incoming messages. You can send messages to this admin from your phoenix-chat component.
The next step is to allow our admin to respond.
Previous: Connect the Frontend to AdminChannelNext: Change Room and Respond to Messages