ensl_gathers/lib/react/message.jsx

147 lines
3.2 KiB
React
Raw Normal View History

2015-07-28 15:54:29 +00:00
"use strict";
var Chatroom = React.createClass({
getDefaultProps: function () {
return {
history: []
};
},
componentDidMount: function () {
var self = this;
2015-07-31 11:21:07 +00:00
var TIMER_INTERVAL = 5000; // Every minute
2015-07-28 15:54:29 +00:00
socket.on("message:new", function (data) {
var history = self.props.history;
history.push(data);
self.setProps({
history: history
});
self.scrollToBottom();
});
// Message History Retrieved
socket.on("message:refresh", function (data) {
self.setProps({
history: data.chatHistory
});
self.scrollToBottom();
});
socket.emit("message:refresh", {});
self.timer = setInterval(function () {
2015-07-31 11:21:07 +00:00
self.forceUpdate();
2015-07-28 15:54:29 +00:00
}, TIMER_INTERVAL);
},
componentDidUnmount: function () {
clearInterval(this.timer);
},
sendMessage: function (message) {
socket.emit("newMessage", {message: message});
},
scrollToBottom: function () {
var node = React.findDOMNode(this.refs.messageContainer);
node.scrollTop = node.scrollHeight;
},
render: function () {
var messages = this.props.history.map(function (message) {
return (
<ChatMessage
2015-07-31 11:21:07 +00:00
message={message}
key={message.id} />
2015-07-28 15:54:29 +00:00
);
});
return (
<div className="panel panel-default">
<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-07-31 11:21:07 +00:00
var updateMessageCallbacks = [];
var timer = setInterval(function () {
updateMessageCallbacks.forEach(function (callback) {
callback();
});
}, 60000);
2015-07-28 15:54:29 +00:00
var ChatMessage = React.createClass({
2015-07-31 11:21:07 +00:00
componentDidMount: function () {
2015-07-28 15:54:29 +00:00
var self = this;
2015-07-31 11:21:07 +00:00
updateMessageCallbacks.push(function () {
self.forceUpdate();
2015-07-28 15:54:29 +00:00
});
},
render: function () {
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-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>
);
}
});
var MessageBar = React.createClass({
sendMessage: function (content) {
socket.emit("message:new", {
content: content
});
},
handleSubmit: function (e) {
e.preventDefault();
var content = React.findDOMNode(this.refs.content).value.trim();
if (!content) return;
React.findDOMNode(this.refs.content).value = '';
this.sendMessage(content);
return;
},
render: function () {
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>
);
}
});