frontAnswerer/lib/api/useQuizGetNext.ts
2025-05-01 16:15:54 +03:00

35 lines
1009 B
TypeScript

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<Error | null>(null);
const [currentPage, setCurrentPage] = useState(1);
const loadMoreQuestions = async () => {
if (settings.status === "ai") {
setIsLoading(true);
setError(null);
try {
const data = await getQuizDataAI(quizId);
const newQuestion = data?.questions[0];
if (newQuestion) {
newQuestion.page = currentPage;
addQuestion(newQuestion);
setCurrentPage((old) => old++);
return newQuestion;
}
} catch (err) {
setError(err as Error);
} finally {
setIsLoading(false);
}
}
};
return { loadMoreQuestions, isLoading, error, currentPage };
};