import { useState } from "react"; import { getQuizDataAI } from "./quizRelase"; import { addQuestion, useQuizStore } from "@/stores/useQuizStore"; 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: "", }, }, }; } export const useQuizGetNext = () => { const { quizId, settings } = useQuizStore(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [currentPage, setCurrentPage] = useState(1); const loadMoreQuestions = async () => { console.log("STATUS loadMoreQuestions"); console.log(settings); console.log(settings.status); if (settings.status === "ai") { console.log("STATUS after IF"); setIsLoading(true); setError(null); try { console.log("STATUS after TRY TRY TRY"); const data = await getQuizDataAI(quizId); console.log("data"); console.log(data); const newQuestion = qparse(data[0]); console.log("newQuestion"); console.log(newQuestion); if (newQuestion) { newQuestion.page = currentPage; //@ts-ignore addQuestion(newQuestion as AnyTypedQuizQuestion); setCurrentPage((old) => old++); console.log("newQuestion + page"); console.log(newQuestion); return newQuestion; } } catch (err) { setError(err as Error); } finally { setIsLoading(false); } } }; return { loadMoreQuestions, isLoading, error, currentPage }; };