From f40b3a7627cc4d78d6675afeec9c6e6abd873711 Mon Sep 17 00:00:00 2001 From: bombnp Date: Sun, 11 Aug 2024 15:01:50 +0700 Subject: [PATCH] refactor: split client side component from page.tsx --- app/game/[id]/AnswerPage.tsx | 120 +++++++++++++++++++++++++++++++++++ app/game/[id]/page.tsx | 96 ++++------------------------ 2 files changed, 131 insertions(+), 85 deletions(-) create mode 100644 app/game/[id]/AnswerPage.tsx diff --git a/app/game/[id]/AnswerPage.tsx b/app/game/[id]/AnswerPage.tsx new file mode 100644 index 0000000..4e29af0 --- /dev/null +++ b/app/game/[id]/AnswerPage.tsx @@ -0,0 +1,120 @@ +"use client"; +import Link from "next/link"; +import quizs from "@/lib/database"; +import { useRouter } from "next/navigation"; +import { useState } from "react"; +import { useSearchParams } from "next/navigation"; + +interface AnswerPageProps { + id: string; +} + +export function AnswerPage({ id }: AnswerPageProps) { + const searchParams = useSearchParams(); + const correct = searchParams.get("correctOrWrong") === "true"; + const view = searchParams.get("view") === "true"; + const currentQuiz = quizs.quizs[Number(id)]; + const [selectedOption, setSelectedOption] = useState(""); + const [score, setScore] = useState(0); + const router = useRouter(); + + return ( +
+
+ + + + + +

บริหารการเงิน

+
+

+ {currentQuiz.id} quiz +

+
+

+ {currentQuiz.question} +

+
+ {/* {correct && !view ? ( +
+ ถูก +

+1 coin

+
+ ) : ( +
+ ผิด +

-1 coin

+

เฉลย: {currentQuiz.answer}

+
+ )} */} + {correct && !view ? ( +
+ ถูก +

+1 coin

+
+ ) : !correct && !view ? ( +
+ ผิด +

-1 coin

+

เฉลย: {currentQuiz.answer}

+
+ ) : view ? ( +
+ {currentQuiz.options.map((option, index) => ( + + ))} +
+ ) : ( +
Error Occur!
+ )} +
+ {!view && ( + + Next + + )} +
+
+ ); +} diff --git a/app/game/[id]/page.tsx b/app/game/[id]/page.tsx index 6f9960b..ef960ed 100644 --- a/app/game/[id]/page.tsx +++ b/app/game/[id]/page.tsx @@ -1,91 +1,17 @@ -'use client' -import Link from "next/link"; import quizs from "@/lib/database"; -import { useRouter } from "next/navigation"; -import { useState } from "react"; -import { useSearchParams } from 'next/navigation' +import { AnswerPage } from "./AnswerPage"; +import { Suspense } from "react"; function Answer({ params }: { params: { id: string } }) { - const searchParams = useSearchParams() - const correct = searchParams.get('correctOrWrong') === 'true' - const view = searchParams.get('view') === 'true' - console.log("Param",params.id) - console.log("quizs",quizs) - const currentQuiz = quizs.quizs[Number(params.id)] - console.log(currentQuiz) - console.log(currentQuiz.id) - console.log("CorrectOrWrong Value:", correct, typeof correct) - console.log("View:", view) - const [selectedOption, setSelectedOption] = useState(""); - const [score, setScore] = useState(0); - const router = useRouter(); + return ( + + + + ); +} - return ( -
-
- - - - - -

บริหารการเงิน

-
-

{currentQuiz.id} quiz

-
-

{currentQuiz.question}

-
- {/* {correct && !view ? ( -
- ถูก -

+1 coin

-
- ) : ( -
- ผิด -

-1 coin

-

เฉลย: {currentQuiz.answer}

-
- )} */} - {correct && !view ? ( -
- ถูก -

+1 coin

-
- ) : !correct && !view ? ( -
- ผิด -

-1 coin

-

เฉลย: {currentQuiz.answer}

-
- ) : view ? ( -
- {currentQuiz.options.map((option, index) => ( - - ))} -
- ) : ( -
- Error Occur! -
- )} -
- {!view && Next} -
-
- ); +export function generateStaticParams() { + return quizs.quizs.map((_, index) => ({ id: String(index) })); } -export default Answer; \ No newline at end of file +export default Answer;