2017-04-22 12:53:48 +00:00
|
|
|
import { Component } from 'react'
|
2018-06-23 20:17:37 +00:00
|
|
|
import Link from 'next/link'
|
2017-11-04 14:05:16 +00:00
|
|
|
import fetch from 'isomorphic-unfetch'
|
2017-04-22 12:53:48 +00:00
|
|
|
|
2018-06-23 20:17:37 +00:00
|
|
|
class ChatOne extends Component {
|
2017-04-22 12:53:48 +00:00
|
|
|
// fetch old messages data from the server
|
|
|
|
static async getInitialProps ({ req }) {
|
2018-06-23 20:17:37 +00:00
|
|
|
const response = await fetch('http://localhost:3000/messages/chat1')
|
2017-04-22 12:53:48 +00:00
|
|
|
const messages = await response.json()
|
|
|
|
return { messages }
|
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
messages: []
|
|
|
|
}
|
|
|
|
|
|
|
|
// init state with the prefetched messages
|
|
|
|
state = {
|
|
|
|
field: '',
|
2018-06-23 20:17:37 +00:00
|
|
|
newMessage: 0,
|
|
|
|
messages: this.props.messages,
|
|
|
|
subscribe: false,
|
|
|
|
subscribed: false
|
2017-04-22 12:53:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 20:17:37 +00:00
|
|
|
subscribe = () => {
|
|
|
|
if (this.state.subscribe && !this.state.subscribed) {
|
|
|
|
// connect to WS server and listen event
|
|
|
|
this.props.socket.on('message.chat1', this.handleMessage)
|
|
|
|
this.props.socket.on('message.chat2', this.handleOtherMessage)
|
|
|
|
this.setState({ subscribed: true })
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 12:53:48 +00:00
|
|
|
componentDidMount () {
|
2018-06-23 20:17:37 +00:00
|
|
|
this.subscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate () {
|
|
|
|
this.subscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDerivedStateFromProps (props, state) {
|
|
|
|
if (props.socket && !state.subscribe) return { subscribe: true }
|
|
|
|
return null
|
2017-04-22 12:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// close socket connection
|
|
|
|
componentWillUnmount () {
|
2018-06-23 20:17:37 +00:00
|
|
|
this.props.socket.off('message.chat1', this.handleMessage)
|
|
|
|
this.props.socket.off('message.chat2', this.handleOtherMessage)
|
2017-04-22 12:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add messages from server to the state
|
|
|
|
handleMessage = (message) => {
|
|
|
|
this.setState(state => ({ messages: state.messages.concat(message) }))
|
|
|
|
}
|
|
|
|
|
2018-06-23 20:17:37 +00:00
|
|
|
handleOtherMessage = () => {
|
|
|
|
this.setState((prevState) => ({ newMessage: (prevState.newMessage + 1) }))
|
|
|
|
}
|
|
|
|
|
2017-04-22 12:53:48 +00:00
|
|
|
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
|
2018-06-23 20:17:37 +00:00
|
|
|
this.props.socket.emit('message.chat1', message)
|
2017-04-22 12:53:48 +00:00
|
|
|
|
|
|
|
// add it to state and clean current input value
|
|
|
|
this.setState(state => ({
|
|
|
|
field: '',
|
|
|
|
messages: state.messages.concat(message)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<main>
|
|
|
|
<div>
|
2018-06-23 20:17:37 +00:00
|
|
|
<Link href={'/'}><a>{'Chat One'}</a></Link>
|
|
|
|
<br />
|
|
|
|
<Link href={'/clone'}><a>{`Chat Two ${this.state.newMessage > 0 ? `( ${this.state.newMessage} new message )` : ''}`}</a></Link>
|
2017-04-22 12:53:48 +00:00
|
|
|
<ul>
|
|
|
|
{this.state.messages.map(message =>
|
|
|
|
<li key={message.id}>{message.value}</li>
|
|
|
|
)}
|
|
|
|
</ul>
|
2018-06-23 20:17:37 +00:00
|
|
|
<form onSubmit={(e) => this.handleSubmit(e)}>
|
2017-04-22 12:53:48 +00:00
|
|
|
<input
|
|
|
|
onChange={this.handleChange}
|
|
|
|
type='text'
|
|
|
|
placeholder='Hello world!'
|
|
|
|
value={this.state.field}
|
|
|
|
/>
|
|
|
|
<button>Send</button>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</main>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 20:17:37 +00:00
|
|
|
export default ChatOne
|