frontAnswerer/src/pages/ViewPublicationPage/Footer.tsx

284 lines
7.6 KiB
TypeScript
Raw Normal View History

2023-12-16 14:55:56 +00:00
import { useState, useEffect } from "react";
import { Box, Button, useTheme } from "@mui/material";
import type {
AnyTypedQuizQuestion,
QuizQuestionBase,
} from "../../model/questionTypes/shared";
import { useQuestionsStore } from "@root/quizData/store";
import { getQuestionById } from "@root/quizData/actions";
import { useQuizViewStore } from "@root/quizView/store";
2023-12-16 14:55:56 +00:00
import { enqueueSnackbar } from "notistack";
type FooterProps = {
setCurrentQuestion: (step: AnyTypedQuizQuestion) => void;
question: AnyTypedQuizQuestion;
setShowContactForm: (show: boolean) => void;
setShowResultForm: (show: boolean) => void;
};
export const Footer = ({
setCurrentQuestion,
question,
setShowContactForm,
setShowResultForm,
}: FooterProps) => {
const theme = useTheme();
const { settings, items } = useQuestionsStore();
const { answers } = useQuizViewStore();
const [disablePreviousButton, setDisablePreviousButton] = useState<boolean>(false);
const [disableNextButton, setDisableNextButton] = useState<boolean>(false);
const linear = !items.find(
2023-12-16 14:55:56 +00:00
({ content }) => content.rule.parentId === "root"
);
useEffect(() => {
// Логика для аргумента disabled у кнопки "Назад"
if (linear) {
const questionIndex = items.findIndex(({ id }) => id === question.id);
2023-12-16 14:55:56 +00:00
const previousQuestion = items[questionIndex - 1];
2023-12-16 14:55:56 +00:00
if (previousQuestion) {
setDisablePreviousButton(false);
} else {
setDisablePreviousButton(true);
}
} else {
if (question?.content.rule.parentId === "root") {
setDisablePreviousButton(true);
} else {
setDisablePreviousButton(false);
}
}
// Логика для аргумента disabled у кнопки "Далее"
const answer = answers.find(
({ questionId }) => questionId === question.id
2023-12-16 14:55:56 +00:00
);
if ("required" in question.content && question.content.required && answer) {
setDisableNextButton(false);
return;
}
if (
"required" in question.content &&
question.content.required &&
!answer
) {
setDisableNextButton(true);
return;
}
if (linear) {
return;
}
const nextQuestionId = getNextQuestionId();
if (nextQuestionId) {
setDisableNextButton(false);
} else {
const nextQuestion = getQuestionById(
2023-12-16 14:55:56 +00:00
question.content.rule.default
);
if (nextQuestion?.type) {
setDisableNextButton(false);
}
}
}, [question, answers]);
const showResult = (nextQuestion:any) => {
console.log(nextQuestion)
2023-12-16 14:55:56 +00:00
if (nextQuestion && settings?.cfg.resultInfo.when === "before") {
2023-12-16 14:55:56 +00:00
setShowResultForm(true);
} else {
setShowContactForm(true);
}
};
const getNextQuestionId = () => {
if (answers.length) {
const answer = answers.find(
({ questionId }) => questionId === question.id
2023-12-16 14:55:56 +00:00
);
let readyBeNextQuestion = "";
(question as QuizQuestionBase).content.rule.main.forEach(
({ next, rules }) => {
let longerArray = Math.max(
rules[0].answers.length,
answer?.answer && Array.isArray(answer?.answer)
? answer?.answer.length
: [answer?.answer].length
);
for (
var i = 0;
i < longerArray;
i++ // Цикл по всем эле­мен­там бОльшего массива
) {
if (Array.isArray(answer?.answer)) {
if (
answer?.answer.find((item) =>
String(item === rules[0].answers[i])
)
) {
readyBeNextQuestion = next; // Ес­ли хоть один эле­мент от­ли­ча­ет­ся, мас­си­вы не рав­ны
}
return;
}
if (String(rules[0].answers[i]) === answer?.answer) {
readyBeNextQuestion = next; // Ес­ли хоть один эле­мент от­ли­ча­ет­ся, мас­си­вы не рав­ны
}
}
}
);
return readyBeNextQuestion;
}
};
const followPreviousStep = () => {
if (linear) {
const questionIndex = items.findIndex(({ id }) => id === question.id);
2023-12-16 14:55:56 +00:00
const previousQuestion = items[questionIndex - 1];
2023-12-16 14:55:56 +00:00
if (previousQuestion) {
setCurrentQuestion(previousQuestion);
}
return;
}
if (question?.content.rule.parentId !== "root") {
const parent = getQuestionById(question?.content.rule.parentId);
2023-12-16 14:55:56 +00:00
if (parent?.type) {
setCurrentQuestion(parent);
} else {
enqueueSnackbar("не могу получить предыдущий вопрос");
}
} else {
enqueueSnackbar("вы находитесь на первом вопросе");
}
};
const followNextStep = () => {
if (linear) {
const questionIndex = items.findIndex(({ id }) => id === question.id);
const nextQuestion = items[questionIndex + 1];
2023-12-16 14:55:56 +00:00
console.log(nextQuestion)
2023-12-16 14:55:56 +00:00
if (nextQuestion && nextQuestion.type !== "result") {
setCurrentQuestion(nextQuestion);
} else {
showResult(nextQuestion);
2023-12-16 14:55:56 +00:00
}
return;
}
const nextQuestionId = getNextQuestionId();
if (nextQuestionId) {
const nextQuestion = getQuestionById(nextQuestionId);
2023-12-16 14:55:56 +00:00
if (nextQuestion?.type && nextQuestion.type !== "result") {
setCurrentQuestion(nextQuestion);
return;
} else {
enqueueSnackbar("не могу получить последующий вопрос");
}
} else {
const nextQuestion = getQuestionById(
2023-12-16 14:55:56 +00:00
question.content.rule.default
);
if (nextQuestion?.type && nextQuestion.type !== "result") {
setCurrentQuestion(nextQuestion);
} else {
showResult(undefined);
2023-12-16 14:55:56 +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>
<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>
<Typography sx={{ fontWeight: "bold" }}>
{items.length}
2023-12-16 14:55:56 +00:00
</Typography> */}
</Box>
<Button
disabled={disablePreviousButton}
variant="contained"
sx={{ fontSize: "16px", padding: "10px 15px" }}
onClick={followPreviousStep}
>
Назад
</Button>
<Button
disabled={disableNextButton}
variant="contained"
sx={{ fontSize: "16px", padding: "10px 15px" }}
onClick={followNextStep}
>
Далее
</Button>
</Box>
</Box>
);
};