2024-06-22 15:35:11 +00:00
|
|
|
|
import { ContactForm } from "@/components/ViewPublicationPage/ContactForm/ContactForm.tsx";
|
|
|
|
|
import { extractImageLinksFromQuestion } from "@/utils/extractImageLinks";
|
|
|
|
|
import { useVKMetrics } from "@/utils/hooks/metrics/useVKMetrics";
|
|
|
|
|
import { useYandexMetrics } from "@/utils/hooks/metrics/useYandexMetrics";
|
2024-05-06 13:47:19 +00:00
|
|
|
|
import { sendAnswer } from "@api/quizRelase";
|
2024-05-31 17:56:17 +00:00
|
|
|
|
import { useQuizSettings } from "@contexts/QuizDataContext";
|
2024-05-06 13:47:19 +00:00
|
|
|
|
import { ThemeProvider, Typography } from "@mui/material";
|
|
|
|
|
import { useQuizViewStore } from "@stores/quizView";
|
|
|
|
|
import { useQuestionFlowControl } from "@utils/hooks/useQuestionFlowControl";
|
|
|
|
|
import { notReachable } from "@utils/notReachable";
|
|
|
|
|
import { quizThemes } from "@utils/themes/Publication/themePublication";
|
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
|
|
|
|
import { ReactElement, useEffect } from "react";
|
2024-06-22 15:35:11 +00:00
|
|
|
|
import { Helmet } from "react-helmet";
|
2024-05-06 13:47:19 +00:00
|
|
|
|
import { Question } from "./Question";
|
2024-06-22 15:35:11 +00:00
|
|
|
|
import QuestionSelect from "./QuestionSelect";
|
2024-05-06 13:47:19 +00:00
|
|
|
|
import { ResultForm } from "./ResultForm";
|
|
|
|
|
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";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
2024-02-08 13:42:31 +00:00
|
|
|
|
export default function ViewPublicationPage() {
|
2024-05-31 17:56:17 +00:00
|
|
|
|
const { settings, recentlyCompleted, quizId, preview, changeFaviconAndTitle } = useQuizSettings();
|
2024-04-22 08:55:19 +00:00
|
|
|
|
const answers = useQuizViewStore((state) => state.answers);
|
|
|
|
|
let currentQuizStep = useQuizViewStore((state) => state.currentQuizStep);
|
|
|
|
|
const {
|
|
|
|
|
currentQuestion,
|
|
|
|
|
currentQuestionStepNumber,
|
2024-06-22 15:35:11 +00:00
|
|
|
|
nextQuestion,
|
2024-04-22 08:55:19 +00:00
|
|
|
|
isNextButtonEnabled,
|
|
|
|
|
isPreviousButtonEnabled,
|
|
|
|
|
moveToPrevQuestion,
|
|
|
|
|
moveToNextQuestion,
|
|
|
|
|
showResultAfterContactForm,
|
|
|
|
|
setQuestion,
|
|
|
|
|
} = useQuestionFlowControl();
|
2024-05-06 13:47:19 +00:00
|
|
|
|
useYandexMetrics(settings?.cfg?.yandexMetricsNumber);
|
|
|
|
|
useVKMetrics(settings?.cfg?.vkMetricsNumber);
|
2024-01-30 16:49:33 +00:00
|
|
|
|
|
2024-05-31 16:41:18 +00:00
|
|
|
|
const isAnswer = answers.some((ans) => ans.questionId === currentQuestion?.id);
|
2024-04-01 05:54:12 +00:00
|
|
|
|
|
2024-04-22 08:55:19 +00:00
|
|
|
|
useEffect(
|
|
|
|
|
function setFaviconAndTitle() {
|
|
|
|
|
if (!changeFaviconAndTitle) return;
|
2024-04-18 15:23:27 +00:00
|
|
|
|
|
2024-04-22 08:55:19 +00:00
|
|
|
|
const link = document.querySelector('link[rel="icon"]');
|
|
|
|
|
if (link && settings.cfg.startpage.favIcon) {
|
|
|
|
|
link.setAttribute("href", settings.cfg.startpage.favIcon);
|
|
|
|
|
}
|
2024-02-02 14:35:02 +00:00
|
|
|
|
|
2024-04-22 08:55:19 +00:00
|
|
|
|
document.title = settings.name;
|
|
|
|
|
},
|
|
|
|
|
[changeFaviconAndTitle, settings.cfg.startpage.favIcon, settings.name]
|
|
|
|
|
);
|
2024-04-06 14:01:04 +00:00
|
|
|
|
|
2024-05-31 17:56:17 +00:00
|
|
|
|
if (settings.cfg.antifraud && recentlyCompleted) throw new Error("Quiz already completed");
|
2024-05-31 16:41:18 +00:00
|
|
|
|
if (currentQuizStep === "startpage" && settings.cfg.noStartPage) currentQuizStep = "question";
|
2024-01-15 17:41:15 +00:00
|
|
|
|
|
2024-04-22 08:55:19 +00:00
|
|
|
|
if (!currentQuestion)
|
|
|
|
|
return (
|
2024-05-31 16:41:18 +00:00
|
|
|
|
<ThemeProvider theme={quizThemes[settings.cfg.theme || "StandardTheme"].theme}>
|
2024-06-22 15:35:11 +00:00
|
|
|
|
<Typography
|
|
|
|
|
textAlign={"center"}
|
|
|
|
|
mt="50px"
|
|
|
|
|
>
|
2024-04-22 08:55:19 +00:00
|
|
|
|
Вопрос не выбран
|
|
|
|
|
</Typography>
|
|
|
|
|
</ThemeProvider>
|
2024-04-12 11:29:37 +00:00
|
|
|
|
);
|
2024-02-14 11:03:35 +00:00
|
|
|
|
|
2024-04-22 08:55:19 +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-22 08:55:19 +00:00
|
|
|
|
quizStepElement = (
|
|
|
|
|
<Question
|
2024-05-07 14:44:51 +00:00
|
|
|
|
key={currentQuestion.id}
|
2024-04-22 08:55:19 +00:00
|
|
|
|
currentQuestion={currentQuestion}
|
|
|
|
|
currentQuestionStepNumber={currentQuestionStepNumber}
|
|
|
|
|
prevButton={
|
2024-06-22 15:35:11 +00:00
|
|
|
|
<PrevButton
|
|
|
|
|
isPreviousButtonEnabled={isPreviousButtonEnabled}
|
|
|
|
|
moveToPrevQuestion={moveToPrevQuestion}
|
|
|
|
|
/>
|
2024-04-22 08:55:19 +00:00
|
|
|
|
}
|
|
|
|
|
nextButton={
|
|
|
|
|
<NextButton
|
|
|
|
|
isNextButtonEnabled={isNextButtonEnabled}
|
|
|
|
|
moveToNextQuestion={async () => {
|
|
|
|
|
if (!isAnswer) {
|
|
|
|
|
try {
|
|
|
|
|
await sendAnswer({
|
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
|
body: "",
|
|
|
|
|
qid: quizId,
|
|
|
|
|
preview,
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
enqueueSnackbar("ответ не был засчитан");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
moveToNextQuestion();
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
}
|
2024-06-22 15:35:11 +00:00
|
|
|
|
questionSelect={
|
|
|
|
|
<QuestionSelect
|
|
|
|
|
selectedQuestion={currentQuestion}
|
|
|
|
|
setQuestion={setQuestion}
|
|
|
|
|
/>
|
|
|
|
|
}
|
2024-04-22 08:55:19 +00:00
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
break;
|
2024-02-08 13:42:31 +00:00
|
|
|
|
}
|
2024-04-22 08:55:19 +00:00
|
|
|
|
case "contactform": {
|
2024-06-22 15:35:11 +00:00
|
|
|
|
quizStepElement = (
|
|
|
|
|
<ContactForm
|
|
|
|
|
currentQuestion={currentQuestion}
|
|
|
|
|
onShowResult={showResultAfterContactForm}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2024-04-22 08:55:19 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
notReachable(currentQuizStep);
|
|
|
|
|
}
|
2024-01-30 16:49:33 +00:00
|
|
|
|
|
2024-06-22 15:35:11 +00:00
|
|
|
|
const preloadLinks = new Set([
|
|
|
|
|
...extractImageLinksFromQuestion(currentQuestion),
|
|
|
|
|
...extractImageLinksFromQuestion(nextQuestion),
|
|
|
|
|
]);
|
|
|
|
|
|
2024-04-22 08:55:19 +00:00
|
|
|
|
return (
|
2024-06-22 15:35:11 +00:00
|
|
|
|
<ThemeProvider theme={quizThemes[settings.cfg.theme || "StandardTheme"].theme}>
|
|
|
|
|
<Helmet>
|
|
|
|
|
{Array.from(preloadLinks).map((link) => (
|
|
|
|
|
<link
|
|
|
|
|
key={link}
|
|
|
|
|
rel="preload"
|
|
|
|
|
as="image"
|
|
|
|
|
href={link}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Helmet>
|
|
|
|
|
{quizStepElement}
|
|
|
|
|
</ThemeProvider>
|
2024-04-22 08:55:19 +00:00
|
|
|
|
);
|
2024-02-28 14:10:50 +00:00
|
|
|
|
}
|