2023-11-29 13:49:52 +00:00
|
|
|
|
import { questionApi } from "@api/question";
|
|
|
|
|
import { devlog } from "@frontend/kitui";
|
|
|
|
|
import { isAxiosError } from "axios";
|
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
import { setQuestions } from "./actions";
|
|
|
|
|
import { useQuestionsStore } from "./store";
|
|
|
|
|
import { useCurrentQuiz } from "@root/quizes/hooks";
|
2023-12-14 09:40:53 +00:00
|
|
|
|
import { useEffect } from "react";
|
2023-11-29 13:49:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useQuestions() {
|
|
|
|
|
const quiz = useCurrentQuiz();
|
|
|
|
|
const { isLoading, error, isValidating } = useSWR(["questions", quiz?.backendId], ([, id]) => questionApi.getList({ quiz_id: id }), {
|
|
|
|
|
onSuccess: setQuestions,
|
|
|
|
|
onError: error => {
|
|
|
|
|
const message = isAxiosError<string>(error) ? (error.response?.data ?? "") : "";
|
|
|
|
|
|
|
|
|
|
devlog("Error getting question list", error);
|
|
|
|
|
enqueueSnackbar(`Не удалось получить вопросы. ${message}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const questions = useQuestionsStore(state => state.questions);
|
|
|
|
|
|
|
|
|
|
return { questions, isLoading, error, isValidating };
|
|
|
|
|
}
|