frontAnswerer/lib/api/useQuizGetNext.ts
Nastya aefda2b767
All checks were successful
Deploy / CreateImage (push) Successful in 3m9s
Deploy / DeployService (push) Successful in 21s
Merge branch 'newai'
2025-05-02 16:02:33 +03:00

78 lines
2.1 KiB
TypeScript

import { useState } from "react";
import { getQuizDataAI } from "./quizRelase";
import { addQuestion, useQuizStore } from "@/stores/useQuizStore";
import { AnyTypedQuizQuestion } from "..";
function qparse(q: { desc: string; id: string; req: boolean; title: string; typ: string }) {
return {
description: q.desc,
id: q.id,
required: q.req,
title: q.title,
type: q.typ,
page: 0,
content: {
answerType: "single",
autofill: false,
back: "",
hint: { text: "", video: "" },
id: "",
innerName: "",
innerNameCheck: false,
onlyNumbers: false,
originalBack: "",
placeholder: "",
required: false,
rule: {
children: [],
default: "",
main: [],
parentId: "",
},
},
};
}
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 = qparse(data[0]);
console.log("newQuestion");
console.log(newQuestion);
if (newQuestion) {
newQuestion.page = currentPage;
//@ts-ignore
addQuestion(newQuestion as AnyTypedQuizQuestion);
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 };
};