frontPanel/src/pages/ViewPublicationPage/Footer.tsx

148 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-11-30 17:39:57 +00:00
import { useState, useEffect } from "react";
import { Box, Typography, Button, useTheme } from "@mui/material";
2023-12-04 11:47:10 +00:00
import { useQuizViewStore } from "@root/quizView";
2023-11-30 17:39:57 +00:00
2023-12-04 11:47:10 +00:00
import type {
AnyTypedQuizQuestion,
QuizQuestionBase,
} from "../../model/questionTypes/shared";
import { getQuestionByContentId } from "@root/questions/actions";
import { enqueueSnackbar } from "notistack";
2023-11-30 17:39:57 +00:00
type FooterProps = {
setCurrentQuestion: (step: AnyTypedQuizQuestion) => void;
question: QuizQuestionBase;
2023-11-30 17:39:57 +00:00
};
2023-12-04 11:47:10 +00:00
export const Footer = ({ setCurrentQuestion, question }: FooterProps) => {
2023-11-30 17:39:57 +00:00
const [disabledQuestionsId, setDisabledQuestionsId] = useState<Set<string>>(
new Set()
);
const { answers } = useQuizViewStore();
const theme = useTheme();
const followPreviousStep = () => {
if (question?.content.rule.parentId !== "root") {
2023-12-04 11:47:10 +00:00
const parent = getQuestionByContentId(question?.content.rule.parentId);
if (parent) {
2023-12-04 11:47:10 +00:00
setCurrentQuestion(parent);
} else {
2023-12-04 11:47:10 +00:00
enqueueSnackbar("не могу получить предыдущий вопрос");
}
} else {
2023-12-04 11:47:10 +00:00
enqueueSnackbar("вы находитесь на первом вопросе");
}
};
2023-11-30 17:39:57 +00:00
const followNextStep = () => {
2023-12-04 11:47:10 +00:00
if (answers.length) {
let readyBeNextQuestion = "";
2023-11-30 17:39:57 +00:00
question.content.rule.main.forEach(({ next, rules }) => {
2023-11-30 17:39:57 +00:00
2023-12-04 11:47:10 +00:00
let longerArray = Math.max(
rules[0].answers.length,
[answers.at(-1)?.answer].length
);
for (
var i = 0;
i < longerArray;
i++ // Цикл по всем эле­мен­там бОльшего массива
) {
2023-12-04 13:36:30 +00:00
if (rules[0].answers[i] === answers.at(-1)?.answer) {
2023-12-04 11:47:10 +00:00
readyBeNextQuestion = next; // Ес­ли хоть один эле­мент от­ли­ча­ет­ся, мас­си­вы не рав­ны
2023-12-04 13:36:30 +00:00
}
2023-12-04 11:47:10 +00:00
}
});
if (readyBeNextQuestion) {
2023-11-30 17:39:57 +00:00
2023-12-04 11:47:10 +00:00
const nextQuestion = getQuestionByContentId(readyBeNextQuestion);
2023-12-04 11:47:10 +00:00
if (nextQuestion) {
setCurrentQuestion(nextQuestion);
return;
} else {
2023-12-04 11:47:10 +00:00
enqueueSnackbar("не могу получить последующий вопрос");
}
2023-12-04 11:47:10 +00:00
} else {
const nextQuestion = getQuestionByContentId(
question.content.rule.default
);
if (nextQuestion) {
setCurrentQuestion(nextQuestion);
} else {
enqueueSnackbar("не могу получить последующий вопрос (дефолтный)");
}
}
}
2023-11-30 17:39:57 +00:00
};
return (
<Box
sx={{
padding: "15px 0",
borderTop: `1px solid ${theme.palette.grey[400]}`,
}}
>
<Box
sx={{
width: "100%",
maxWidth: "1000px",
padding: "0 10px",
margin: "0 auto",
display: "flex",
alignItems: "center",
gap: "10px",
}}
>
<Box
sx={{
display: "flex",
alignItems: "center",
gap: "10px",
marginRight: "auto",
color: theme.palette.grey1.main,
}}
>
{/* <Typography>Шаг</Typography>
2023-11-30 17:39:57 +00:00
<Typography
sx={{
display: "flex",
justifyContent: "center",
alignItems: "center",
fontWeight: "bold",
borderRadius: "50%",
width: "30px",
height: "30px",
color: "#FFF",
background: theme.palette.brightPurple.main,
}}
>
{stepNumber} */}
{/* </Typography> */}
{/* <Typography>Из</Typography>
2023-11-30 17:39:57 +00:00
<Typography sx={{ fontWeight: "bold" }}>
{questions.length}
</Typography> */}
2023-11-30 17:39:57 +00:00
</Box>
<Button
variant="contained"
sx={{ fontSize: "16px", padding: "10px 15px" }}
onClick={followPreviousStep}
>
Назад
</Button>
<Button
variant="contained"
sx={{ fontSize: "16px", padding: "10px 15px" }}
onClick={followNextStep}
>
Далее
</Button>
</Box>
</Box>
);
};