|
@@ -1,6 +1,6 @@
|
|
|
-import React, { useState, useEffect } from "react";
|
|
|
+import React, { useState, useEffect, useMemo } from "react";
|
|
|
import styles from "./index.module.scss";
|
|
|
-import randomQuestions from "../../assets/data/questions";
|
|
|
+import { shuffle, generateThreeOptions, questions } from "../../assets/data/questions";
|
|
|
import { getSessionStorage, setSessionStorage, clearSessionStorage } from "../../utils/storage";
|
|
|
import { useNavigate } from "react-router-dom";
|
|
|
import dayjs from "dayjs";
|
|
@@ -14,6 +14,7 @@ interface QuestionItem {
|
|
|
}
|
|
|
|
|
|
interface QuestionProps {
|
|
|
+ randomQuestions: QuestionItem[];
|
|
|
item: QuestionItem;
|
|
|
setItem: (item: QuestionItem) => void;
|
|
|
currentIndex: number;
|
|
@@ -22,7 +23,7 @@ interface QuestionProps {
|
|
|
setRightCount: (rightCount: number) => void;
|
|
|
}
|
|
|
|
|
|
-const Question = ({ item, setItem, currentIndex, setCurrentIndex, setIsFinish, setRightCount }: QuestionProps) => {
|
|
|
+const Question = ({ randomQuestions, item, setItem, currentIndex, setCurrentIndex, setIsFinish, setRightCount }: QuestionProps) => {
|
|
|
const sentence = item.title.split("——")[0];
|
|
|
const from = item.title.split("——")[1];
|
|
|
const [questionStatus, setQuestionStatus] = useState(["or", "or", "or", "or", "or", "or", "or", "or", "or", "or"]);
|
|
@@ -96,6 +97,15 @@ const Question = ({ item, setItem, currentIndex, setCurrentIndex, setIsFinish, s
|
|
|
};
|
|
|
|
|
|
const QuestionList = () => {
|
|
|
+ const randomQuestions: QuestionItem[] = useMemo(() => {
|
|
|
+ return (shuffle(questions.map((questionArray) => {
|
|
|
+ const question = shuffle(questionArray)[0];
|
|
|
+ return {
|
|
|
+ ...question,
|
|
|
+ options: generateThreeOptions(question.options),
|
|
|
+ };
|
|
|
+ })) as QuestionItem[])
|
|
|
+ }, [])
|
|
|
const [item, setItem] = useState<QuestionItem>(randomQuestions[0]);
|
|
|
const [index, setIndex] = useState(0);
|
|
|
const [timeLeft, setTimeLeft] = useState(180);
|
|
@@ -130,6 +140,7 @@ const QuestionList = () => {
|
|
|
) : (
|
|
|
<div className={styles.question}>
|
|
|
<Question
|
|
|
+ randomQuestions={randomQuestions}
|
|
|
item={item}
|
|
|
setItem={setItem}
|
|
|
currentIndex={index}
|