Index.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { h, render, Component } from 'preact'; // eslint-disable-line no-unused-vars
  2. // content and config:
  3. import content from '../content/module.json';
  4. import data from '../content/data.json';
  5. import config from '../config';
  6. // services and helper:
  7. import { fixedDigits } from '../utilities/formatter';
  8. // screens and items:
  9. import TitleScreen from './TitleScreen.jsx';
  10. import PollScreen from './PollScreen.jsx';
  11. /**
  12. * titlescreen -> pollscreen (*n)
  13. */
  14. export default class App extends Component {
  15. // construct and initialize functions
  16. constructor (props) {
  17. super(props);
  18. this.state = {
  19. route: 'titlescreen',
  20. iteration: 0,
  21. currentAnswerIndex: null
  22. };
  23. if (window.location.hash) this.state.route = window.location.hash.replace('#', '');
  24. // context binding
  25. this.navigate = this.navigate.bind(this);
  26. this.reset = this.reset.bind(this);
  27. this.toNext = this.toNext.bind(this);
  28. this.setAnswer = this.setAnswer.bind(this);
  29. this.jumpTo = this.jumpTo.bind(this);
  30. }
  31. // set navigation state
  32. navigate (route) {
  33. this.setState({ route });
  34. }
  35. // reset everything to restart the module
  36. reset () {
  37. this.setState({
  38. route: 'titlescreen',
  39. currentAnswerIndex: null
  40. });
  41. }
  42. // shortcut: jump to specific screen using `#question_2`
  43. jumpTo () {
  44. if (window.location.hash) {
  45. const hashInfo = window.location.hash.replace('#', '').split('_');
  46. if (hashInfo[1] === '1') {
  47. this.setState({ iteration: 8, continue: false, question: 0, route: 'pollscreen' });
  48. this.toNext();
  49. } else if (hashInfo[1] === '2') {
  50. this.setState({ iteration: 9, continue: false, question: 1, route: 'pollscreen' });
  51. this.toNext();
  52. }
  53. }
  54. }
  55. // to next question, if finished trigger restart
  56. toNext () {
  57. if (this.state.iteration < config.numberQuestions - 1) {
  58. this.setState({
  59. iteration: this.state.iteration + 1,
  60. currentAnswerIndex: null
  61. });
  62. } else {
  63. location.reload();
  64. }
  65. }
  66. // set and check answer
  67. setAnswer (checked) {
  68. if (this.state.currentAnswerIndex === null) {
  69. this.setState({ currentAnswerIndex: checked });
  70. }
  71. }
  72. // LIFECYLCE
  73. componentWillMount () {
  74. this.jumpTo();
  75. }
  76. // RENDER
  77. render () {
  78. let outputContent;
  79. // get header content
  80. const total = fixedDigits(config.numberQuestions, 2);
  81. const index = this.state.iteration + 1; // 0-indexed
  82. const headerState = `${fixedDigits(index, 2)}/${total}`;
  83. // get header and intro content
  84. const header = { title: content.title, number: content.number, headerState };
  85. const introContent = { number: content.number, title: content.title, introtext: content.introtext, start: content.start };
  86. switch (this.state.route) {
  87. case 'pollscreen':
  88. outputContent = <PollScreen
  89. {...header }
  90. {...content.sections[this.state.iteration]}
  91. data={data}
  92. toNext={this.toNext}
  93. iteration={this.state.iteration}
  94. currentAnswerIndex={this.state.currentAnswerIndex}
  95. setAnswer={this.setAnswer} />;
  96. break;
  97. case 'titlescreen':
  98. default:
  99. outputContent = <TitleScreen
  100. {...introContent}
  101. navigateTo='pollscreen'
  102. navigate={this.navigate} />;
  103. break;
  104. }
  105. return outputContent;
  106. }
  107. }