frontAnswerer/lib/api/useQuizGetNext.ts

35 lines
1009 B
TypeScript
Raw Normal View History

2025-04-30 17:59:50 +00:00
import { useState } from "react";
2025-05-01 13:15:54 +00:00
import { getQuizDataAI } from "./quizRelase";
import { addQuestion, useQuizStore } from "@/stores/useQuizStore";
2025-04-30 17:59:50 +00:00
export const useQuizGetNext = () => {
2025-05-01 13:15:54 +00:00
const { quizId, settings } = useQuizStore();
2025-04-30 17:59:50 +00:00
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const [currentPage, setCurrentPage] = useState(1);
const loadMoreQuestions = async () => {
2025-05-01 13:15:54 +00:00
if (settings.status === "ai") {
setIsLoading(true);
setError(null);
2025-04-30 17:59:50 +00:00
2025-05-01 13:15:54 +00:00
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);
2025-04-30 17:59:50 +00:00
}
}
};
return { loadMoreQuestions, isLoading, error, currentPage };
};