Last active
October 22, 2016 18:29
-
-
Save bnhansn/08deab7d833f2dd98037b9c90c2d4b76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // @flow | |
| import React, { Component } from 'react'; | |
| import { connect } from 'react-redux'; | |
| import { connectToChannel, leaveChannel, createMessage } from '../../actions/room'; | |
| import MessageList from '../../components/MessageList'; | |
| import MessageForm from '../../components/MessageForm'; | |
| import RoomNavbar from '../../components/RoomNavbar'; | |
| import RoomSidebar from '../../components/RoomSidebar'; // new line | |
| type MessageType = { | |
| id: number, | |
| } | |
| type Props = { | |
| socket: any, | |
| channel: any, | |
| room: Object, | |
| params: { | |
| id: number, | |
| }, | |
| connectToChannel: () => void, | |
| leaveChannel: () => void, | |
| createMessage: () => void, | |
| messages: Array<MessageType>, | |
| presentUsers: Array, | |
| currentUser: Object, | |
| } | |
| class Room extends Component { | |
| componentDidMount() { | |
| this.props.connectToChannel(this.props.socket, this.props.params.id); | |
| } | |
| componentWillReceiveProps(nextProps) { | |
| if (nextProps.params.id !== this.props.params.id) { | |
| this.props.leaveChannel(this.props.channel); | |
| this.props.connectToChannel(nextProps.socket, nextProps.params.id); | |
| } | |
| if (!this.props.socket && nextProps.socket) { | |
| this.props.connectToChannel(nextProps.socket, nextProps.params.id); | |
| } | |
| } | |
| componentWillUnmount() { | |
| this.props.leaveChannel(this.props.channel); | |
| } | |
| props: Props | |
| handleMessageCreate = (data) => { | |
| this.props.createMessage(this.props.channel, data); | |
| } | |
| render() { | |
| return ( | |
| <div style={{ display: 'flex', height: '100vh' }}> | |
| <RoomSidebar // new component | |
| room={this.props.room} | |
| currentUser={this.props.currentUser} | |
| presentUsers={this.props.presentUsers} | |
| /> | |
| <div style={{ display: 'flex', flexDirection: 'column' }}> | |
| <RoomNavbar room={this.props.room} /> | |
| <MessageList messages={this.props.messages} /> | |
| <MessageForm onSubmit={this.handleMessageCreate} /> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| } | |
| export default connect( | |
| state => ({ | |
| room: state.room.currentRoom, | |
| socket: state.session.socket, | |
| channel: state.room.channel, | |
| messages: state.room.messages, | |
| presentUsers: state.room.presentUsers, // new line | |
| currentUser: state.session.currentUser, // new line | |
| }), | |
| { connectToChannel, leaveChannel, createMessage } | |
| )(Room); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment