Created
November 2, 2018 17:32
-
-
Save aditodkar/1504d03db0f42185b35c406d1cb70f69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import './chat.css' | |
import io from "socket.io-client"; | |
import UserList from './userlist'; | |
import { connect } from 'react-redux'; | |
import { saveAuthor } from '../../store/actions/authorAction' | |
import { saveMessages } from '../../store/actions/messageAction' | |
class Chat extends Component { | |
constructor(props){ | |
super(props); | |
this.state = { | |
message: '', | |
date: '', | |
author: '', | |
messages: [] | |
}; | |
this.sendMessage = this.sendMessage.bind(this); | |
this.addMessage = this.addMessage.bind(this); | |
this.socket = io('localhost:5000'); | |
} | |
componentDidMount() { | |
this.socket.on('RECEIVE_MESSAGE', data => { | |
console.log(data); | |
this.addMessage(data); | |
}); | |
// this.socket.emit('USER_ID', { | |
// userId: this.props.match.params.user | |
// }); | |
} | |
sendMessage(event) { | |
event.preventDefault(); | |
if(this.state.message !== ''){ | |
this.socket.emit('SEND_MESSAGE', { | |
author: this.props.match.params.user, | |
message: this.state.message, | |
date: Date.now() | |
}); | |
// this.socket.emit('TYPING', { | |
// typing: this.props.match.params.user | |
// }) | |
} | |
}; | |
addMessage(data) { | |
//console.log(data); | |
this.setState({ | |
messages: [...this.state.messages, ...data], | |
message: '', | |
date: '' | |
}); | |
//console.log(this.state.message); | |
console.log(this.state.messages); | |
}; | |
render() { | |
return ( | |
<div> | |
<h2>Hello {this.props.match.params.user}</h2> | |
<div className="container"> | |
<div id="chat"> | |
<div className="card"> | |
<div id="messages" className="card-block"> | |
{this.state.messages.map((message, index) => { | |
if(message.author === this.props.match.params.user){ | |
return ( | |
<div key={index} className="msgBoxRight"><p className="msgTextRight">{message.message}</p></div> | |
) | |
}else{ | |
return ( | |
<div key={index} className="msgBoxLeft"><p className="msgTextLeft">{message.message}</p></div> | |
) | |
} | |
})} | |
</div> | |
<div id="feedback"></div> | |
<div className="row"> | |
<div className="column"> | |
<input id="inputmsg" type="text" placeholder="Enter Message...." | |
value={this.state.message} onChange={ev => this.setState({message: ev.target.value})}/> | |
</div> | |
<div className="column2"> | |
<button id="send" className="button" onClick={this.sendMessage}>Send</button> | |
</div> | |
<div className="upload"> | |
</div> | |
</div> | |
</div> | |
</div> | |
<div className="userlist"> | |
<UserList currentUser={this.props.match.params.user}/> | |
</div> | |
</div> | |
</div> | |
) | |
} | |
} | |
const mapStateToProps = state => ({ | |
author: state.author, | |
messages: state.messages | |
}) | |
export default connect (mapStateToProps, { saveAuthor, saveMessages })(Chat); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment