2024-02-02 14:35:02 +00:00
|
|
|
import { getData } from "@api/quizRelase";
|
|
|
|
import { parseQuizData } from "@model/api/getQuizData";
|
|
|
|
import { QuizSettings } from "@model/settingsData";
|
|
|
|
import useSWR from "swr";
|
|
|
|
import { useQuizId } from "../../contexts/QuizIdContext";
|
2024-02-12 10:58:51 +00:00
|
|
|
import { replaceSpacesToEmptyLines } from "../../components/ViewPublicationPage/tools/replaceSpacesToEmptyLines";
|
2024-02-02 14:35:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
export function useQuizData() {
|
|
|
|
const quizId = useQuizId();
|
|
|
|
const { data } = useSWR(["quizData", quizId], params => getQuizData(params[1]), {
|
|
|
|
suspense: true,
|
2024-02-11 23:23:42 +00:00
|
|
|
revalidateOnFocus: false,
|
|
|
|
revalidateOnMount:false,
|
|
|
|
revalidateOnReconnect: false,
|
|
|
|
refreshWhenOffline: false,
|
|
|
|
refreshWhenHidden: false,
|
|
|
|
refreshInterval: 0
|
2024-02-02 14:35:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getQuizData(quizId: string) {
|
|
|
|
const response = await getData(quizId);
|
|
|
|
const quizDataResponse = response.data;
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
throw new Error(response.error);
|
|
|
|
}
|
|
|
|
if (!quizDataResponse) {
|
|
|
|
throw new Error("Quiz not found");
|
|
|
|
}
|
|
|
|
|
2024-02-08 13:42:31 +00:00
|
|
|
const quizSettings = replaceSpacesToEmptyLines(parseQuizData(quizDataResponse));
|
2024-02-02 14:35:02 +00:00
|
|
|
|
2024-02-10 19:25:08 +00:00
|
|
|
const res = JSON.parse(JSON.stringify({ data: quizSettings }).replaceAll(/\\" \\"/g, '""').replaceAll(/" "/g, '""')).data as QuizSettings;
|
|
|
|
res.recentlyCompleted = response.isRecentlyCompleted;
|
|
|
|
return res;
|
2024-02-02 14:35:02 +00:00
|
|
|
}
|