2017-04-22 12:53:48 +00:00
|
|
|
import { Component } from 'react'
|
|
|
|
import io from 'socket.io-client'
|
2017-11-04 14:05:16 +00:00
|
|
|
import fetch from 'isomorphic-unfetch'
|
2017-04-22 12:53:48 +00:00
|
|
|
|
|
|
|
class HomePage extends Component {
|
|
|
|
// fetch old messages data from the server
|
|
|
|
static async getInitialProps ({ req }) {
|
|
|
|
const response = await fetch('http://localhost:3000/messages')
|
|
|
|
const messages = await response.json()
|
|
|
|
return { messages }
|
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
messages: []
|
|
|
|
}
|
|
|
|
|
|
|
|
// init state with the prefetched messages
|
|
|
|
state = {
|
|
|
|
field: '',
|
|
|
|
messages: this.props.messages
|
|
|
|
}
|
|
|
|
|
|
|
|
// connect to WS server and listen event
|
|
|
|
componentDidMount () {
|
2017-06-08 17:11:08 +00:00
|
|
|
this.socket = io()
|
2017-04-22 12:53:48 +00:00
|
|
|
this.socket.on('message', this.handleMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// close socket connection
|
|
|
|
componentWillUnmount () {
|
|
|
|
this.socket.off('message', this.handleMessage)
|
|
|
|
this.socket.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// add messages from server to the state
|
|
|
|
handleMessage = (message) => {
|
|
|
|
this.setState(state => ({ messages: state.messages.concat(message) }))
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = event => {
|
|
|
|
this.setState({ field: event.target.value })
|
|
|
|
}
|
|
|
|
|
|
|
|
// send messages to server and add them to the state
|
|
|
|
handleSubmit = event => {
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
// create message object
|
|
|
|
const message = {
|
|
|
|
id: (new Date()).getTime(),
|
|
|
|
value: this.state.field
|
|
|
|
}
|
|
|
|
|
|
|
|
// send object to WS server
|
|
|
|
this.socket.emit('message', message)
|
|
|
|
|
|
|
|
// add it to state and clean current input value
|
|
|
|
this.setState(state => ({
|
|
|
|
field: '',
|
|
|
|
messages: state.messages.concat(message)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<main>
|
|
|
|
<div>
|
|
|
|
<ul>
|
|
|
|
{this.state.messages.map(message =>
|
|
|
|
<li key={message.id}>{message.value}</li>
|
|
|
|
)}
|
|
|
|
</ul>
|
|
|
|
<form onSubmit={this.handleSubmit}>
|
|
|
|
<input
|
|
|
|
onChange={this.handleChange}
|
|
|
|
type='text'
|
|
|
|
placeholder='Hello world!'
|
|
|
|
value={this.state.field}
|
|
|
|
/>
|
|
|
|
<button>Send</button>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</main>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default HomePage
|