frontAnswerer/lib/api/useQuizGetNext.ts

33 lines
1002 B
TypeScript
Raw Normal View History

2025-04-30 17:59:50 +00:00
import { useQuizSettings } from "@/contexts/QuizDataContext";
import { useState } from "react";
import { getQuizData } from "./quizRelase";
export const useQuizGetNext = () => {
const { addQuestion, quizId, settings } = useQuizSettings();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const [currentPage, setCurrentPage] = useState(1);
const loadMoreQuestions = async () => {
setIsLoading(true);
setError(null);
try {
const data = await getQuizData({ quizId, type: settings.cfg.type || "", status: settings.status });
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 };
};