QuestionScreen.jsx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { h, render, Component } from 'preact'; // eslint-disable-line no-unused-vars
  2. import GraphItem from './partials/GraphItem.jsx';
  3. import IntroItem from './partials/IntroItem.jsx';
  4. import QuestionItem from './partials/QuestionItem.jsx';
  5. import UserVoteItem from './partials/UserVoteItem.jsx';
  6. import HeaderLightItem from './partials/HeaderLightItem.jsx';
  7. // module02: Question Screen
  8. // outputs the wrapper which contains
  9. // - a) the current question
  10. // - b) the current user vote result
  11. // - c) the initial intro item
  12. export default class QuestionScreen extends Component {
  13. // get answer and validate it
  14. getAnswers (question) {
  15. return Object.prototype.hasOwnProperty.call(question, 'antworten') ? question.antworten : [];
  16. }
  17. // RENDER
  18. render () {
  19. let topItem = ''; // content (will either be questions or statistics)
  20. let isCorrectClass = '';
  21. let isIncorrectClass = '';
  22. let buttonLabel = this.props.next;
  23. const question = this.props.questions[this.props.currentQuestion];
  24. const answers = this.getAnswers(question);
  25. // set classes to style lines in svg graph
  26. const hasAnswered = this.props.currentAnswerIndex !== null;
  27. const correct = answers.find(item => item.korrekt === true);
  28. if (this.props.currentGroup === 1 && hasAnswered) {
  29. isCorrectClass = `correct--${answers.indexOf(correct) + 1}`;
  30. isIncorrectClass = !this.props.currentAnswerCorrect
  31. ? `incorrect--${this.props.currentAnswerIndex + 1}` : '';
  32. } else if (this.props.currentGroup === 2) {
  33. isCorrectClass = this.props.currentAnswerCorrect ? 'correct' : 'incorrect';
  34. buttonLabel = `${this.props.success} ${this.props.next}`;
  35. }
  36. // d3js properties to pass through
  37. const graphItemProps = {
  38. dataSet: question,
  39. setData: this.props.setData,
  40. chartMode: this.props.chartMode,
  41. currentQuestion: this.props.currentQuestion,
  42. points: this.props.points
  43. };
  44. // render sub component depending on chartMode
  45. if (this.props.chartMode === 'question') {
  46. topItem = <QuestionItem question={question} answers={answers} currentGroup={this.props.currentGroup}
  47. currentQuestion={this.props.currentQuestion} currentAnswerIndex={this.props.currentAnswerIndex} setAnswer={this.props.setAnswer} />;
  48. } else if (this.props.voteRatios !== null) {
  49. topItem = <UserVoteItem usersRight={this.props.usersRight} voteRatios={this.props.voteRatios} />;
  50. } else {
  51. topItem = <IntroItem headline={this.props.usersVoteHeadline} />;
  52. }
  53. // output
  54. return (
  55. <section className="wrapper wrapper__question">
  56. <HeaderLightItem { ...this.props } />
  57. <main className={`wrapper__main wrapper--centered wrapper__main--question ${isCorrectClass} ${isIncorrectClass}`}>
  58. {topItem}
  59. <GraphItem { ...graphItemProps } />
  60. </main>
  61. <footer className="footer footer--chart">
  62. {this.props.currentAnswerIndex !== null
  63. ? <a href="#" title={this.props.next} className="button--wide" onClick={() => this.props.toNextQuestion('score')}>
  64. {buttonLabel}
  65. </a>
  66. : []}
  67. </footer>
  68. </section>
  69. );
  70. }
  71. }