GameHeader.js 2.5 KB

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