2023-12-16 14:55:56 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2023-12-17 13:22:21 +00:00
|
|
|
import { Box, Skeleton } 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
|
|
|
|
2023-12-17 13:22:21 +00:00
|
|
|
import { useQuestionsStore } from "@root/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-16 14:55:56 +00:00
|
|
|
|
2023-12-17 22:20:52 +00:00
|
|
|
|
|
|
|
const QID =
|
|
|
|
process.env.NODE_ENV === "production" ?
|
2023-12-18 00:23:03 +00:00
|
|
|
window.location.pathname.replace(/\//g, '')
|
2023-12-17 22:20:52 +00:00
|
|
|
:
|
2023-12-24 07:57:30 +00:00
|
|
|
"e883eccc-78b0-47bb-98b9-66d2cb0cf51d"
|
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-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 {
|
2023-12-17 22:20:52 +00:00
|
|
|
const data = await getData(QID)
|
2023-12-17 18:15:59 +00:00
|
|
|
//@ts-ignore
|
|
|
|
const settings = data.settings
|
|
|
|
console.log(data)
|
2023-12-19 21:59:35 +00:00
|
|
|
console.log(window.location.pathname.replace(/\//g, ''))
|
2023-12-17 18:15:59 +00:00
|
|
|
const parseData = {
|
|
|
|
settings: {
|
2023-12-19 21:59:35 +00:00
|
|
|
//@ts-ignore
|
2023-12-23 18:03:34 +00:00
|
|
|
qid: QID,
|
2023-12-17 18:15:59 +00:00
|
|
|
fp: settings.fp,
|
|
|
|
rep: settings.rep,
|
|
|
|
name: settings.name,
|
|
|
|
cfg: JSON.parse(settings?.cfg),
|
|
|
|
lim: settings.lim,
|
|
|
|
due: settings.due,
|
|
|
|
delay: settings.delay,
|
|
|
|
pausable: settings.pausable
|
|
|
|
},
|
|
|
|
//@ts-ignore
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
//@ts-ignore
|
|
|
|
cnt: data.cnt
|
|
|
|
}
|
|
|
|
console.log(parseData)
|
|
|
|
useQuestionsStore.setState(parseData)
|
2023-12-17 21:28:57 +00:00
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
//@ts-ignore
|
|
|
|
if (e?.response?.status === 423) setErrormessage("квиз не активирован")
|
|
|
|
}
|
2023-12-17 18:15:59 +00:00
|
|
|
}
|
|
|
|
get()
|
|
|
|
},[])
|
|
|
|
|
|
|
|
|
2023-12-21 19:22:06 +00:00
|
|
|
useEffect(() => {//установка фавиконки
|
2023-12-17 18:15:59 +00:00
|
|
|
if (Object.values(settings).length > 0) {
|
|
|
|
console.log(settings)
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2023-12-17 13:22:21 +00:00
|
|
|
const link = document.querySelector('link[rel="icon"]');
|
2023-12-17 18:15:59 +00:00
|
|
|
if (link && settings?.cfg.startpage.favIcon) {
|
|
|
|
link.setAttribute("href", settings?.cfg.startpage.favIcon);
|
2023-12-16 14:55:56 +00:00
|
|
|
}
|
2023-12-17 13:22:21 +00:00
|
|
|
|
2023-12-17 18:15:59 +00:00
|
|
|
setVisualStartPage(!settings?.cfg.noStartPage);}
|
2023-12-17 13:22:21 +00:00
|
|
|
}, [settings]);
|
|
|
|
|
|
|
|
|
2023-12-17 18:15:59 +00:00
|
|
|
console.log(items)
|
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 18:15:59 +00:00
|
|
|
console.log(filteredQuestions)
|
2023-12-16 14:55:56 +00:00
|
|
|
|
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 (
|
|
|
|
<Box>
|
2023-12-17 13:22:21 +00:00
|
|
|
{
|
|
|
|
visualStartPage ?
|
|
|
|
<StartPageViewPublication setVisualStartPage={setVisualStartPage} />
|
|
|
|
:
|
|
|
|
<Question questions={filteredQuestions} />
|
|
|
|
}
|
2023-12-16 14:55:56 +00:00
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|