1
0
forked from notnull/chatz

Added chat window

This commit is contained in:
notnull 2019-07-14 02:33:46 -04:00
parent 477b5cd51a
commit 7051201e0d
6 changed files with 863 additions and 7 deletions

775
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,13 +5,17 @@
"dependencies": { "dependencies": {
"bootstrap": "^4.3.1", "bootstrap": "^4.3.1",
"concurrently": "^4.1.1", "concurrently": "^4.1.1",
"nodemon": "^1.19.1",
"react": "^16.8.6", "react": "^16.8.6",
"react-bootstrap": "^1.0.0-beta.9",
"react-dom": "^16.8.6", "react-dom": "^16.8.6",
"react-scripts": "3.0.1", "react-scripts": "3.0.1",
"socket.io-client": "^2.2.0" "react-scroll-to-bottom": "^1.3.2",
"socket.io-client": "^2.2.0",
"uuid": "^3.3.2"
}, },
"scripts": { "scripts": {
"start": "concurrently --kill-others-on-fail \"react-scripts start\" \"node server\" ", "start": "concurrently --kill-others-on-fail \"react-scripts start\" \"nodemon server\" ",
"build": "react-scripts build", "build": "react-scripts build",
"test": "react-scripts test", "test": "react-scripts test",
"eject": "react-scripts eject" "eject": "react-scripts eject"

View File

@ -1,10 +1,13 @@
module.exports = io => { module.exports = io => {
console.log('socket initialized', io)
io.on('connection', socket => { io.on('connection', socket => {
console.log(`A socket connection to the server has been made: ${socket.id}`) console.log(`A socket connection to the server has been made: ${socket.id}`)
socket.on('disconnect', () => { socket.on('disconnect', () => {
console.log(`Connection ${socket.id} has left the building`) console.log(`${socket.id} has disconnected.`)
})
socket.on('message', message => {
socket.broadcast.emit('message', message)
}) })
}) })
} }

View File

@ -1,6 +1,7 @@
import React from 'react' import React from 'react'
import { Chat } from './components'
const initialState = { loading: true } import { Container } from 'react-bootstrap'
const initialState = { loading: true, messages: [] }
const initialData = { message: 'Hello, world!' } const initialData = { message: 'Hello, world!' }
class App extends React.Component { class App extends React.Component {
@ -29,8 +30,11 @@ class App extends React.Component {
return <div>something went wrong.</div> return <div>something went wrong.</div>
} }
renderMain() {
return <Chat {...this.state} />
}
renderApp() { renderApp() {
return <div>{this.state.message}</div> return <Container>{this.renderMain()}</Container>
} }
render() { render() {

69
src/components/Chat.js Normal file
View File

@ -0,0 +1,69 @@
import React from 'react'
import { Form, Row, Col, Button } from 'react-bootstrap'
import uuid from 'uuid'
import socket from '../socket'
class Chat extends React.Component {
constructor() {
super()
this.state = { text: '', messages: [] }
this.handleSubmit = this.handleSubmit.bind(this)
this.handleChange = this.handleChange.bind(this)
}
componentDidMount() {
socket.on('message', message => {
this.addMessage(message)
})
}
handleChange(e) {
this.setState({ [e.target.name]: e.target.value })
}
addMessage(message) {
const messages = this.state.messages.concat(message)
this.setState({ messages })
}
handleSubmit(e) {
e.preventDefault()
const message = { userId: socket.id, id: uuid(), text: this.state.text }
const messages = this.state.messages.concat(message)
this.setState({ messages, text: '' })
socket.emit('message', message)
}
render() {
return (
<div className="mt-5">
<div style={{ height: '30rem' }}>
{this.state.messages.map(msg => (
<div>
{msg.userId}: {msg.text}
</div>
))}
</div>
<Form onSubmit={this.handleSubmit}>
<Form.Group as={Row}>
<Col xs={9} sm={10}>
<Form.Control
name="text"
autocomplete="off"
value={this.state.text}
onChange={this.handleChange}
/>
</Col>
<Col xs={1} sm={2}>
<Button type="submit" variant="dark" onSubmit={this.handleSubmit}>
Send
</Button>
</Col>
</Form.Group>
</Form>
</div>
)
}
}
export default Chat

1
src/components/index.js Normal file
View File

@ -0,0 +1 @@
export { default as Chat } from './Chat'