From bb5e88a3708969de3eb0f938d09617f55d31302b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fern=C3=A1ndez=20Noriega?= Date: Thu, 11 Apr 2024 23:18:06 +0200 Subject: [PATCH 1/2] Questions prepared for game ending and fixed bug at ending time to answer --- webapp/src/components/Question.jsx | 78 +++++++++++++++++++----------- 1 file changed, 49 insertions(+), 29 deletions(-) diff --git a/webapp/src/components/Question.jsx b/webapp/src/components/Question.jsx index eec1b31..337815a 100644 --- a/webapp/src/components/Question.jsx +++ b/webapp/src/components/Question.jsx @@ -4,68 +4,86 @@ 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]); + useEffect(() => { + console.log(renderedImages) + }, [renderedImages]); - const fetchQuestion = async () => { + const fetchQuestions = async () => { + console.log("Questions are going to be fetched") 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 { + console.log("Answered question: "+currentQuestion) 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 +91,24 @@ const Question = (props) => { Score: {score}
{loading ? ( -

-
-

+ <> +

+
+

+ ) : ( <> - -

{question.question}

+

{questions[currentQuestion].question}

- {question.images.map(image => ( + {questions[currentQuestion].images.map(image => ( ))}
From a68253891be865b6b6dc9d70a26e2ed5a247a4ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fern=C3=A1ndez=20Noriega?= Date: Thu, 11 Apr 2024 23:46:44 +0200 Subject: [PATCH 2/2] Debugging console logs removed --- webapp/src/components/Question.jsx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/webapp/src/components/Question.jsx b/webapp/src/components/Question.jsx index 337815a..55d93fa 100644 --- a/webapp/src/components/Question.jsx +++ b/webapp/src/components/Question.jsx @@ -34,13 +34,8 @@ const Question = (props) => { } }, [counter]); - useEffect(() => { - console.log(renderedImages) - }, [renderedImages]); - const fetchQuestions = async () => { - console.log("Questions are going to be fetched") try { setRenderedImages(0) let auxQuestions = [] @@ -48,7 +43,6 @@ const Question = (props) => { let question = ((await axios.get(`${apiEndpoint}/${props.type}/${props.category}/question`)).data) auxQuestions.push(question) } - console.log(auxQuestions) setQuestions(auxQuestions) setLoading(false) } catch (error) { @@ -57,7 +51,6 @@ const Question = (props) => { }; const answerQuestion = async (answer, question) => { - console.log("Answered question: "+currentQuestion) try { setLoading(true); setRenderedImages(0)