"use strict"; var Chatroom = React.createClass({ getDefaultProps() { return { history: [] }; }, componentDidMount() { let self = this; socket.on("message:append", data => { let history = self.props.history; history.push(data); self.setProps({ history: history.sort((a, b) => { return new Date(a.createdAt) - new Date(b.createdAt); }) }); self.scrollToBottom(); }); // Message History Retrieved socket.on("message:refresh", data => { self.setProps({ history: data.chatHistory }); self.scrollToBottom(); }); socket.on("users:update", data => { self.setProps({ currentUser: data.currentUser }); }); socket.emit("message:refresh", {}); }, sendMessage(message) { socket.emit("newMessage", {message: message}); }, scrollToBottom() { let node = React.findDOMNode(this.refs.messageContainer); node.scrollTop = node.scrollHeight; }, render() { let messages = this.props.history.map(message => ); return (
Gather Chat
    {messages}
); } }); var ChatMessage = React.createClass({ mixins: [ ReactAutolink ], getInitialState() { return { createdAt: "" } }, updateCreatedAt() { let self = this; if (this.props.message.createdAt) { self.setState({ createdAt: $.timeago(self.props.message.createdAt) }) } }, componentDidMount() { this.updateCreatedAt(); this.interval = setInterval(this.updateCreatedAt.bind(this), 60000); }, componentWillUnmount: function () { clearInterval(this.interval); }, messageContent: function () { return this.autolink(this.props.message.content, { target: "_blank", rel: "nofollow" }); }, render() { let deleteButton; let currentUser = this.props.currentUser; if (currentUser && currentUser.admin) { deleteButton = ; } return (
  • User Avatar
    {this.props.message.author.username} {deleteButton} {this.state.createdAt}

    {this.messageContent()}

  • ); } }); var DeleteMessageButton = React.createClass({ handleClick (e) { e.preventDefault(); socket.emit("message:delete", { id: this.props.messageId }); }, render() { return ( ); } }) var MessageBar = React.createClass({ sendMessage(content) { socket.emit("message:new", { content: content }); }, handleSubmit(e) { e.preventDefault(); let content = React.findDOMNode(this.refs.content).value.trim(); if (!content) return; React.findDOMNode(this.refs.content).value = ''; this.sendMessage(content); return; }, render() { return (
    ); } });