Skip to content

Commit

Permalink
Merge pull request #1 from Ibrahim-Halil-Kuray/local
Browse files Browse the repository at this point in the history
Update Questions.tsx
  • Loading branch information
Ibrahim-Halil-Kuray committed Oct 29, 2023
2 parents 91f9603 + 329bfd6 commit dfc93c0
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/components/Questions.tsx
Expand Up @@ -18,6 +18,7 @@ const Questions: React.FC<QuestionsProps> = ({ apiUrl }) => {
const [isTimeUp, setIsTimeUp] = useState<boolean>(false);
const [isAnswered, setIsAnswered] = useState<boolean>(false);
const [timer, setTimer] = useState<number>(0);
const [showResults, setShowResults] = useState<boolean>(false);

useEffect(() => {
fetch(apiUrl)
Expand Down Expand Up @@ -52,8 +53,9 @@ const Questions: React.FC<QuestionsProps> = ({ 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]);
Expand All @@ -65,16 +67,29 @@ const Questions: React.FC<QuestionsProps> = ({ 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 (
<div>
<h1>You have answered all questions</h1>
<p>Correct answers: {correctAnswers}</p>
<p>Wrong answers: {wrongAnswers}</p>
</div>
);
};

return (
<div>
{timer > 0 && !isTimeUp && <p className='text-2xl font-bold text-right'>Timer: {timer} second</p>}

{questions.length > 0 && currentQuestionIndex < questions.length ? (

<div>
<br />
<h1 className='text-xl font-bold'>Question {currentQuestionIndex + 1}</h1>
Expand Down Expand Up @@ -121,14 +136,7 @@ const Questions: React.FC<QuestionsProps> = ({ apiUrl }) => {
</div>
) : (
<div>
<h1>You have have answred all question</h1>
<p>
Correct answers:{' '}
{userAnswers.filter((answer) => answer === 1).length}
</p>
<p>
Wrong answers: {userAnswers.filter((answer) => answer === 0).length}
</p>
{renderResults()}
</div>
)}
</div>
Expand Down

0 comments on commit dfc93c0

Please sign in to comment.