ensl_gathers/lib/react/message.jsx

171 lines
3.5 KiB
React
Raw Normal View History

2015-07-28 15:54:29 +00:00
"use strict";
var Chatroom = React.createClass({
2015-08-11 00:31:03 +00:00
getDefaultProps() {
2015-07-28 15:54:29 +00:00
return {
history: []
};
},
2015-08-11 00:31:03 +00:00
componentDidMount() {
let self = this;
socket.on("message:new", data => {
let history = self.props.history;
2015-07-28 15:54:29 +00:00
history.push(data);
self.setProps({
history: history
});
self.scrollToBottom();
});
// Message History Retrieved
2015-08-11 00:31:03 +00:00
socket.on("message:refresh", data => {
2015-07-28 15:54:29 +00:00
self.setProps({
history: data.chatHistory
});
self.scrollToBottom();
});
2015-09-14 21:03:39 +00:00
socket.on("users:update", data => {
self.setProps({
currentUser: data.currentUser
});
});
2015-07-28 15:54:29 +00:00
socket.emit("message:refresh", {});
},
2015-08-11 00:31:03 +00:00
sendMessage(message) {
2015-07-28 15:54:29 +00:00
socket.emit("newMessage", {message: message});
2015-08-11 00:37:43 +00:00
},
2015-08-11 00:31:03 +00:00
scrollToBottom() {
2015-08-11 00:37:43 +00:00
let node = React.findDOMNode(this.refs.messageContainer);
2015-07-28 15:54:29 +00:00
node.scrollTop = node.scrollHeight;
},
2015-08-11 00:31:03 +00:00
render() {
2015-08-11 00:37:43 +00:00
let messages = this.props.history.map(message =>
2015-09-14 21:03:39 +00:00
<ChatMessage message={message}
key={message.id}
currentUser={this.props.currentUser} />
2015-08-11 00:37:43 +00:00
);
2015-07-28 15:54:29 +00:00
return (
2015-08-27 14:14:00 +00:00
<div className="panel panel-default chatbox">
2015-07-28 15:54:29 +00:00
<div className="panel-heading">Gather Chat</div>
<div className="panel-body">
<ul className="chat" id="chatmessages" ref="messageContainer">
{messages}
</ul>
</div>
<div className="panel-footer">
<MessageBar />
</div>
</div>
);
}
});
2015-08-11 00:37:43 +00:00
let updateMessageCallbacks = [];
2015-07-31 11:21:07 +00:00
2015-08-11 00:37:43 +00:00
let timer = setInterval(() => {
updateMessageCallbacks.forEach(callback => callback())
2015-07-31 11:21:07 +00:00
}, 60000);
2015-07-28 15:54:29 +00:00
var ChatMessage = React.createClass({
2015-08-11 00:37:43 +00:00
componentDidMount() {
let self = this;
updateMessageCallbacks.push(() => {
2015-07-31 11:21:07 +00:00
self.forceUpdate();
2015-07-28 15:54:29 +00:00
});
},
2015-08-11 00:37:43 +00:00
render() {
2015-09-14 21:03:39 +00:00
let deleteButton;
let currentUser = this.props.currentUser;
if (this.props.currentUser && this.props.currentUser.admin) {
deleteButton = <DeleteMessageButton messageId={this.props.message._id} />;
}
2015-07-28 15:54:29 +00:00
return (
<li className="left clearfix">
<span className="chat-img pull-left">
<img
2015-07-31 11:21:07 +00:00
src={this.props.message.author.avatar}
2015-07-28 15:54:29 +00:00
alt="User Avatar"
height="40"
width="40"
className="img-circle" />
</span>
<div className="chat-body clearfix">
<div className="header">
2015-07-31 11:21:07 +00:00
<strong className="primary-font">{this.props.message.author.username}</strong>
2015-07-28 15:54:29 +00:00
<small className="pull-right text-muted">
2015-09-14 21:03:39 +00:00
{deleteButton}
2015-07-31 11:21:07 +00:00
<i className="fa fa-clock-o fa-fw"></i> {$.timeago(this.props.message.createdAt)}
2015-07-28 15:54:29 +00:00
</small>
</div>
2015-07-31 11:21:07 +00:00
<p>{this.props.message.content}</p>
2015-07-28 15:54:29 +00:00
</div>
</li>
);
}
});
2015-09-14 21:03:39 +00:00
var DeleteMessageButton = React.createClass({
handleClick (e) {
e.preventDefault();
socket.emit("message:delete", {
id: this.props.messageId
});
},
render() {
return (
<a href="#" onClick={this.handleClick}>
<i className="fa fa-trash-o"></i>
</a>
);
}
})
2015-07-28 15:54:29 +00:00
var MessageBar = React.createClass({
2015-08-11 00:37:43 +00:00
sendMessage(content) {
2015-07-28 15:54:29 +00:00
socket.emit("message:new", {
content: content
});
},
2015-08-11 00:37:43 +00:00
handleSubmit(e) {
2015-07-28 15:54:29 +00:00
e.preventDefault();
2015-08-11 00:37:43 +00:00
let content = React.findDOMNode(this.refs.content).value.trim();
2015-07-28 15:54:29 +00:00
if (!content) return;
React.findDOMNode(this.refs.content).value = '';
this.sendMessage(content);
return;
},
2015-08-11 00:37:43 +00:00
render() {
2015-07-28 15:54:29 +00:00
return (
<form onSubmit={this.handleSubmit} >
<div className="input-group">
<input
id="btn-input"
type="text"
className="form-control"
ref="content"
placeholder="Be polite please..." />
<span className="input-group-btn">
<input
type="submit"
className="btn btn-primary"
id="btn-chat"
value="Send" />
</span>
</div>
</form>
);
}
});
2015-08-11 00:37:43 +00:00