GameHeader.js 2.7 KB

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