From 6890c93cf88bfeba204ecd7e7c76b2b67a94cf04 Mon Sep 17 00:00:00 2001 From: Krish Chothani <143370415+KrishChothani@users.noreply.github.com> Date: Sat, 12 Oct 2024 08:20:04 +0530 Subject: [PATCH] Quiz Ui Redesign (#70) * Improve Quize UI * Improve --- frontend/src/appComponents/QuizPage.tsx | 205 ++++++++++++++---------- 1 file changed, 119 insertions(+), 86 deletions(-) diff --git a/frontend/src/appComponents/QuizPage.tsx b/frontend/src/appComponents/QuizPage.tsx index c3035aa..d330a44 100644 --- a/frontend/src/appComponents/QuizPage.tsx +++ b/frontend/src/appComponents/QuizPage.tsx @@ -24,12 +24,10 @@ export default function QuizPage() { const { id } = useParams<{ id: string }>(); const navigate = useNavigate(); + useEffect(() => { if (userId && secretCode != "") { fetchQuiz(); - } else { - console.log("userId", userId); - console.log("secretCode", secretCode); } }, [userId]); @@ -43,8 +41,6 @@ export default function QuizPage() { }, ); const data = response.data; - console.log("data"); - console.log(data); setQuiz(data); setUserAnswers({}); } catch (error) { @@ -81,7 +77,9 @@ export default function QuizPage() { correctAnswers++; } }); - const finalScore = quiz ? (correctAnswers / quiz.questions.length) * 100 : 0; + const finalScore = quiz + ? (correctAnswers / quiz.questions.length) * 100 + : 0; setScore(finalScore); axios @@ -97,7 +95,8 @@ export default function QuizPage() { "Content-Type": "application/json", }, }, - ).then(() => { + ) + .then(() => { alert("Quiz submitted successfully"); navigate("/"); }) @@ -108,100 +107,134 @@ export default function QuizPage() { if (!userId) { return ( -
-

Enter Your Information

-
- setUserInfo({ ...userInfo, name: e.target.value })} - placeholder="Name" - className="w-full p-2 border rounded" - required - /> +
+
+

+ Enter Your Information +

+ + + setUserInfo({ ...userInfo, name: e.target.value }) + } + placeholder="Name" + className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" + required + /> + + setUserInfo({ ...userInfo, rollNo: e.target.value }) + } + placeholder="Roll No" + className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" + required + /> + + setUserInfo({ ...userInfo, year: e.target.value }) + } + placeholder="Year" + className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" + required + /> + + +
+
+ ); + } + + if (!quiz) { + return ( +
+
+

+ Enter Secret Code +

- setUserInfo({ ...userInfo, rollNo: e.target.value }) - } - placeholder="Roll No" - className="w-full p-2 border rounded" - required - /> - setUserInfo({ ...userInfo, year: e.target.value })} - placeholder="Year" - className="w-full p-2 border rounded" - required + value={secretCode} + onChange={(e) => setSecretCode(e.target.value)} + placeholder="Secret Code" + className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 mb-6" /> - -
- ); - } - - if (!quiz) { - return ( -
-

Enter Secret Code

- setSecretCode(e.target.value)} - placeholder="Secret Code" - className="w-full p-2 border rounded mb-4" - /> - +
); } return ( -
-

{quiz.title}

-
- {quiz.questions.map((question) => ( -
-

{question.question}

- {question.options.split(",").map((option, index) => ( - +
+
+ {quiz ? ( + <> +

+ {quiz.title || "Quiz Title"} +

+ + {quiz.questions.map((question) => ( +
+

+ {question.question || "Untitled Question"} +

+
+ {question.options.split(",").map((option, index) => ( + + ))} +
+
))} -
- ))} - - - {score !== null && ( -
-

Your Score: {score.toFixed(2)}%

+ + + {score !== null && ( +
+

+ Your Score: {score.toFixed(2)}% +

+
+ )} + + ) : ( +
+

Loading Quiz...

)}
+
+ ); }