33 lines
1002 B
TypeScript
33 lines
1002 B
TypeScript
|
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 };
|
||
|
};
|