GameHeader.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. const React = require('react/addons');
  3. const Clock = require('./Clock');
  4. const ChatStore = require('../stores/ChatStore');
  5. const ChatActions = require('../actions/ChatActions');
  6. const GameHeader = React.createClass({
  7. propTypes: {
  8. io: React.PropTypes.object.isRequired,
  9. params: React.PropTypes.array.isRequired,
  10. color: React.PropTypes.string,
  11. openModal: React.PropTypes.func.isRequired,
  12. gameOver: React.PropTypes.bool.isRequired
  13. },
  14. mixins: [React.addons.PureRenderMixin],
  15. getInitialState() {
  16. return {
  17. isChatHidden: ChatStore.getState().isChatHidden,
  18. newMessage: false
  19. };
  20. },
  21. componentDidMount() {
  22. const io = this.props.io;
  23. io.on('receive-message', () => {
  24. if (this.state.isChatHidden) {
  25. this.setState({newMessage: true});
  26. }
  27. });
  28. ChatStore.on('change', this._onChatStoreChange);
  29. },
  30. componentWillUnmount() {
  31. ChatStore.off('change', this._onChatStoreChange);
  32. },
  33. render() {
  34. const [_, time, inc] = this.props.params;
  35. return (
  36. <header className="clearfix">
  37. <Clock
  38. io={this.props.io}
  39. params={this.props.params} />
  40. <span id="game-type">
  41. {`${time}|${inc}`}
  42. </span>
  43. <a className="btn" href="/">New game</a>
  44. {!this.props.gameOver ?
  45. <a className="btn btn--red resign"
  46. onClick={this._onResign}>
  47. Resign
  48. </a> :
  49. <a className="btn btn--red rematch"
  50. onClick={this._onRematch}>
  51. Rematch
  52. </a>
  53. }
  54. <a id="chat-icon"
  55. onClick={this._toggleChat}>
  56. {this.state.newMessage ?
  57. <span className="new-message">You have a new message!</span>
  58. :null}
  59. <img src="/img/chat.svg"
  60. width="50"
  61. height="50" />
  62. Chat
  63. </a>
  64. </header>
  65. );
  66. },
  67. _onChatStoreChange() {
  68. this.setState({
  69. isChatHidden: ChatStore.getState().isChatHidden
  70. });
  71. },
  72. _toggleChat() {
  73. this.setState({newMessage: false});
  74. ChatActions.toggleChat();
  75. },
  76. _onResign() {
  77. const {io, params, color} = this.props;
  78. io.emit('resign', {
  79. token: params[0],
  80. color: color
  81. });
  82. },
  83. _onRematch() {
  84. const {io, params, openModal} = this.props;
  85. io.emit('rematch-offer', {
  86. token: params[0]
  87. });
  88. openModal('info', 'Your offer has been sent.');
  89. }
  90. });
  91. module.exports = GameHeader;