React app
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

112 行
3.6 KiB

  1. import React, { useEffect, useState } from "react";
  2. import styles from './Revise.module.scss';
  3. import { Question } from "./question/Question";
  4. import { Summary } from "./summary/Summary";
  5. import { recollectionQuestions, updateRecollectionQuestion } from "../../services/recollectionquestions";
  6. import { MobileShelf } from "../../shared/models/shelf";
  7. import { MobileUser } from "../../shared/models/user";
  8. export const Revise: React.FC = () => {
  9. const [progressState, setProgressState] = useState<'INTRO' | 'QUESTION' | 'END'>('QUESTION');
  10. const [questions, setQuestions] = useState<Array<any>>([]);
  11. const [answeredQuestions, setAnsweredQuestion] = useState<Array<{
  12. wordId: string,
  13. hasRecalled: boolean
  14. }>>([]);
  15. const [currentQuestion, setCurrentQuestion] = useState<number>(0);
  16. useEffect(() => {
  17. recollectionQuestions().then((response: any) => {
  18. let questions: Array<any> = response.data;
  19. const curatedQuestions: Array<any> = [];
  20. let allShelves: Array<MobileShelf> = [];
  21. const userProfile: MobileUser = JSON.parse(localStorage.getItem('userProfile') || "");
  22. userProfile.categories.forEach(category => {
  23. category.shelves?.forEach((shelf) => {
  24. allShelves.push(shelf);
  25. });
  26. });
  27. questions.forEach((question) => {
  28. let index = allShelves.findIndex(shelf => shelf._id === question.shelfId);
  29. if (index !== undefined) {
  30. const word = allShelves[index].words?.find(word => word.word._id === question.word)?.word;
  31. if (word !== undefined) {
  32. curatedQuestions.push({
  33. ...question,
  34. word: word
  35. })
  36. }
  37. }
  38. });
  39. setQuestions(curatedQuestions);
  40. if (curatedQuestions.length === 0) {
  41. setProgressState('END');
  42. }
  43. }, (e) => {
  44. window.alert("Failed to get questions");
  45. console.log(e);
  46. });
  47. }, []);
  48. return <section className={styles.revisePage}>
  49. { progressState === 'QUESTION' && <div>
  50. { questions.map((question, index) => {
  51. return currentQuestion === index && <Question key={index} totalQuestions={questions.length} questionNumber={currentQuestion}
  52. word={question.word}
  53. selectOption={(flag) => {
  54. let temp = answeredQuestions;
  55. temp.push({
  56. wordId: question.word._id,
  57. hasRecalled: flag,
  58. });
  59. setAnsweredQuestion(temp);
  60. console.log(question)
  61. setTimeout(() => {
  62. if (currentQuestion < questions.length - 1) {
  63. setCurrentQuestion(currentQuestion + 1);
  64. } else {
  65. setProgressState('END');
  66. }
  67. updateRecollectionQuestion(question.shelfId, question.word._id, flag);
  68. }, 200);
  69. }} />
  70. }) }
  71. <button className={styles.finishButton} onClick={() => setProgressState('END')}>
  72. Done
  73. </button>
  74. </div> }
  75. { progressState === 'END' && <div>
  76. <Summary />
  77. <button className={styles.finishButton} onClick={() => window.history.back()}>
  78. Done
  79. </button>
  80. </div> }
  81. </section>
  82. }