frontAnswerer/lib/api/useQuizGetNext.ts
Nastya 17013922df
Some checks failed
Deploy / CreateImage (push) Successful in 4m10s
Deploy / DeployService (push) Failing after 24s
Merge branch 'newai'
2025-05-01 19:01:40 +03:00

46 lines
1.4 KiB
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 () => {
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 };
};