File tree 3 files changed +21
-7
lines changed
3 files changed +21
-7
lines changed Original file line number Diff line number Diff line change @@ -83,23 +83,27 @@ export default class Chatroom extends React.Component {
83
83
constructor ( props , context ) {
84
84
super ( props , context )
85
85
86
+ const { chatHistory } = props
87
+
86
88
this . state = {
87
- chatHistory : [ ] ,
89
+ chatHistory,
88
90
input : ''
89
91
}
90
92
91
93
this . onInput = this . onInput . bind ( this )
92
94
this . onSendMessage = this . onSendMessage . bind ( this )
93
95
this . onMessageReceived = this . onMessageReceived . bind ( this )
94
96
this . updateChatHistory = this . updateChatHistory . bind ( this )
97
+ this . scrollChatToBottom = this . scrollChatToBottom . bind ( this )
95
98
}
96
99
97
100
componentDidMount ( ) {
98
101
this . props . registerHandler ( this . onMessageReceived )
102
+ this . scrollChatToBottom ( )
99
103
}
100
104
101
105
componentDidUpdate ( ) {
102
- this . panel . scrollTo ( 0 , this . panel . scrollHeight )
106
+ this . scrollChatToBottom ( )
103
107
}
104
108
105
109
componentWillUnmount ( ) {
@@ -133,6 +137,10 @@ export default class Chatroom extends React.Component {
133
137
this . setState ( { chatHistory : this . state . chatHistory . concat ( entry ) } )
134
138
}
135
139
140
+ scrollChatToBottom ( ) {
141
+ this . panel . scrollTo ( 0 , this . panel . scrollHeight )
142
+ }
143
+
136
144
render ( ) {
137
145
return (
138
146
< div style = { { height : '100%' } } >
Original file line number Diff line number Diff line change @@ -36,10 +36,10 @@ export default class Root extends React.Component {
36
36
if ( ! this . state . user )
37
37
return onNoUserSelected ( )
38
38
39
- return this . state . client . join ( chatroomName , ( err ) => {
39
+ return this . state . client . join ( chatroomName , ( err , chatHistory ) => {
40
40
if ( err )
41
41
return console . error ( err )
42
- return onEnterSuccess ( )
42
+ return onEnterSuccess ( chatHistory )
43
43
} )
44
44
}
45
45
@@ -81,9 +81,12 @@ export default class Root extends React.Component {
81
81
return < Redirect to = "/" />
82
82
}
83
83
84
+ const { chatHistory } = history . location . state
85
+
84
86
return (
85
87
< Chatroom
86
88
chatroom = { chatroom }
89
+ chatHistory = { chatHistory }
87
90
user = { this . state . user }
88
91
onLeave = {
89
92
( ) => this . onLeaveChatroom (
@@ -129,7 +132,10 @@ export default class Root extends React.Component {
129
132
chatroomName => this . onEnterChatroom (
130
133
chatroomName ,
131
134
( ) => props . history . push ( '/user' ) ,
132
- ( ) => props . history . push ( chatroomName )
135
+ chatHistory => props . history . push ( {
136
+ pathname : chatroomName ,
137
+ state : { chatHistory }
138
+ } )
133
139
)
134
140
}
135
141
/>
Original file line number Diff line number Diff line change @@ -6,8 +6,8 @@ module.exports = function ({ name, image }) {
6
6
members . forEach ( m => m . emit ( 'message' , message ) )
7
7
}
8
8
9
- function addEntry ( user , entry ) {
10
- chatHistory = chatHistory . concat ( { user , entry } )
9
+ function addEntry ( entry ) {
10
+ chatHistory = chatHistory . concat ( entry )
11
11
}
12
12
13
13
function getChatHistory ( ) {
You can’t perform that action at this time.
0 commit comments