2024-04-02 13:09:13 +00:00
|
|
|
import { sendAnswer } from "@api/quizRelase";
|
|
|
|
import { useQuizData } from "@contexts/QuizDataContext";
|
|
|
|
import { ThemeProvider } from "@mui/material";
|
2024-02-12 10:58:51 +00:00
|
|
|
import { useQuizViewStore } from "@stores/quizView";
|
2024-02-08 13:42:31 +00:00
|
|
|
import { useQuestionFlowControl } from "@utils/hooks/useQuestionFlowControl";
|
|
|
|
import { notReachable } from "@utils/notReachable";
|
2024-01-30 16:49:33 +00:00
|
|
|
import { quizThemes } from "@utils/themes/Publication/themePublication";
|
2024-04-11 14:11:43 +00:00
|
|
|
import "https://markknol.github.io/console-log-viewer/console-log-viewer.js";
|
2024-04-02 13:09:13 +00:00
|
|
|
import { enqueueSnackbar } from "notistack";
|
2024-02-08 13:42:31 +00:00
|
|
|
import { ReactElement, useEffect } from "react";
|
|
|
|
import { ContactForm } from "./ContactForm";
|
2023-12-16 14:55:56 +00:00
|
|
|
import { Question } from "./Question";
|
2024-02-08 13:42:31 +00:00
|
|
|
import { ResultForm } from "./ResultForm";
|
2024-01-30 16:49:33 +00:00
|
|
|
import { StartPageViewPublication } from "./StartPageViewPublication";
|
2024-03-26 00:06:54 +00:00
|
|
|
import NextButton from "./tools/NextButton";
|
2024-04-02 13:09:13 +00:00
|
|
|
import PrevButton from "./tools/PrevButton";
|
2024-04-11 14:11:43 +00:00
|
|
|
import QuestionSelect from "./QuestionSelect";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2024-02-08 13:42:31 +00:00
|
|
|
export default function ViewPublicationPage() {
|
2024-04-06 14:01:04 +00:00
|
|
|
const { settings, recentlyCompleted, quizId, preview, changeFaviconAndTitle } = useQuizData();
|
2024-04-03 12:42:12 +00:00
|
|
|
const answers = useQuizViewStore(state => state.answers);
|
2024-04-02 13:09:13 +00:00
|
|
|
let currentQuizStep = useQuizViewStore((state) => state.currentQuizStep);
|
|
|
|
const {
|
|
|
|
currentQuestion,
|
|
|
|
currentQuestionStepNumber,
|
|
|
|
isNextButtonEnabled,
|
|
|
|
isPreviousButtonEnabled,
|
|
|
|
moveToPrevQuestion,
|
|
|
|
moveToNextQuestion,
|
|
|
|
showResultAfterContactForm,
|
2024-04-11 14:11:43 +00:00
|
|
|
setQuestion,
|
2024-04-02 13:09:13 +00:00
|
|
|
} = useQuestionFlowControl();
|
2024-01-30 16:49:33 +00:00
|
|
|
|
2024-04-02 13:09:13 +00:00
|
|
|
const isAnswer = answers.some(ans => ans.questionId === currentQuestion.id);
|
2024-04-01 05:54:12 +00:00
|
|
|
|
2024-04-06 14:01:04 +00:00
|
|
|
useEffect(function setFaviconAndTitle() {
|
|
|
|
if (!changeFaviconAndTitle) return;
|
2024-02-02 14:35:02 +00:00
|
|
|
|
2024-04-06 14:01:04 +00:00
|
|
|
const link = document.querySelector('link[rel="icon"]');
|
|
|
|
if (link && settings.cfg.startpage.favIcon) {
|
|
|
|
link.setAttribute("href", settings.cfg.startpage.favIcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.title = settings.name;
|
|
|
|
}, [changeFaviconAndTitle, settings.cfg.startpage.favIcon, settings.name]);
|
2024-01-15 17:41:15 +00:00
|
|
|
|
2024-04-02 13:09:13 +00:00
|
|
|
if (recentlyCompleted) throw new Error("Quiz already completed");
|
|
|
|
if (currentQuizStep === "startpage" && settings.cfg.noStartPage)
|
|
|
|
currentQuizStep = "question";
|
2024-02-14 11:03:35 +00:00
|
|
|
|
2024-04-02 13:09:13 +00:00
|
|
|
let quizStepElement: ReactElement;
|
|
|
|
switch (currentQuizStep) {
|
|
|
|
case "startpage": {
|
|
|
|
quizStepElement = <StartPageViewPublication />;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "question": {
|
|
|
|
if (currentQuestion.type === "result") {
|
|
|
|
quizStepElement = <ResultForm resultQuestion={currentQuestion} />;
|
|
|
|
break;
|
|
|
|
}
|
2024-02-08 13:42:31 +00:00
|
|
|
|
2024-04-02 13:09:13 +00:00
|
|
|
quizStepElement = (
|
|
|
|
<Question
|
|
|
|
currentQuestion={currentQuestion}
|
|
|
|
currentQuestionStepNumber={currentQuestionStepNumber}
|
|
|
|
prevButton={<PrevButton isPreviousButtonEnabled={isPreviousButtonEnabled} moveToPrevQuestion={moveToPrevQuestion} />}
|
|
|
|
nextButton={
|
|
|
|
<NextButton
|
|
|
|
isNextButtonEnabled={isNextButtonEnabled}
|
|
|
|
moveToNextQuestion={async () => {
|
|
|
|
if (!isAnswer) {
|
|
|
|
try {
|
|
|
|
await sendAnswer({
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
body: "",
|
|
|
|
qid: quizId,
|
|
|
|
preview
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
enqueueSnackbar("ответ не был засчитан");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
moveToNextQuestion();
|
|
|
|
}}
|
2024-04-11 14:11:43 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
questionSelect={
|
|
|
|
<QuestionSelect
|
|
|
|
selectedQuestion={currentQuestion}
|
|
|
|
setQuestion={setQuestion}
|
|
|
|
/>
|
|
|
|
}
|
2024-04-02 13:09:13 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "contactform": {
|
|
|
|
quizStepElement = (
|
|
|
|
<ContactForm
|
|
|
|
currentQuestion={currentQuestion}
|
|
|
|
onShowResult={showResultAfterContactForm}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
notReachable(currentQuizStep);
|
2024-02-08 13:42:31 +00:00
|
|
|
}
|
2024-01-30 16:49:33 +00:00
|
|
|
|
2024-04-02 13:09:13 +00:00
|
|
|
return (
|
|
|
|
<ThemeProvider
|
|
|
|
theme={quizThemes[settings.cfg.theme || "StandardTheme"].theme}
|
|
|
|
>
|
|
|
|
{quizStepElement}
|
|
|
|
</ThemeProvider>
|
|
|
|
);
|
2024-02-28 14:10:50 +00:00
|
|
|
}
|