ScoreScreen.jsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { h, render, Component } from 'preact'; // eslint-disable-line no-unused-vars
  2. import DonutGraphItem from './partials/DonutGraphItem.jsx';
  3. // render the current score including animated donut graph
  4. export default class ScoreScreen extends Component {
  5. // construct properties
  6. constructor (props) {
  7. super(props);
  8. this.state = {
  9. myScore: this.getMyScore(),
  10. otherUsersScore: this.getOtherUsersScore()
  11. };
  12. }
  13. // get score from current user
  14. getMyScore () {
  15. let numberCorrect = 0;
  16. [ ...this.props.answers ].forEach(item => {
  17. numberCorrect += item.isCorrect ? 1 : 0;
  18. });
  19. return numberCorrect / this.props.answers.length;
  20. }
  21. // get score from other users
  22. getOtherUsersScore () {
  23. let sum = 0;
  24. [ ...this.props.voteStats ].forEach(stat => {
  25. sum += stat;
  26. });
  27. return sum / this.props.voteStats.length;
  28. }
  29. // LIFECYLCE methods
  30. componentDidMount () {
  31. if (this.props.answers.length >= this.props.numberQuestions) this.props.endUserSession();
  32. }
  33. // ... and render it
  34. render () {
  35. const isLast = this.props.answers.length >= this.props.numberQuestions;
  36. return (
  37. <section className="wrapper wrapper__score">
  38. <main className="wrapper__main wrapper--centered">
  39. <div className="score-item">
  40. <span className="text-min-mod1">
  41. { isLast ? this.props.endscorelabel : this.props.interimscorelabel }
  42. </span>
  43. <span className="number--huge number--huge--spaced">{Math.round(this.state.myScore * 100)}%</span> korrekt.
  44. <DonutGraphItem endval={this.state.myScore} />
  45. </div>
  46. { !this.props.isOffline
  47. ? <div className="score-item">
  48. <span className="text-min-mod1">{this.props.othersscorelabel}</span>
  49. <span className="number--huge number--huge--spaced">{Math.round(this.state.otherUsersScore * 100)}%</span> korrekt.
  50. <DonutGraphItem endval={this.state.otherUsersScore} />
  51. </div> : [] }
  52. </main>
  53. <footer className="footer footer--question">
  54. { !isLast
  55. ? <a href="#" title="Start" className="button--wide" onClick={ this.props.newQuestion }>{this.props.next}</a>
  56. : <a href="#" title="Start" className="button--wide" onClick={ this.props.reset }>Zum Start</a> }
  57. </footer>
  58. </section>
  59. );
  60. }
  61. }