diff --git a/webapp/src/components/Question.jsx b/webapp/src/components/Question.jsx index eec1b31..55d93fa 100644 --- a/webapp/src/components/Question.jsx +++ b/webapp/src/components/Question.jsx @@ -4,37 +4,47 @@ import useAuthUser from "react-auth-kit/hooks/useAuthUser"; const Question = (props) => { const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000'; - const [question, setQuestion] = useState([]); + const [currentQuestion, setCurrentQuestion] = useState(0); + const [questions, setQuestions] = useState([]); const [loading, setLoading] = useState(true); + const [renderedImages, setRenderedImages] = useState(0); const [counter, setCounter] = useState(0); const [score, setScore] = useState(0); const auth = useAuthUser(); + const questionsPerGame = 10; + const imagesPerQuestion = 4; + + useEffect(() => { + fetchQuestions(); + }, []); useEffect(() => { const interval = setInterval(() => { - setCounter((prevCounter) => prevCounter + 0.4); + if(renderedImages==imagesPerQuestion){ + setCounter((prevCounter) => prevCounter + 0.4); + } }, 40); return () => clearInterval(interval); - }, []); + }, [renderedImages]); useEffect(() => { - if (counter >= 100) { - setLoading(true); - fetchQuestion(); + if (counter >= 100 && !loading) { + answerQuestion("",questions[currentQuestion]); } }, [counter]); - - const fetchQuestion = async () => { + const fetchQuestions = async () => { try { - - const res = await axios.get(`${apiEndpoint}/${props.type}/${props.category}/question`); - setQuestion(res.data); - setLoading(false); - setCounter(0); - + setRenderedImages(0) + let auxQuestions = [] + for(let i=0;i { const answerQuestion = async (answer, question) => { try { setLoading(true); + setRenderedImages(0) + if(currentQuestion>=questionsPerGame-1){ + fetchQuestions() + setCurrentQuestion(0) + setCounter(0); + return + } const result = await axios.post(`${apiEndpoint}/${props.type}/answer`, { answer: answer, question:question, username: auth.username, category: props.category }, { headers: {'Content-Type': 'application/json'}}); - const res = await axios.get(`${apiEndpoint}/${props.type}/${props.category}/question`); if ( result.data.correct === "true" ) { setScore(score +1); } - setQuestion(res.data); - setLoading(false); + setCurrentQuestion((question)=>question+1) setCounter(0); - - + setLoading(false) } catch (error) { console.log(error); } } - useEffect(() => { - fetchQuestion(); - - }, []); + return (
@@ -73,22 +84,24 @@ const Question = (props) => { Score: {score}
{loading ? ( -

-
-

+ <> +

+
+

+ ) : ( <> - -

{question.question}

+

{questions[currentQuestion].question}

- {question.images.map(image => ( + {questions[currentQuestion].images.map(image => ( ))}