frontPanel/src/pages/createQuize/MyQuizzesFull.tsx

102 lines
3.4 KiB
TypeScript
Raw Normal View History

2023-11-13 18:04:51 +00:00
import { quizApi } from "@api/quiz";
import { devlog } from "@frontend/kitui";
2023-10-18 12:46:59 +00:00
import {
2023-11-13 18:04:51 +00:00
Box,
Button,
SxProps,
Theme,
Typography,
useMediaQuery,
useTheme,
2023-10-18 12:46:59 +00:00
} from "@mui/material";
import SectionWrapper from "@ui_kit/SectionWrapper";
2023-11-13 18:04:51 +00:00
import { isAxiosError } from "axios";
import { enqueueSnackbar } from "notistack";
import React from "react";
2023-10-18 12:46:59 +00:00
import { useNavigate } from "react-router-dom";
2023-11-13 18:04:51 +00:00
import useSWR from "swr";
import ComplexNavText from "./ComplexNavText";
import FirstQuiz from "./FirstQuiz";
import QuizCard from "./QuizCard";
import { setQuizes, createQuiz } from "@root/quizes/actions";
2023-11-27 23:07:24 +00:00
import { useQuizStore } from "@root/quizes/store";
2023-11-13 18:04:51 +00:00
interface Props {
2023-11-13 18:04:51 +00:00
outerContainerSx?: SxProps<Theme>;
children?: React.ReactNode;
}
2022-12-09 11:48:15 +00:00
2023-10-18 12:46:59 +00:00
export default function MyQuizzesFull({
2023-11-13 18:04:51 +00:00
outerContainerSx: sx,
children,
2023-10-18 12:46:59 +00:00
}: Props) {
2023-11-13 18:04:51 +00:00
useSWR("quizes", () => quizApi.getList(), {
onSuccess: setQuizes,
onError: error => {
const message = isAxiosError<string>(error) ? (error.response?.data ?? "") : "";
2023-11-14 16:43:21 +00:00
devlog("Error getting quiz list", error);
2023-11-13 18:04:51 +00:00
enqueueSnackbar(`Не удалось получить квизы. ${message}`);
},
});
2023-11-27 23:07:24 +00:00
const quizArray = useQuizStore(state => state.quizes);
2023-11-13 18:04:51 +00:00
const navigate = useNavigate();
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down(500));
2023-11-13 18:04:51 +00:00
return (
<>
{quizArray.length === 0 ? (
<FirstQuiz />
) : (
<SectionWrapper maxWidth="lg">
<ComplexNavText text1="Кабинет квизов" />
<Box
sx={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
mt: "20px",
mb: "30px",
}}
>
<Typography variant="h4">Мои квизы</Typography>
<Button
variant="contained"
sx={{
padding: isMobile ? "10px" : "10px 47px",
minWidth: "44px",
}}
onClick={() => createQuiz(navigate)}
2023-11-27 23:07:24 +00:00
data-cy="create-quiz"
2023-11-13 18:04:51 +00:00
>
{isMobile ? "+" : "Создать +"}
</Button>
</Box>
<Box
sx={{
py: "10px",
display: "flex",
flexWrap: "wrap",
gap: "40px",
mb: "60px",
}}
>
{quizArray.map(quiz => (
<QuizCard
key={quiz.id}
quiz={quiz}
openCount={0}
applicationCount={0}
conversionPercent={0}
/>
))}
</Box>
{children}
</SectionWrapper>
)}
</>
);
}