frontAnswerer/lib/api/useQuizGetNext.ts

78 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-04-30 17:59:50 +00:00
import { useState } from "react";
2025-05-01 13:15:54 +00:00
import { getQuizDataAI } from "./quizRelase";
import { addQuestion, useQuizStore } from "@/stores/useQuizStore";
2025-05-01 13:23:10 +00:00
import { AnyTypedQuizQuestion } from "..";
function qparse(q: { desc: string; id: string; req: boolean; title: string; typ: string }) {
return {
description: q.desc,
id: q.id,
required: q.req,
title: q.title,
type: q.typ,
page: 0,
content: {
answerType: "single",
autofill: false,
back: "",
hint: { text: "", video: "" },
id: "",
innerName: "",
innerNameCheck: false,
onlyNumbers: false,
originalBack: "",
placeholder: "",
required: false,
rule: {
children: [],
default: "",
main: [],
parentId: "",
},
},
};
}
2025-04-30 17:59:50 +00:00
export const useQuizGetNext = () => {
2025-05-01 13:15:54 +00:00
const { quizId, settings } = useQuizStore();
2025-04-30 17:59:50 +00:00
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const [currentPage, setCurrentPage] = useState(1);
const loadMoreQuestions = async () => {
2025-05-01 13:23:10 +00:00
console.log("STATUS loadMoreQuestions");
console.log(settings);
console.log(settings.status);
2025-05-01 13:15:54 +00:00
if (settings.status === "ai") {
2025-05-01 13:23:10 +00:00
console.log("STATUS after IF");
2025-05-01 13:15:54 +00:00
setIsLoading(true);
setError(null);
2025-04-30 17:59:50 +00:00
2025-05-01 13:15:54 +00:00
try {
2025-05-01 13:23:10 +00:00
console.log("STATUS after TRY TRY TRY");
2025-05-01 13:15:54 +00:00
const data = await getQuizDataAI(quizId);
2025-05-01 13:23:10 +00:00
console.log("data");
console.log(data);
const newQuestion = qparse(data[0]);
console.log("newQuestion");
console.log(newQuestion);
2025-05-01 13:15:54 +00:00
if (newQuestion) {
newQuestion.page = currentPage;
2025-05-01 13:23:10 +00:00
//@ts-ignore
addQuestion(newQuestion as AnyTypedQuizQuestion);
2025-05-01 13:15:54 +00:00
setCurrentPage((old) => old++);
2025-05-01 13:23:10 +00:00
console.log("newQuestion + page");
console.log(newQuestion);
2025-05-01 13:15:54 +00:00
return newQuestion;
}
} catch (err) {
setError(err as Error);
} finally {
setIsLoading(false);
2025-04-30 17:59:50 +00:00
}
}
};
return { loadMoreQuestions, isLoading, error, currentPage };
};