2023-12-16 14:55:56 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2023-12-29 00:58:19 +00:00
|
|
|
import { Box, Skeleton, ThemeProvider } from "@mui/material";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
import { StartPageViewPublication } from "./StartPageViewPublication";
|
|
|
|
import { Question } from "./Question";
|
2023-12-17 13:22:21 +00:00
|
|
|
import { ApologyPage } from "./ApologyPage"
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2024-01-19 11:46:17 +00:00
|
|
|
import { useQuestionsStore } from "@stores/quizData/store"
|
2023-12-17 21:28:57 +00:00
|
|
|
import { getData } from "@api/quizRelase"
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2023-12-17 13:22:21 +00:00
|
|
|
import type { AnyTypedQuizQuestion } from "@model/questionTypes/shared";
|
2023-12-17 18:15:59 +00:00
|
|
|
import { useGetSettings } from "../../utils/hooks/useGetSettings";
|
2023-12-29 00:58:19 +00:00
|
|
|
import { themesPublication } from "../../utils/themes/Publication/themePublication";
|
|
|
|
import { replaceSpacesToEmptyLines } from "./tools/replaceSpacesToEmptyLines";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2023-12-17 22:20:52 +00:00
|
|
|
|
|
|
|
const QID =
|
2023-12-29 00:58:19 +00:00
|
|
|
process.env.NODE_ENV === "production" ?
|
|
|
|
window.location.pathname.replace(/\//g, '')
|
|
|
|
:
|
2024-01-10 18:02:37 +00:00
|
|
|
"0bed8483-3016-4bca-b8e0-a72c3146f18b"
|
2023-12-17 22:20:52 +00:00
|
|
|
|
|
|
|
|
2023-12-17 13:22:21 +00:00
|
|
|
export const ViewPage = () => {
|
|
|
|
const { settings, cnt, items } = useQuestionsStore()
|
2023-12-24 11:52:09 +00:00
|
|
|
console.log("КВИЗ ", settings)
|
2023-12-29 00:58:19 +00:00
|
|
|
console.log("ВОПРОСЫ ", items)
|
|
|
|
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
const [visualStartPage, setVisualStartPage] = useState<boolean>();
|
2023-12-17 21:28:57 +00:00
|
|
|
const [errormessage, setErrormessage] = useState<string>("");
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-12-17 18:15:59 +00:00
|
|
|
async function get() {
|
2023-12-17 21:28:57 +00:00
|
|
|
try {
|
2024-01-15 17:41:15 +00:00
|
|
|
let data = await getData(QID)
|
|
|
|
|
|
|
|
console.log(data)
|
|
|
|
//@ts-ignore
|
2023-12-17 18:15:59 +00:00
|
|
|
const settings = data.settings
|
2024-01-15 17:41:15 +00:00
|
|
|
|
|
|
|
console.log(data)
|
|
|
|
//@ts-ignore
|
|
|
|
data.settings = {
|
2023-12-29 00:58:19 +00:00
|
|
|
//@ts-ignore
|
2024-01-15 17:41:15 +00:00
|
|
|
qid: QID,
|
|
|
|
fp: settings.fp,
|
|
|
|
rep: settings.rep,
|
|
|
|
name: settings.name,
|
2023-12-29 00:58:19 +00:00
|
|
|
//@ts-ignore
|
2024-01-15 17:41:15 +00:00
|
|
|
cfg: JSON.parse(data?.settings.cfg),
|
|
|
|
lim: settings.lim,
|
|
|
|
due: settings.due,
|
|
|
|
delay: settings.delay,
|
|
|
|
pausable: settings.pausable
|
2023-12-17 18:15:59 +00:00
|
|
|
}
|
2024-01-15 17:41:15 +00:00
|
|
|
console.log(data)
|
|
|
|
//@ts-ignore
|
|
|
|
data.items = data.items.map((item) => {
|
|
|
|
const content = JSON.parse(item.c)
|
|
|
|
return {
|
|
|
|
description: item.desc,
|
|
|
|
id: item.id,
|
|
|
|
page: item.p,
|
|
|
|
required: item.req,
|
|
|
|
title: item.title,
|
|
|
|
type: item.typ,
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
console.log(data)
|
|
|
|
console.log(JSON.stringify({data: data}).replaceAll(/\\\" \\\"/g, '""').replaceAll(/\" \"/g, '""'))
|
|
|
|
console.log(JSON.parse(JSON.stringify({data: data}).replaceAll(/\\\" \\\"/g, '""').replaceAll(/\" \"/g, '""')).data)
|
|
|
|
|
|
|
|
data = replaceSpacesToEmptyLines(data)
|
|
|
|
|
|
|
|
useQuestionsStore.setState(JSON.parse(JSON.stringify({data: data}).replaceAll(/\\\" \\\"/g, '""').replaceAll(/\" \"/g, '""')).data)
|
2023-12-17 21:28:57 +00:00
|
|
|
|
|
|
|
} catch (e) {
|
2023-12-29 00:58:19 +00:00
|
|
|
|
2023-12-17 21:28:57 +00:00
|
|
|
//@ts-ignore
|
|
|
|
if (e?.response?.status === 423) setErrormessage("квиз не активирован")
|
|
|
|
}
|
2023-12-29 00:58:19 +00:00
|
|
|
}
|
|
|
|
get()
|
|
|
|
}, [])
|
2023-12-17 18:15:59 +00:00
|
|
|
|
|
|
|
|
2023-12-21 19:22:06 +00:00
|
|
|
useEffect(() => {//установка фавиконки
|
2023-12-17 18:15:59 +00:00
|
|
|
if (Object.values(settings).length > 0) {
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2023-12-29 00:58:19 +00:00
|
|
|
const link = document.querySelector('link[rel="icon"]');
|
|
|
|
if (link && settings?.cfg.startpage.favIcon) {
|
|
|
|
link.setAttribute("href", settings?.cfg.startpage.favIcon);
|
|
|
|
}
|
2023-12-17 13:22:21 +00:00
|
|
|
|
2023-12-29 00:58:19 +00:00
|
|
|
setVisualStartPage(!settings?.cfg.noStartPage);
|
|
|
|
}
|
2023-12-17 13:22:21 +00:00
|
|
|
}, [settings]);
|
|
|
|
|
|
|
|
|
2023-12-16 14:55:56 +00:00
|
|
|
const filteredQuestions = (
|
2023-12-17 13:22:21 +00:00
|
|
|
items.filter(({ type }) => type) as AnyTypedQuizQuestion[]
|
2023-12-16 14:55:56 +00:00
|
|
|
).sort((previousItem, item) => previousItem.page - item.page);
|
|
|
|
|
2023-12-17 13:22:21 +00:00
|
|
|
|
2023-12-17 21:28:57 +00:00
|
|
|
if (errormessage) return <ApologyPage message={errormessage} />
|
2023-12-17 13:22:21 +00:00
|
|
|
if (visualStartPage === undefined) return <Skeleton sx={{ bgcolor: 'grey', width: "100vw", height: "100vh" }} variant="rectangular" />;
|
2023-12-19 21:59:35 +00:00
|
|
|
if (cnt === 0 || (cnt === 1 && items[0].type === "result")) return <ApologyPage message="Нет созданных вопросов" />
|
2023-12-16 14:55:56 +00:00
|
|
|
return (
|
2023-12-29 00:58:19 +00:00
|
|
|
<ThemeProvider theme={themesPublication?.[settings?.cfg.theme || "StandardTheme"]}>
|
|
|
|
<Box>
|
|
|
|
{
|
|
|
|
visualStartPage ?
|
|
|
|
<StartPageViewPublication setVisualStartPage={setVisualStartPage} />
|
|
|
|
:
|
|
|
|
<Question questions={filteredQuestions} />
|
|
|
|
}
|
|
|
|
</Box>
|
|
|
|
</ThemeProvider>
|
|
|
|
|
2023-12-16 14:55:56 +00:00
|
|
|
);
|
|
|
|
};
|