frontPanel/src/pages/ViewPublicationPage/Footer.tsx

331 lines
10 KiB
TypeScript
Raw Normal View History

2023-11-30 17:39:57 +00:00
import { useState, useEffect } from "react";
import { Box, Button, Typography, useTheme } from "@mui/material";
2023-11-30 17:39:57 +00:00
2023-12-04 11:47:10 +00:00
import { useQuizViewStore } from "@root/quizView";
2023-12-15 12:12:36 +00:00
import { useCurrentQuiz } from "@root/quizes/hooks";
2023-12-15 14:12:06 +00:00
import { useQuestionsStore } from "@root/questions/store";
2023-11-30 17:39:57 +00:00
import type { AnyTypedQuizQuestion, QuizQuestionBase } from "../../model/questionTypes/shared";
import { getQuestionByContentId } from "@root/questions/actions";
import { enqueueSnackbar } from "notistack";
import { NameplateLogoFQ } from "@icons/NameplateLogoFQ";
import {NameplateLogoFQDark} from "@icons/NameplateLogoFQDark";
import {modes} from "../../utils/themes/Publication/themePublication";
import { checkEmptyData } from "../ResultPage/cards/ResultCard";
2023-11-30 17:39:57 +00:00
type FooterProps = {
setCurrentQuestion: (step: AnyTypedQuizQuestion) => void;
2023-12-13 18:19:12 +00:00
question: AnyTypedQuizQuestion;
2023-12-15 12:12:36 +00:00
setShowContactForm: (show: boolean) => void;
setShowResultForm: (show: boolean) => void;
2023-11-30 17:39:57 +00:00
};
export const Footer = ({ setCurrentQuestion, question, setShowContactForm, setShowResultForm }: FooterProps) => {
const [disablePreviousButton, setDisablePreviousButton] = useState<boolean>(false);
const [disableNextButton, setDisableNextButton] = useState<boolean>(false);
const [stepNumber, setStepNumber] = useState(1);
2023-12-15 12:12:36 +00:00
const quiz = useCurrentQuiz();
const mode = modes;
2023-11-30 17:39:57 +00:00
const { answers } = useQuizViewStore();
2023-12-15 14:12:06 +00:00
const questions = useQuestionsStore().questions as AnyTypedQuizQuestion[];
2023-11-30 17:39:57 +00:00
const theme = useTheme();
const linear = !questions.find(({ content }) => content.rule.parentId === "root");
2023-11-30 17:39:57 +00:00
useEffect(() => {
// Логика для аргумента disabled у кнопки "Назад"
2023-12-06 12:09:25 +00:00
if (linear) {
const questionIndex = questions.findIndex(({ id }) => id === question.id);
const previousQuestion = questions[questionIndex - 1];
if (previousQuestion) {
setDisablePreviousButton(false);
} else {
setDisablePreviousButton(true);
}
} else {
2023-12-06 12:09:25 +00:00
if (question?.content.rule.parentId === "root") {
setDisablePreviousButton(true);
} else {
setDisablePreviousButton(false);
}
}
// Логика для аргумента disabled у кнопки "Далее"
const answer = answers.find(({ questionId }) => questionId === question.content.id);
2023-12-06 15:30:46 +00:00
2023-12-13 18:19:12 +00:00
if ("required" in question.content && question.content.required && answer) {
setDisableNextButton(false);
2023-12-06 15:30:46 +00:00
return;
}
if ("required" in question.content && question.content.required && !answer) {
2023-12-13 18:19:12 +00:00
setDisableNextButton(true);
2023-12-13 12:15:44 +00:00
return;
}
2023-12-06 12:09:25 +00:00
if (linear) {
2023-12-15 12:12:36 +00:00
return;
}
2023-12-06 12:09:25 +00:00
2023-12-15 12:12:36 +00:00
const nextQuestionId = getNextQuestionId();
if (nextQuestionId) {
setDisableNextButton(false);
2023-12-06 12:09:25 +00:00
} else {
const nextQuestion = getQuestionByContentId(question.content.rule.default);
2023-12-15 12:12:36 +00:00
if (nextQuestion?.type) {
2023-12-06 12:09:25 +00:00
setDisableNextButton(false);
}
}
2023-12-06 15:30:46 +00:00
}, [question, answers]);
const showResult = (nextQuestion) => {
2023-12-15 14:12:06 +00:00
if (nextQuestion && quiz?.config.resultInfo.when === "email") {
2023-12-16 09:36:35 +00:00
setShowContactForm(true);
return;
}
const isEmpty = checkEmptyData({ resultData: nextQuestion })
console.log("пустой результат? ", isEmpty)
if (nextQuestion) {
if (nextQuestion && quiz?.config.resultInfo.when === "before") {
if (isEmpty) {
setShowContactForm(true); //до+пустая = кидать на ФК
console.log("до+пустая = кидать на ФК")
} else {
setShowResultForm(true); //до+заполнена = показать
console.log("до+заполнена = показать")
}
}
if (nextQuestion && quiz?.config.resultInfo.when === "after") {
if (isEmpty) {
setShowContactForm(true); //после+пустая
console.log("после+пустая")
} else {
setShowContactForm(true); //после+заполнена = показать ФК
console.log("после+заполнена = показать")
}
}
2023-12-15 12:12:36 +00:00
}
2023-12-15 12:12:36 +00:00
};
const getNextQuestionId = () => {
if (answers.length) {
const answer = answers.find(({ questionId }) => questionId === question.content.id);
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]))) {
2023-12-14 14:05:19 +00:00
readyBeNextQuestion = next; // Ес­ли хоть один эле­мент от­ли­ча­ет­ся, мас­си­вы не рав­ны
}
return;
}
if (String(rules[0].answers[i]) === answer?.answer) {
readyBeNextQuestion = next; // Ес­ли хоть один эле­мент от­ли­ча­ет­ся, мас­си­вы не рав­ны
}
}
});
return readyBeNextQuestion;
}
};
const followPreviousStep = () => {
2023-12-06 12:09:25 +00:00
if (linear) {
setStepNumber(q => q - 1)
2023-12-06 12:09:25 +00:00
const questionIndex = questions.findIndex(({ id }) => id === question.id);
const previousQuestion = questions[questionIndex - 1];
if (previousQuestion) {
setCurrentQuestion(previousQuestion);
}
return;
}
if (question?.content.rule.parentId !== "root") {
2023-12-04 11:47:10 +00:00
const parent = getQuestionByContentId(question?.content.rule.parentId);
if (parent?.type) {
2023-12-04 11:47:10 +00:00
setCurrentQuestion(parent);
} else {
2023-12-04 11:47:10 +00:00
enqueueSnackbar("не могу получить предыдущий вопрос");
}
2023-12-05 10:33:01 +00:00
} else {
enqueueSnackbar("вы находитесь на первом вопросе");
}
2023-12-05 10:33:01 +00:00
};
2023-12-05 10:33:01 +00:00
const followNextStep = () => {
2023-12-06 12:09:25 +00:00
if (linear) {
setStepNumber(q => q + 1)
2023-12-06 12:09:25 +00:00
const questionIndex = questions.findIndex(({ id }) => id === question.id);
const nextQuestion = questions[questionIndex + 1];
2023-12-15 14:12:06 +00:00
if (nextQuestion && nextQuestion.type !== "result") {
2023-12-06 12:09:25 +00:00
setCurrentQuestion(nextQuestion);
2023-12-15 12:12:36 +00:00
} else {
showResult(nextQuestion);
2023-12-06 12:09:25 +00:00
}
return;
}
const nextQuestionId = getNextQuestionId();
if (nextQuestionId) {
const nextQuestion = getQuestionByContentId(nextQuestionId);
2023-12-15 14:12:06 +00:00
if (nextQuestion?.type && nextQuestion.type !== "result") {
setCurrentQuestion(nextQuestion);
return;
} else {
enqueueSnackbar("не могу получить последующий вопрос");
}
} else {
const nextQuestion = getQuestionByContentId(question.content.rule.default);
2023-12-15 14:12:06 +00:00
if (nextQuestion?.type && nextQuestion.type !== "result") {
setCurrentQuestion(nextQuestion);
} else {
showResult(nextQuestion);
}
}
};
2023-11-30 17:39:57 +00:00
return (
<Box
sx={{
position: "relative",
2023-11-30 17:39:57 +00:00
padding: "15px 0",
borderTop: `1px solid ${theme.palette.grey[400]}`,
height: '75px',
display: "flex"
2023-11-30 17:39:57 +00:00
}}
>
<Box
sx={{
width: "100%",
maxWidth: "1000px",
padding: "0 10px",
margin: "0 auto",
display: "flex",
alignItems: "center",
gap: "10px",
}}
>
{mode[quiz.config.theme] ? (
<NameplateLogoFQ style={{ fontSize: "34px", width:"200px", height:"auto" }} />
):(
<NameplateLogoFQDark style={{ fontSize: "34px", width:"200px", height:"auto" }} />
)
}
{linear &&
<>
<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" }}>
{questions.filter(q => q.type !== "result").length}
</Typography>
</Box>
</>
}
2023-11-30 17:39:57 +00:00
<Box
sx={{
display: "flex",
alignItems: "center",
gap: "10px",
marginRight: "auto",
// color: theme.palette.grey1.main,
2023-11-30 17:39:57 +00:00
}}
>
{/* <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
disabled={disablePreviousButton}
2023-11-30 17:39:57 +00:00
variant="contained"
sx={{ fontSize: "16px", padding: "10px 15px", "& :disabled": {color: `{hsl((theme.palette.primary.main) 30%)}`} }}
2023-11-30 17:39:57 +00:00
onClick={followPreviousStep}
>
Назад
</Button>
<Button
disabled={disableNextButton}
2023-11-30 17:39:57 +00:00
variant="contained"
sx={{ fontSize: "16px", padding: "10px 15px" }}
onClick={followNextStep}
>
Далее
</Button>
</Box>
</Box>
);
};