Skip to content

Commit

Permalink
Merge pull request #373 from VinayLodhi1712/backbutton
Browse files Browse the repository at this point in the history
back buttons added for quiz page: #351 done
  • Loading branch information
Avdhesh-Varshney authored Oct 29, 2024
2 parents 61be3b7 + fbfe066 commit 17f82da
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
29 changes: 16 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"react-loader-spinner": "^6.1.6",
"react-parallax-tilt": "^1.7.229",
"react-player": "^2.16.0",
"react-router-dom": "^6.23.1",
"react-router-dom": "^6.27.0",
"react-spinners": "^0.14.1",
"react-top-loading-bar": "^2.3.1",
"react-typed": "^2.0.12"
Expand Down
22 changes: 17 additions & 5 deletions src/pages/resources/ChanakyaQuiz.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { useNavigate } from 'react-router-dom'; // Only import useNavigate
import "./../../css/ChanakyaQuiz.css";

const allQuestions = [
Expand Down Expand Up @@ -113,6 +114,8 @@ const getRandomQuestions = (questions, num) => {
};

const ChanakyaQuiz = () => {
const navigate = useNavigate(); // Initialize useNavigate

const [started, setStarted] = useState(false);
const [questions, setQuestions] = useState([]);
const [currentQuestionIndex, setCurrentQuestionIndex] = useState(0);
Expand All @@ -133,6 +136,10 @@ const ChanakyaQuiz = () => {
setUserAnswers(newAnswers);
};

const handleBackToHome = () => {
navigate('/'); // Navigate to the home page
};

const handleNext = () => {
if (currentQuestionIndex < questions.length - 1) {
setCurrentQuestionIndex(currentQuestionIndex + 1);
Expand Down Expand Up @@ -170,13 +177,17 @@ const ChanakyaQuiz = () => {
<>
<h1>Ready to attempt Quiz</h1>
<button className="start-button" onClick={startQuiz}>Start Quiz</button>
<button className="start-button" onClick={handleBackToHome}>Back</button>
</>
) : showResult ? (
<Result
score={calculateScore()}
questions={questions}
userAnswers={userAnswers}
/>
<>
<Result
score={calculateScore()}
questions={questions}
userAnswers={userAnswers}
/>
<button className="quizbuttons" onClick={handleBackToHome}>Back</button>
</>
) : (
<div>
<div className="question-number">
Expand All @@ -201,6 +212,7 @@ const ChanakyaQuiz = () => {
<button className="quizbuttons" onClick={finishQuiz}>Finish</button>
)}
<button className="quizbuttons" onClick={handleSkip}>Skip</button>
<button className="quizbuttons" onClick={handleBackToHome}>Back</button>
</div>
</div>
)}
Expand Down

0 comments on commit 17f82da

Please sign in to comment.