Skip to content

Commit 7100219

Browse files
display chat history
1 parent 09fe543 commit 7100219

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

client/Chatroom.jsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,27 @@ export default class Chatroom extends React.Component {
8383
constructor(props, context) {
8484
super(props, context)
8585

86+
const { chatHistory } = props
87+
8688
this.state = {
87-
chatHistory: [],
89+
chatHistory,
8890
input: ''
8991
}
9092

9193
this.onInput = this.onInput.bind(this)
9294
this.onSendMessage = this.onSendMessage.bind(this)
9395
this.onMessageReceived = this.onMessageReceived.bind(this)
9496
this.updateChatHistory = this.updateChatHistory.bind(this)
97+
this.scrollChatToBottom = this.scrollChatToBottom.bind(this)
9598
}
9699

97100
componentDidMount() {
98101
this.props.registerHandler(this.onMessageReceived)
102+
this.scrollChatToBottom()
99103
}
100104

101105
componentDidUpdate() {
102-
this.panel.scrollTo(0, this.panel.scrollHeight)
106+
this.scrollChatToBottom()
103107
}
104108

105109
componentWillUnmount() {
@@ -133,6 +137,10 @@ export default class Chatroom extends React.Component {
133137
this.setState({ chatHistory: this.state.chatHistory.concat(entry) })
134138
}
135139

140+
scrollChatToBottom() {
141+
this.panel.scrollTo(0, this.panel.scrollHeight)
142+
}
143+
136144
render() {
137145
return (
138146
<div style={{ height: '100%' }}>

client/Root.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ export default class Root extends React.Component {
3636
if (!this.state.user)
3737
return onNoUserSelected()
3838

39-
return this.state.client.join(chatroomName, (err) => {
39+
return this.state.client.join(chatroomName, (err, chatHistory) => {
4040
if (err)
4141
return console.error(err)
42-
return onEnterSuccess()
42+
return onEnterSuccess(chatHistory)
4343
})
4444
}
4545

@@ -81,9 +81,12 @@ export default class Root extends React.Component {
8181
return <Redirect to="/" />
8282
}
8383

84+
const { chatHistory } = history.location.state
85+
8486
return (
8587
<Chatroom
8688
chatroom={chatroom}
89+
chatHistory={chatHistory}
8790
user={this.state.user}
8891
onLeave={
8992
() => this.onLeaveChatroom(
@@ -129,7 +132,10 @@ export default class Root extends React.Component {
129132
chatroomName => this.onEnterChatroom(
130133
chatroomName,
131134
() => props.history.push('/user'),
132-
() => props.history.push(chatroomName)
135+
chatHistory => props.history.push({
136+
pathname: chatroomName,
137+
state: { chatHistory }
138+
})
133139
)
134140
}
135141
/>

server/Chatroom.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module.exports = function ({ name, image }) {
66
members.forEach(m => m.emit('message', message))
77
}
88

9-
function addEntry(user, entry) {
10-
chatHistory = chatHistory.concat({ user, entry })
9+
function addEntry(entry) {
10+
chatHistory = chatHistory.concat(entry)
1111
}
1212

1313
function getChatHistory() {

0 commit comments

Comments
 (0)