Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. /*
  2.  
  3. Why isn't the ENTERING_FIRST_LETTER (initial) case executed in the reducer-function?
  4.  
  5. Why, after returning state in the ENTERED_FIRST_LETTER-case, isn't the dispatch-method called again automatically?
  6.  
  7. */
  8.  
  9. import React, { useEffect, useReducer } from 'react';
  10. import Question from './Question';
  11. import Instruction from './Instruction';
  12. import FocusableEnableableTextInput from './FocusableEnableableTextInput';
  13. /** @jsx jsx */import { css, jsx } from "@emotion/core";
  14. import Progress from './Progress';
  15.  
  16. const appStyle = css`
  17. text-align: center;
  18. `;
  19.  
  20. const appHeaderCss = css`
  21. font-family: 'Times new Roman';
  22. background-color: #282c34;
  23. min-height: 100vh;
  24. display: flex;
  25. flex-direction: column;
  26. align-items: center;
  27. justify-content: center;
  28. font-size: calc(36px + 2vmin);
  29. color: white;
  30. `;
  31.  
  32. const playfieldStyle = css`
  33. display: flex;
  34. flex-direction: row;
  35. justify-content: center;
  36. width: 80%;
  37. `;
  38.  
  39. const progressStyle = css`
  40.  
  41. `;
  42.  
  43. const questionLetter = 'B';
  44. const correctPreAnswer = 'A';
  45. const correctPostAnswer = 'C';
  46.  
  47. const initialState = {
  48. gameState: 'ENTERING_FIRST_LETTER',
  49. instructionText: `Geef de letter die voor ${questionLetter} komt.`,
  50. preLetter: '',
  51. postLetter: '',
  52. preLetterFocus: true,
  53. postLetterFocus: false,
  54. preLetterEnabled: true,
  55. postLetterEnabled: false
  56. }
  57.  
  58. const initialDispatch = () => {
  59. return initialState;
  60. }
  61.  
  62. function reducer(state, action) {
  63. console.log('in reducer, state: ')
  64. console.log(state);
  65. console.log('in reducer, action: ');
  66. console.log(action);
  67. switch (action.type) {
  68. case 'ENTERING_FIRST_LETTER':
  69. return {
  70. gameState: 'ENTERED_FIRST_LETTER',
  71. preLetter: '',
  72. postLetter: '',
  73. instructionText: `Geef de letter die voor ${questionLetter} komt.`,
  74. preLetterFocus: true,
  75. postLetterFocus: false,
  76. preLetterEnabled: true,
  77. postLetterEnabled: false,
  78. }
  79. case 'ENTERED_FIRST_LETTER':
  80. const preLetter = action.preLetter.toUpperCase();
  81. return {
  82. ...state,
  83. gameState: preLetter === correctPreAnswer ?
  84. 'ENTERING_SECOND_LETTER' : 'WRONG_AFTER_FIRST_LETTER',
  85. preLetter
  86. };
  87. case 'ENTERING_SECOND_LETTER':
  88. return {
  89. ...state,
  90. gameState: 'ENTERED_SECOND_LETTER',
  91. instructionText: `Juist! Geef nu de letter die na ${questionLetter} komt.`,
  92. preLetterFocus: false,
  93. postLetterFocus: true,
  94. preLetterEnabled: false,
  95. postLetterEnabled: false,
  96. };
  97. case 'ENTERED_SECOND_LETTER':
  98. const postLetter = action.postLetter.toUpperCase();
  99. return {
  100. ...state,
  101. gameState: postLetter === correctPostAnswer ?
  102. 'CORRECT' : 'WRONG_AFTER_SECOND_LETTER',
  103. postLetter,
  104. postLetterFocus: false,
  105. postLetterEnabled: false,
  106. }
  107. case 'WRONG_AFTER_FIRST_LETTER':
  108. return {
  109. ...state,
  110. gameState: 'WAITING_FOR_RESTART',
  111. instructionText: `Fout! ${correctPreAnswer} komt voor ${questionLetter}!`,
  112.  
  113. }
  114. case 'WRONG_AFTER_SECOND_LETTER':
  115. return {
  116. ...state,
  117. gameState: 'WAITING_FOR_RESTART',
  118. instructionText: `Fout! ${correctPostAnswer} komt na ${questionLetter}!`,
  119. }
  120. case 'CORRECT':
  121. return {
  122. ...state,
  123. gameState: 'WAITING_FOR_RESTART',
  124. instructionText: `${correctPreAnswer} ${questionLetter} ${correctPostAnswer} is inderdaad juist!`,
  125. }
  126. case 'WAITING_FOR_RESTART':
  127. setTimeout(() => {
  128. state.preLetter = '';
  129. state.postLetter = '';
  130. state.gameState = 'ENTERING_FIRST_LETTER';
  131. }, 2000);
  132. break;
  133. default:
  134. console.log("Unknown gameState in reducer! This shouldn't happen!");
  135. }
  136. console.log('before return reducer');
  137. console.log(state);
  138. return state;
  139. }
  140.  
  141. const App = () => {
  142. const [state, dispatch] = useReducer(reducer, initialState);
  143.  
  144. const preLetterHandler = l => dispatch({ type: 'ENTERED_FIRST_LETTER', preLetter: l });
  145. const postLetterHandler = l => dispatch( {type: 'ENTERED_SECOND_LETTER', postLetter: l })
  146.  
  147. return <div css={appStyle}>
  148. <header css={appHeaderCss}>
  149. <h1>Alfabet Trainer</h1>
  150. <div css={playfieldStyle}>
  151. <FocusableEnableableTextInput
  152. onChange={preLetterHandler}
  153. value={state.preLetter}
  154. enabled={state.preLetterEnabled}
  155. giveFocus={state.preLetterFocus}
  156. />
  157. <Question letter="B" />
  158. <FocusableEnableableTextInput
  159. onChange={postLetterHandler}
  160. value={state.postLetter}
  161. enabled={state.postLetterEnabled}
  162. giveFocus={state.postLetterFocus}
  163. />
  164. </div>
  165. <Instruction instructionText={state.instructionText} />
  166. </header>
  167. </div>
  168. }
  169.  
  170. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement