'use strict'; const React = require('react/addons'); const Clock = require('./Clock'); const ChatStore = require('../stores/ChatStore'); const ChatActions = require('../actions/ChatActions'); const GameHeader = React.createClass({ propTypes: { io: React.PropTypes.object, params: React.PropTypes.array.isRequired, color: React.PropTypes.string, openModal: React.PropTypes.func.isRequired }, mixins: [React.addons.PureRenderMixin], getInitialState() { return { isChatHidden: ChatStore.getState().isChatHidden, newMessage: false }; }, componentDidMount() { let io = this.props.io; io.on('receive-message', () => { if (this.state.isChatHidden) { this.setState({newMessage: true}); } }); ChatStore.on('change', this._onChatStoreChange); }, componentWillUnmount() { ChatStore.off('change', this._onChatStoreChange); }, render() { let [_, time, inc] = this.props.params; return (
{`${time}|${inc}`} New game {this.state.newMessage ? You have a new message! :null} Chat
); }, _onChatStoreChange() { this.setState({ isChatHidden: ChatStore.getState().isChatHidden }); }, _toggleChat(e) { e.preventDefault(); this.setState({newMessage: false}); ChatActions.toggleChat(); }, _onResign(e) { e.preventDefault(); let {io, params, color} = this.props; io.emit('resign', { token: params[0], color: color }); }, _onRematch(e) { e.preventDefault(); let {io, params, openModal} = this.props; io.emit('rematch-offer', { token: params[0] }); openModal('info', 'Your offer has been sent.'); } }); module.exports = GameHeader;