2024-05-31 17:56:17 +00:00
|
|
|
import { useQuizData } from "@/api/hooks";
|
2024-05-06 13:47:19 +00:00
|
|
|
import { QuizViewContext, createQuizViewStore } from "@/stores/quizView";
|
|
|
|
import LoadingSkeleton from "@/ui_kit/LoadingSkeleton";
|
|
|
|
import { useVkMetricsGoals } from "@/utils/hooks/metrics/useVkMetricsGoals";
|
|
|
|
import { useYandexMetricsGoals } from "@/utils/hooks/metrics/useYandexMetricsGoals";
|
2024-05-31 17:56:17 +00:00
|
|
|
import { QuizSettingsContext } from "@contexts/QuizDataContext";
|
|
|
|
import { RootContainerWidthContext } from "@contexts/RootContainerWidthContext";
|
|
|
|
import type { QuizSettings } from "@model/settingsData";
|
|
|
|
import { Box, CssBaseline, ScopedCssBaseline, ThemeProvider } from "@mui/material";
|
|
|
|
import { LocalizationProvider } from "@mui/x-date-pickers";
|
|
|
|
import { AdapterMoment } from "@mui/x-date-pickers/AdapterMoment";
|
|
|
|
import { ruRU } from "@mui/x-date-pickers/locales";
|
2024-02-02 14:35:02 +00:00
|
|
|
import { handleComponentError } from "@utils/handleComponentError";
|
2024-02-14 11:03:35 +00:00
|
|
|
import lightTheme from "@utils/themes/light";
|
2024-05-31 17:56:17 +00:00
|
|
|
import moment from "moment";
|
|
|
|
import { SnackbarProvider } from "notistack";
|
|
|
|
import { startTransition, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
|
|
import { ErrorBoundary } from "react-error-boundary";
|
|
|
|
import { ApologyPage } from "./ViewPublicationPage/ApologyPage";
|
|
|
|
import ViewPublicationPage from "./ViewPublicationPage/ViewPublicationPage";
|
2024-06-25 12:54:17 +00:00
|
|
|
import { HelmetProvider } from "react-helmet-async";
|
2024-02-01 13:18:16 +00:00
|
|
|
|
2024-10-26 17:24:49 +00:00
|
|
|
import "moment/dist/locale/ru";
|
2024-02-01 13:18:16 +00:00
|
|
|
moment.locale("ru");
|
2024-05-31 16:41:18 +00:00
|
|
|
const localeText = ruRU.components.MuiLocalizationProvider.defaultProps.localeText;
|
2024-02-01 13:18:16 +00:00
|
|
|
|
2024-02-14 11:03:35 +00:00
|
|
|
type Props = {
|
2024-05-06 13:47:19 +00:00
|
|
|
quizSettings?: QuizSettings;
|
|
|
|
quizId: string;
|
|
|
|
preview?: boolean;
|
|
|
|
changeFaviconAndTitle?: boolean;
|
|
|
|
className?: string;
|
|
|
|
disableGlobalCss?: boolean;
|
2024-02-14 11:03:35 +00:00
|
|
|
};
|
|
|
|
|
2024-05-06 13:47:19 +00:00
|
|
|
function QuizAnswererInner({
|
|
|
|
quizSettings,
|
|
|
|
quizId,
|
|
|
|
preview = false,
|
|
|
|
changeFaviconAndTitle = true,
|
|
|
|
className,
|
|
|
|
disableGlobalCss = false,
|
|
|
|
}: Props) {
|
|
|
|
const [quizViewStore] = useState(createQuizViewStore);
|
2024-05-31 16:41:18 +00:00
|
|
|
const [rootContainerWidth, setRootContainerWidth] = useState<number>(() => window.innerWidth);
|
2024-05-06 13:47:19 +00:00
|
|
|
const rootContainerRef = useRef<HTMLDivElement>(null);
|
2024-07-17 00:08:20 +00:00
|
|
|
const { data, error, isLoading } = useQuizData(quizId, preview);
|
2024-05-31 16:41:18 +00:00
|
|
|
const vkMetrics = useVkMetricsGoals(quizSettings?.settings.cfg.vkMetricsNumber);
|
|
|
|
const yandexMetrics = useYandexMetricsGoals(quizSettings?.settings.cfg.yandexMetricsNumber);
|
2024-05-06 13:47:19 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
vkMetrics.quizOpened();
|
|
|
|
yandexMetrics.quizOpened();
|
|
|
|
}, 4000);
|
2024-05-31 17:56:17 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-05-06 13:47:19 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
2024-05-31 16:41:18 +00:00
|
|
|
if (rootContainerRef.current) setRootContainerWidth(rootContainerRef.current.clientWidth);
|
2024-05-06 13:47:19 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const handleWindowResize = () => {
|
|
|
|
startTransition(() => {
|
2024-05-31 16:41:18 +00:00
|
|
|
if (rootContainerRef.current) setRootContainerWidth(rootContainerRef.current.clientWidth);
|
2024-05-06 13:47:19 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
window.addEventListener("resize", handleWindowResize);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("resize", handleWindowResize);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (isLoading) return <LoadingSkeleton />;
|
|
|
|
if (error) return <ApologyPage error={error} />;
|
2024-07-17 00:08:20 +00:00
|
|
|
// if (!data) return <LoadingSkeleton />;
|
2024-05-06 13:47:19 +00:00
|
|
|
quizSettings ??= data;
|
2024-07-17 00:08:20 +00:00
|
|
|
if (!quizSettings) return <ApologyPage error={new Error("Quiz data is null")} />;
|
2024-05-06 13:47:19 +00:00
|
|
|
|
2024-08-31 01:57:26 +00:00
|
|
|
if (quizSettings.questions.length === 1 && quizSettings?.settings.cfg.noStartPage)
|
|
|
|
return <ApologyPage error={new Error("Quiz is empty")} />;
|
|
|
|
// if (quizSettings.questions.length === 1) return <ApologyPage error={new Error("No questions found")} />;
|
2024-05-06 13:47:19 +00:00
|
|
|
if (!quizId) return <ApologyPage error={new Error("No quiz id")} />;
|
|
|
|
|
|
|
|
const quizContainer = (
|
|
|
|
<Box
|
|
|
|
ref={rootContainerRef}
|
|
|
|
className={className}
|
|
|
|
sx={{
|
|
|
|
width: "100%",
|
|
|
|
height: "100%",
|
|
|
|
position: "relative",
|
|
|
|
}}
|
|
|
|
>
|
2024-06-25 12:54:17 +00:00
|
|
|
<ErrorBoundary
|
|
|
|
FallbackComponent={ApologyPage}
|
|
|
|
onError={handleComponentError}
|
|
|
|
>
|
2024-05-06 13:47:19 +00:00
|
|
|
<ViewPublicationPage />
|
|
|
|
</ErrorBoundary>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<QuizViewContext.Provider value={quizViewStore}>
|
|
|
|
<RootContainerWidthContext.Provider value={rootContainerWidth}>
|
2024-05-31 17:56:17 +00:00
|
|
|
<QuizSettingsContext.Provider value={{ ...quizSettings, quizId, preview, changeFaviconAndTitle }}>
|
2024-05-06 13:47:19 +00:00
|
|
|
{disableGlobalCss ? (
|
|
|
|
<ScopedCssBaseline
|
|
|
|
sx={{
|
|
|
|
height: "100%",
|
|
|
|
width: "100%",
|
|
|
|
backgroundColor: "transparent",
|
|
|
|
}}
|
2024-04-24 15:56:11 +00:00
|
|
|
>
|
2024-05-06 13:47:19 +00:00
|
|
|
{quizContainer}
|
|
|
|
</ScopedCssBaseline>
|
|
|
|
) : (
|
|
|
|
<CssBaseline>{quizContainer}</CssBaseline>
|
|
|
|
)}
|
2024-05-31 17:56:17 +00:00
|
|
|
</QuizSettingsContext.Provider>
|
2024-05-06 13:47:19 +00:00
|
|
|
</RootContainerWidthContext.Provider>
|
|
|
|
</QuizViewContext.Provider>
|
|
|
|
);
|
2024-04-24 15:56:11 +00:00
|
|
|
}
|
|
|
|
export default function QuizAnswerer(props: Props) {
|
2024-05-06 13:47:19 +00:00
|
|
|
return (
|
2024-06-25 12:54:17 +00:00
|
|
|
<HelmetProvider>
|
|
|
|
<LocalizationProvider
|
|
|
|
dateAdapter={AdapterMoment}
|
|
|
|
adapterLocale="ru"
|
|
|
|
localeText={localeText}
|
|
|
|
>
|
|
|
|
<ThemeProvider theme={lightTheme}>
|
|
|
|
<SnackbarProvider
|
|
|
|
preventDuplicate={true}
|
|
|
|
style={{ backgroundColor: lightTheme.palette.brightPurple.main }}
|
|
|
|
>
|
2024-12-28 23:27:07 +00:00
|
|
|
<Box
|
|
|
|
id="hideoverlay"
|
|
|
|
sx={{
|
|
|
|
position: "absolute",
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
width: "100%",
|
|
|
|
height: "100%",
|
|
|
|
background: "black",
|
|
|
|
zIndex: 999,
|
|
|
|
opacity: 0,
|
2024-12-29 20:52:10 +00:00
|
|
|
pointerEvents: "auto",
|
2024-12-28 23:27:07 +00:00
|
|
|
}}
|
|
|
|
/>
|
2024-06-25 12:54:17 +00:00
|
|
|
<QuizAnswererInner {...props} />
|
|
|
|
</SnackbarProvider>
|
|
|
|
</ThemeProvider>
|
|
|
|
</LocalizationProvider>
|
|
|
|
</HelmetProvider>
|
2024-05-06 13:47:19 +00:00
|
|
|
);
|
2024-04-24 15:56:11 +00:00
|
|
|
}
|