1
0
forked from notnull/chatz

added SingleMessage, fixed user.socketId

This commit is contained in:
notnull 2019-07-14 11:53:38 -04:00
parent e4aae7e158
commit 974dcc2c1b
2 changed files with 51 additions and 19 deletions

View File

@ -1,6 +1,6 @@
import React from 'react' import React from 'react'
import { Form, Row, Col, Button, Card, ListGroup, Image } from 'react-bootstrap' import { Form, Row, Col, Button, Card, ListGroup, Image } from 'react-bootstrap'
//import ScrollToBottom from 'react-scroll-to-bottom' import SingleMessage from './SingleMessage'
import userImage from '../assets/default-user-image.png' import userImage from '../assets/default-user-image.png'
import uuid from 'uuid' import uuid from 'uuid'
@ -33,7 +33,7 @@ class Chat extends React.Component {
socket.on('connect', () => { socket.on('connect', () => {
console.log('Connected!') console.log('Connected!')
this.setState({ user: { socketId: socket.id } }) this.setState({ user: { socketId: socket.id, userImage } })
}) })
} }
@ -63,29 +63,30 @@ class Chat extends React.Component {
} }
handleSubmit(e) { handleSubmit(e) {
e.preventDefault() e.preventDefault()
const message = { userId: socket.id, id: uuid(), text: this.state.text } const message = { socketId: socket.id, id: uuid(), text: this.state.text }
const messages = this.state.messages.concat(message) const messages = this.state.messages.concat(message)
this.setState({ messages, text: '' }) this.setState({ messages, text: '' })
socket.emit('message', message) socket.emit('message', message)
} }
getStatus() {
console.log('this is the status')
}
render() { render() {
return ( return (
<Card className="mt-5"> <Card className="mt-5">
<Card.Body style={{ height: '60vh' }}> <Card.Body style={{ height: '60vh' }}>
<ListGroup style={{ height: '100%', overflowY: 'hidden' }}> <ListGroup style={{ height: '100%', overflowY: 'hidden' }}>
{this.state.messages.map(msg => ( {this.state.messages.map(msg => {
<ListGroup.Item key={uuid()} className="my-2"> return (
<Image <SingleMessage
className={`mx-2 ${ key={uuid()}
socket.id === this.state.user.socketId ? 'float-right' : '' user={this.state.user}
}`} socketId={msg.socketId}
style={{ height: '24px' }} text={msg.text}
src={userImage}
/> />
{msg.userId}: {msg.text} )
</ListGroup.Item> })}
))}
<div <div
style={{ float: 'left', clear: 'both' }} style={{ float: 'left', clear: 'both' }}
ref={el => { ref={el => {
@ -117,11 +118,18 @@ class Chat extends React.Component {
</Col> </Col>
</Form.Group> </Form.Group>
</Form> </Form>
{this.state.typing ? ( </Card.Body>
<div style={{ height: '2em' }}>Someone is typing...</div> <Card.Body>
) : ( <Row>
<div style={{ height: '2em' }} /> <Col>{this.state.typing ? 'Someone is typing...' : ''}</Col>
)} <Col>
<Image
style={{ height: '24px' }}
src={this.state.user.userImage}
/>
{this.state.user.socketId}
</Col>
</Row>
</Card.Body> </Card.Body>
</Card> </Card>
) )

View File

@ -0,0 +1,24 @@
import React from 'react'
import { ListGroup, Image } from 'react-bootstrap'
const SingleMessage = props => {
const { user, socketId, text } = props
if (user.socketId === socketId)
return (
<ListGroup.Item className="my-2 bg-dark text-white">
<div className="float-right">{text}</div>
</ListGroup.Item>
)
return (
<ListGroup.Item className="my-2">
<Image
className={`mx-2`}
style={{ height: '24px' }}
src={user.userImage}
/>
{user.socketId}: {text}
</ListGroup.Item>
)
}
export default SingleMessage