2024-04-02 13:09:13 +00:00
|
|
|
import { getQuizData } from "@/api/quizRelase";
|
|
|
|
import LoadingSkeleton from "@/ui_kit/LoadingSkeleton";
|
2024-02-14 11:03:35 +00:00
|
|
|
import { QuizDataContext } from "@contexts/QuizDataContext";
|
2024-04-02 13:09:13 +00:00
|
|
|
import { RootContainerWidthContext } from "@contexts/RootContainerWidthContext";
|
2024-02-14 11:03:35 +00:00
|
|
|
import { QuizSettings } from "@model/settingsData";
|
2024-02-16 15:49:23 +00:00
|
|
|
import { Box, CssBaseline, ThemeProvider } from "@mui/material";
|
2024-02-01 13:18:16 +00:00
|
|
|
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-02-01 13:18:16 +00:00
|
|
|
import moment from "moment";
|
|
|
|
import { SnackbarProvider } from 'notistack';
|
2024-04-02 13:09:13 +00:00
|
|
|
import { startTransition, useEffect, useLayoutEffect, useRef, useState } from "react";
|
2024-02-02 14:35:02 +00:00
|
|
|
import { ErrorBoundary } from "react-error-boundary";
|
2024-04-02 13:09:13 +00:00
|
|
|
import useSWR from "swr";
|
2024-02-14 11:03:35 +00:00
|
|
|
import { ApologyPage } from "./ViewPublicationPage/ApologyPage";
|
2024-02-12 10:58:51 +00:00
|
|
|
import ViewPublicationPage from "./ViewPublicationPage/ViewPublicationPage";
|
2024-04-03 12:42:12 +00:00
|
|
|
import { QuizViewContext, createQuizViewStore } from "@/stores/quizView";
|
2024-02-01 13:18:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
moment.locale("ru");
|
|
|
|
const localeText = ruRU.components.MuiLocalizationProvider.defaultProps.localeText;
|
|
|
|
|
2024-02-14 11:03:35 +00:00
|
|
|
type Props = {
|
2024-04-02 13:09:13 +00:00
|
|
|
quizSettings?: QuizSettings;
|
2024-02-14 11:03:35 +00:00
|
|
|
quizId: string;
|
|
|
|
preview?: boolean;
|
2024-04-06 14:01:04 +00:00
|
|
|
changeFaviconAndTitle?: boolean;
|
2024-02-14 11:03:35 +00:00
|
|
|
};
|
|
|
|
|
2024-04-06 14:01:04 +00:00
|
|
|
export default function QuizAnswerer({ quizSettings, quizId, preview = false, changeFaviconAndTitle = true }: Props) {
|
2024-04-03 12:42:12 +00:00
|
|
|
const [quizViewStore] = useState(createQuizViewStore);
|
2024-02-16 15:49:23 +00:00
|
|
|
const [rootContainerWidth, setRootContainerWidth] = useState<number>(() => window.innerWidth);
|
|
|
|
const rootContainerRef = useRef<HTMLDivElement>(null);
|
2024-04-02 13:09:13 +00:00
|
|
|
const { data, error, isLoading } = useSWR(quizSettings ? null : ["quizData", quizId], params => getQuizData(params[1]), {
|
|
|
|
revalidateOnFocus: false,
|
|
|
|
revalidateOnReconnect: false,
|
|
|
|
shouldRetryOnError: false,
|
|
|
|
refreshInterval: 0,
|
|
|
|
});
|
2024-02-16 15:49:23 +00:00
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (rootContainerRef.current) setRootContainerWidth(rootContainerRef.current.clientWidth);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const handleWindowResize = () => {
|
|
|
|
startTransition(() => {
|
|
|
|
if (rootContainerRef.current) setRootContainerWidth(rootContainerRef.current.clientWidth);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
window.addEventListener("resize", handleWindowResize);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("resize", handleWindowResize);
|
|
|
|
};
|
|
|
|
}, []);
|
2024-02-01 13:18:16 +00:00
|
|
|
|
2024-04-02 13:09:13 +00:00
|
|
|
if (isLoading) return <LoadingSkeleton />;
|
|
|
|
if (error) return <ApologyPage error={error} />;
|
|
|
|
|
|
|
|
quizSettings ??= data;
|
|
|
|
if (!quizSettings) throw new Error("Quiz data is null");
|
|
|
|
|
2024-02-01 13:18:16 +00:00
|
|
|
return (
|
2024-04-03 12:42:12 +00:00
|
|
|
<QuizViewContext.Provider value={quizViewStore}>
|
|
|
|
<RootContainerWidthContext.Provider value={rootContainerWidth}>
|
2024-04-06 14:01:04 +00:00
|
|
|
<QuizDataContext.Provider value={{ ...quizSettings, quizId, preview, changeFaviconAndTitle }}>
|
2024-04-03 12:42:12 +00:00
|
|
|
<LocalizationProvider dateAdapter={AdapterMoment} adapterLocale="ru" localeText={localeText}>
|
|
|
|
<ThemeProvider theme={lightTheme}>
|
|
|
|
<SnackbarProvider
|
|
|
|
preventDuplicate={true}
|
|
|
|
style={{ backgroundColor: lightTheme.palette.brightPurple.main }}
|
2024-02-16 15:49:23 +00:00
|
|
|
>
|
2024-04-03 12:42:12 +00:00
|
|
|
<CssBaseline />
|
|
|
|
<Box
|
|
|
|
ref={rootContainerRef}
|
|
|
|
sx={{
|
|
|
|
width: "100%",
|
|
|
|
height: "100%",
|
|
|
|
}}
|
2024-02-16 15:49:23 +00:00
|
|
|
>
|
2024-04-03 12:42:12 +00:00
|
|
|
<ErrorBoundary
|
|
|
|
FallbackComponent={ApologyPage}
|
|
|
|
onError={handleComponentError}
|
|
|
|
>
|
|
|
|
<ViewPublicationPage />
|
|
|
|
</ErrorBoundary>
|
|
|
|
</Box>
|
|
|
|
</SnackbarProvider>
|
|
|
|
</ThemeProvider>
|
|
|
|
</LocalizationProvider>
|
|
|
|
</QuizDataContext.Provider>
|
|
|
|
</RootContainerWidthContext.Provider>
|
|
|
|
</QuizViewContext.Provider>
|
2024-02-01 13:18:16 +00:00
|
|
|
);
|
2024-02-02 14:35:02 +00:00
|
|
|
}
|