import moment from "moment"; import { useEffect, useLayoutEffect, useState } from "react"; import { Box, Button, IconButton, Typography, useMediaQuery, useTheme, } from "@mui/material"; import { redirect } from "react-router-dom"; import { LocalizationProvider, DatePicker } from "@mui/x-date-pickers"; import { AdapterMoment } from "@mui/x-date-pickers/AdapterMoment"; import HeaderFull from "@ui_kit/Header/HeaderFull"; import SectionWrapper from "@ui_kit/SectionWrapper"; import { General } from "./General"; import { AnswersStatistics } from "./Answers/AnswersStatistics"; import { Devices } from "./Devices"; import { setQuizes } from "@root/quizes/actions"; import { useQuizStore } from "@root/quizes/store"; import { useAnalytics } from "@utils/hooks/useAnalytics"; import { quizApi } from "@api/quiz"; import CalendarIcon from "@icons/CalendarIcon"; import { ReactComponent as ResetIcon } from "@icons/Analytics/reset.svg"; import type { Moment } from "moment"; import type { ReactNode } from "react"; import type { Quiz } from "@model/quiz/quiz"; export default function Analytics() { const { quizes, editQuizId } = useQuizStore(); const [quiz, setQuiz] = useState({} as Quiz); const [isOpen, setOpen] = useState(false); const [isOpenEnd, setOpenEnd] = useState(false); const [from, setFrom] = useState(null); const [to, setTo] = useState(moment().add(1, "days")); const theme = useTheme(); const isMobile = useMediaQuery(theme.breakpoints.down(600)); const { devices, general, questions } = useAnalytics({ ready: Boolean(Object.keys(quiz).length), quizId: editQuizId?.toString() || "", from, to, }); const resetTime = () => { setFrom(moment(0)); setTo(moment(Date.now())); }; useEffect(() => { if (quizes.length > 0) { const quiz = quizes.find((q) => q.backendId === editQuizId); if (quiz === undefined) throw new Error("Не удалось получить квиз"); setQuiz(quiz); setFrom(moment(new Date(quiz.created_at))); } }, [quizes]); useEffect(() => { const getData = async (): Promise => { try { if (editQuizId !== null) { const gottenQuizes = await quizApi.getList(); setQuizes(gottenQuizes); } } catch (error) { console.error("Не удалось получить квизы", error); } }; getData(); }, []); useLayoutEffect(() => { if (editQuizId === undefined) redirect("/list"); }, [editQuizId]); const handleClose = () => { setOpen(false); }; const handleOpen = () => { setOpen(true); }; const onAdornmentClick = () => { setOpen((old) => !old); if (isOpenEnd) { handleCloseEnd(); } }; const handleCloseEnd = () => { setOpenEnd(false); }; const handleOpenEnd = () => { setOpenEnd(true); }; const onAdornmentClickEnd = () => { setOpenEnd((old) => !old); if (isOpen) { handleClose(); } }; return ( Аналитика Дата начала ) as ReactNode, }, }, }} value={from} onChange={setFrom} /> Дата окончания ), }, }, }} value={to} onChange={setTo} /> 0} /> ); }