Implemented delete for admins

This commit is contained in:
Chris Blanchard 2015-09-14 22:03:39 +01:00
parent add58ff761
commit 58417c9a90
2 changed files with 33 additions and 2 deletions

View file

@ -20,7 +20,7 @@ var Message = mongoose.model("Message");
module.exports = namespace => { module.exports = namespace => {
var broadcastUpdate = message => { var broadcastUpdate = message => {
namespace.emit("message:new", message.toJson()); namespace.emit("message:new", message);
}; };
var refreshMessages = socket => { var refreshMessages = socket => {

View file

@ -27,6 +27,12 @@ var Chatroom = React.createClass({
self.scrollToBottom(); self.scrollToBottom();
}); });
socket.on("users:update", data => {
self.setProps({
currentUser: data.currentUser
});
});
socket.emit("message:refresh", {}); socket.emit("message:refresh", {});
}, },
@ -41,7 +47,9 @@ var Chatroom = React.createClass({
render() { render() {
let messages = this.props.history.map(message => let messages = this.props.history.map(message =>
<ChatMessage message={message} key={message.id} /> <ChatMessage message={message}
key={message.id}
currentUser={this.props.currentUser} />
); );
return ( return (
<div className="panel panel-default chatbox"> <div className="panel panel-default chatbox">
@ -74,6 +82,11 @@ var ChatMessage = React.createClass({
}, },
render() { render() {
let deleteButton;
let currentUser = this.props.currentUser;
if (this.props.currentUser && this.props.currentUser.admin) {
deleteButton = <DeleteMessageButton messageId={this.props.message._id} />;
}
return ( return (
<li className="left clearfix"> <li className="left clearfix">
<span className="chat-img pull-left"> <span className="chat-img pull-left">
@ -88,6 +101,7 @@ var ChatMessage = React.createClass({
<div className="header"> <div className="header">
<strong className="primary-font">{this.props.message.author.username}</strong> <strong className="primary-font">{this.props.message.author.username}</strong>
<small className="pull-right text-muted"> <small className="pull-right text-muted">
{deleteButton}
<i className="fa fa-clock-o fa-fw"></i> {$.timeago(this.props.message.createdAt)} <i className="fa fa-clock-o fa-fw"></i> {$.timeago(this.props.message.createdAt)}
</small> </small>
</div> </div>
@ -98,6 +112,23 @@ var ChatMessage = React.createClass({
} }
}); });
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>
);
}
})
var MessageBar = React.createClass({ var MessageBar = React.createClass({
sendMessage(content) { sendMessage(content) {
socket.emit("message:new", { socket.emit("message:new", {