From 6432e0a97fcc3eada03ae876a2d287b5a0b53278 Mon Sep 17 00:00:00 2001 From: Ibrahim-Halil-Kuray <71791007+Ibrahim-Halil-Kuray@users.noreply.github.com> Date: Sun, 29 Oct 2023 14:21:13 +0100 Subject: [PATCH] Update Questions.tsx Show result --- src/components/Questions.tsx | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/components/Questions.tsx b/src/components/Questions.tsx index 17f7462..bcff0d2 100644 --- a/src/components/Questions.tsx +++ b/src/components/Questions.tsx @@ -18,6 +18,7 @@ const Questions: React.FC = ({ apiUrl }) => { const [isTimeUp, setIsTimeUp] = useState(false); const [isAnswered, setIsAnswered] = useState(false); const [timer, setTimer] = useState(0); + const [showResults, setShowResults] = useState(false); useEffect(() => { fetch(apiUrl) @@ -52,8 +53,9 @@ const Questions: React.FC = ({ apiUrl }) => { return () => { clearInterval(interval); }; - // eslint-disable-next-line react-hooks/exhaustive-deps + // eslint-disable-next-line react-hooks/exhaustive-deps }, [timer, currentQuestionIndex, questions, isTimeUp]); + const handleAnswerQuestion = (selectedOption: number) => { const correctAnswer = questions[currentQuestionIndex].correct_answer; setUserAnswers([...userAnswers, selectedOption === correctAnswer ? 1 : 0]); @@ -65,15 +67,30 @@ const Questions: React.FC = ({ apiUrl }) => { setCurrentQuestionIndex(currentQuestionIndex + 1); setIsAnswered(false); } else { - // Show result + setShowResults(true); } }; + const renderResults = () => { + const correctAnswers = userAnswers.filter((answer) => answer === 1).length; + const wrongAnswers = userAnswers.filter((answer) => answer === 0).length; + + return ( +
+

You have answered all questions

+

Correct answers: {correctAnswers}

+

Wrong answers: {wrongAnswers}

+
+ ); + }; + return (
{timer > 0 && !isTimeUp &&

Timer: {timer} second

} - {questions.length > 0 && currentQuestionIndex < questions.length ? ( + {showResults ? ( + renderResults() + ) : questions.length > 0 && currentQuestionIndex < questions.length ? (

Question {currentQuestionIndex + 1}

{questions[currentQuestionIndex].question}

@@ -105,14 +122,7 @@ const Questions: React.FC = ({ apiUrl }) => {
) : (
-

You have have answred all question

-

- Correct answers:{' '} - {userAnswers.filter((answer) => answer === 1).length} -

-

- Wrong answers: {userAnswers.filter((answer) => answer === 0).length} -

+ {renderResults()}
)}