import { useState } from "react"; import { getQuizDataAI } from "./quizRelase"; import { addQuestion, useQuizStore } from "@/stores/useQuizStore"; 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 = data?.questions[0]; console.log("newQuestion"); console.log(newQuestion); if (newQuestion) { newQuestion.page = currentPage; addQuestion(newQuestion); 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 }; };