ESPTrainer built with Meteor and React

Board.jsx 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React, { Component, PropTypes } from 'react';
  2. import update from 'react-addons-update';
  3. import ReactTimeout from 'react-timeout';
  4. import Square from './Square';
  5. import Trials from './Trials';
  6. import Actions from './Actions';
  7. import './styles/Board';
  8. class Board extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. win: false,
  13. trials: 0,
  14. squares: [false, false, false, false],
  15. image: ""
  16. };
  17. this.actions = [
  18. { label: 'Reset', action: this.reset.bind(this) },
  19. { label: 'Pass', action: this.pass.bind(this) }
  20. ];
  21. }
  22. fail(corrected) {
  23. this.setState({ squares: update(this.state.squares, { [corrected]: { $set: true } }) });
  24. this.props.setTimeout(() => this.setState({
  25. squares: update(this.state.squares, { [corrected]: { $set: false } })
  26. }), 1000);
  27. }
  28. win() {
  29. this.setState({ win: true });
  30. this.props.setTimeout(() => this.setState({ win: false }), 1000);
  31. }
  32. choice(answer) {
  33. let corrected = Math.floor(Math.random() * 10) % 4;
  34. this.setState({ trials: this.state.trials + 1 });
  35. if (answer === corrected)
  36. this.win();
  37. else
  38. this.fail(corrected);
  39. }
  40. reset() {}
  41. pass() {}
  42. render() {
  43. return (
  44. <div className="board">
  45. <div className="squares">
  46. <div className="row">
  47. <Square color="green" click={this.choice.bind(this, 0)} highlighted={this.state.squares[0]} />
  48. <Square color="yellow" click={this.choice.bind(this, 1)} highlighted={this.state.squares[1]} />
  49. </div>
  50. <div className="row">
  51. <Square color="red" click={this.choice.bind(this, 2)} highlighted={this.state.squares[2]} />
  52. <Square color="blue" click={this.choice.bind(this, 3)} highlighted={this.state.squares[3]} />
  53. </div>
  54. </div>
  55. { this.state.win ? <Image src={this.state.image}/> : null }
  56. <Trials count={this.state.trials} />
  57. <Actions actions={this.actions}/>
  58. </div>
  59. );
  60. }
  61. }
  62. export default ReactTimeout(Board)