generated from muhtadeetaron/nextjs-template
fix(exam): fix pretest screen start exam button
This commit is contained in:
@ -7,6 +7,7 @@ import DestructibleAlert from "@/components/DestructibleAlert";
|
||||
import BackgroundWrapper from "@/components/BackgroundWrapper";
|
||||
import { API_URL } from "@/lib/auth";
|
||||
import { useExam } from "@/context/ExamContext";
|
||||
import { Exam } from "@/types/exam";
|
||||
|
||||
interface Metadata {
|
||||
metadata: {
|
||||
@ -20,7 +21,7 @@ interface Metadata {
|
||||
export default function PretestPage() {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const [examData, setExamData] = useState();
|
||||
const [examData, setExamData] = useState<Exam>();
|
||||
const { startExam, setCurrentExam } = useExam();
|
||||
|
||||
// Get params from URL search params
|
||||
@ -41,14 +42,13 @@ export default function PretestPage() {
|
||||
const questionResponse = await fetch(`${API_URL}/mock/${id}`, {
|
||||
method: "GET",
|
||||
});
|
||||
const data = await questionResponse.json();
|
||||
console.log(data);
|
||||
|
||||
if (!questionResponse.ok) {
|
||||
throw new Error("Failed to fetch questions");
|
||||
}
|
||||
setExamData(data);
|
||||
const data = await questionResponse.json();
|
||||
const fetchedMetadata: Metadata = data;
|
||||
|
||||
setExamData(data);
|
||||
setMetadata(fetchedMetadata);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
@ -79,6 +79,25 @@ export default function PretestPage() {
|
||||
</BackgroundWrapper>
|
||||
);
|
||||
}
|
||||
const { isHydrated, isInitialized, currentExam } = useExam();
|
||||
|
||||
useEffect(() => {
|
||||
console.log(
|
||||
"hydrated:",
|
||||
isHydrated,
|
||||
"initialized:",
|
||||
isInitialized,
|
||||
"exam:",
|
||||
currentExam
|
||||
);
|
||||
}, [isHydrated, isInitialized, currentExam]);
|
||||
|
||||
function handleStartExam() {
|
||||
console.log(id);
|
||||
setCurrentExam(examData);
|
||||
startExam();
|
||||
router.push(`/exam/${id}?time=${metadata?.metadata.duration}`);
|
||||
}
|
||||
|
||||
return (
|
||||
<BackgroundWrapper>
|
||||
@ -176,11 +195,7 @@ export default function PretestPage() {
|
||||
</div>
|
||||
|
||||
<button
|
||||
onClick={() => {
|
||||
setCurrentExam(examData); // Set exam first
|
||||
startExam(); // Then start exam
|
||||
router.push(`/exam/${id}?time=${metadata.metadata.duration}`);
|
||||
}}
|
||||
onClick={async () => handleStartExam()}
|
||||
className="fixed bottom-0 w-full bg-[#113768] h-[78px] justify-center items-center flex text-white text-2xl font-bold"
|
||||
>
|
||||
Start Test
|
||||
|
||||
Reference in New Issue
Block a user