frontAnswerer/src/pages/ViewPublicationPage/Footer.tsx

367 lines
12 KiB
TypeScript
Raw Normal View History

2023-12-16 14:55:56 +00:00
import { useState, useEffect } from "react";
2023-12-29 00:58:19 +00:00
import { Box, Button, Typography, useMediaQuery, useTheme } from "@mui/material";
2023-12-16 14:55:56 +00:00
2023-12-18 11:56:32 +00:00
import type { AnyTypedQuizQuestion, QuizQuestionBase } from "../../model/questionTypes/shared";
2024-01-19 11:46:17 +00:00
import { useQuestionsStore } from "@stores/quizData/store";
import { getQuestionById } from "@stores/quizData/actions";
import { useQuizViewStore } from "@stores/quizView/store";
2023-12-16 14:55:56 +00:00
import { enqueueSnackbar } from "notistack";
import { NameplateLogoFQ } from "@icons/NameplateLogoFQ";
2023-12-29 00:58:19 +00:00
import { NameplateLogoFQDark } from "@icons/NameplateLogoFQDark";
import { modes } from "../../utils/themes/Publication/themePublication";
import { checkEmptyData } from "./tools/checkEmptyData";
2023-12-16 14:55:56 +00:00
2024-01-10 18:02:37 +00:00
import type { QuizQuestionResult } from "@model/questionTypes/result";
2023-12-16 14:55:56 +00:00
type FooterProps = {
setCurrentQuestion: (step: AnyTypedQuizQuestion) => void;
question: AnyTypedQuizQuestion;
setShowContactForm: (show: boolean) => void;
setShowResultForm: (show: boolean) => void;
};
2023-12-18 11:56:32 +00:00
export const Footer = ({ setCurrentQuestion, question, setShowContactForm, setShowResultForm }: FooterProps) => {
2023-12-16 14:55:56 +00:00
const theme = useTheme();
const { settings, items } = useQuestionsStore();
const { answers } = useQuizViewStore();
2023-12-29 00:58:19 +00:00
const mode = modes;
const [stepNumber, setStepNumber] = useState(1);
const [disablePreviousButton, setDisablePreviousButton] = useState<boolean>(false);
const [disableNextButton, setDisableNextButton] = useState<boolean>(false);
2023-12-18 11:56:32 +00:00
2023-12-29 00:58:19 +00:00
const isMobileMini = useMediaQuery(theme.breakpoints.down(382));
2023-12-18 11:56:32 +00:00
const linear = !items.find(({ content }) => content.rule.parentId === "root");
2023-12-16 14:55:56 +00:00
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 у кнопки "Далее"
2023-12-18 11:56:32 +00:00
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;
}
2023-12-18 11:56:32 +00:00
if ("required" in question.content && question.content.required && !answer) {
2023-12-16 14:55:56 +00:00
setDisableNextButton(true);
return;
}
if (linear) {
return;
}
const nextQuestionId = getNextQuestionId();
if (nextQuestionId) {
setDisableNextButton(false);
} else {
2023-12-18 11:56:32 +00:00
const nextQuestion = getQuestionById(question.content.rule.default);
2023-12-16 14:55:56 +00:00
if (nextQuestion?.type) {
setDisableNextButton(false);
}
}
}, [question, answers]);
2024-01-10 18:02:37 +00:00
const showResult = (nextQuestion: QuizQuestionResult) => {
const isEmpty = checkEmptyData({ resultData: nextQuestion })
console.log("isEmpty", isEmpty)
2023-12-29 00:58:19 +00:00
if (nextQuestion) {
2024-01-10 18:02:37 +00:00
if (nextQuestion && settings?.cfg.resultInfo.showResultForm === "before") {
if (isEmpty) {
setShowContactForm(true); //до+пустая = кидать на ФК
2023-12-29 00:58:19 +00:00
} else {
setShowResultForm(true); //до+заполнена = показать
2023-12-29 00:58:19 +00:00
}
}
2024-01-10 18:02:37 +00:00
if (nextQuestion && settings?.cfg.resultInfo.showResultForm === "after") {
if (isEmpty) {
setShowContactForm(true); //после+пустая
2023-12-29 00:58:19 +00:00
} else {
setShowContactForm(true); //после+заполнена = показать ФК
2023-12-29 00:58:19 +00:00
}
}
}
2023-12-16 14:55:56 +00:00
};
const getNextQuestionId = () => {
console.log("Смотрим какой вопрос будет дальше. Что у нас сегодня вкусненького? Щя покажу от какого вопроса мы ищем следующий шаг")
2023-12-29 00:58:19 +00:00
console.log(question)
console.log("От вот этого /|")
2023-12-29 00:58:19 +00:00
let readyBeNextQuestion = "";
//вопрос обязателен, анализируем ответ и условия ветвления
2023-12-16 14:55:56 +00:00
if (answers.length) {
2023-12-18 11:56:32 +00:00
const answer = answers.find(({ questionId }) => questionId === question.id);
2023-12-16 14:55:56 +00:00
2023-12-18 11:56:32 +00:00
(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-16 14:55:56 +00:00
readyBeNextQuestion = next; // Ес­ли хоть один эле­мент от­ли­ча­ет­ся, мас­си­вы не рав­ны
}
2023-12-18 11:56:32 +00:00
return;
}
if (String(rules[0].answers[i]) === answer?.answer) {
readyBeNextQuestion = next; // Ес­ли хоть один эле­мент от­ли­ча­ет­ся, мас­си­вы не рав­ны
2023-12-16 14:55:56 +00:00
}
}
2023-12-18 11:56:32 +00:00
});
2023-12-16 14:55:56 +00:00
2023-12-29 00:58:19 +00:00
if (readyBeNextQuestion) return readyBeNextQuestion;
2023-12-16 14:55:56 +00:00
}
2023-12-29 00:58:19 +00:00
if (!question.required) {//вопрос не обязателен и не нашли совпадений между ответами и условиями ветвления
console.log("вопрос не обязателен ищем дальше")
const defaultQ = question.content.rule.default
if (defaultQ.length > 1 && defaultQ !== " ") return defaultQ
2023-12-29 00:58:19 +00:00
//Вопросы типа страница, ползунок, своё поле для ввода и дата не могут иметь больше 1 ребёнка. Пользователь не может настроить там дефолт
//Кинуть на ребёнка надо даже если там нет дефолта
if (
(question?.type === "date" ||
question?.type === "text" ||
question?.type === "number" ||
question?.type === "page") && question.content.rule.children.length === 1
) return question.content.rule.children[0]
}
//ничё не нашли, ищем резулт
console.log("ничё не нашли, ищем резулт ")
return items.find(q => {
console.log('q.type === "result"', q.type === "result")
console.log('q.content.rule.parentId', q.content.rule.parentId)
//@ts-ignore
console.log('question.content.id', question.content.id)
//@ts-ignore
return q.type === "result" && q.content.rule.parentId === question.content.id
2023-12-29 00:58:19 +00:00
})?.id
2023-12-16 14:55:56 +00:00
};
2023-12-29 00:58:19 +00:00
2023-12-16 14:55:56 +00:00
const followPreviousStep = () => {
if (linear) {
setStepNumber(q => q - 1)
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) {
setStepNumber(q => q + 1)
const questionIndex = items.findIndex(({ id }) => id === question.id);
const nextQuestion = items[questionIndex + 1];
2023-12-16 14:55:56 +00:00
if (nextQuestion && nextQuestion.type !== "result") {
setCurrentQuestion(nextQuestion);
} else {
//@ts-ignore
showResult(items.find(q => q.content.rule.parentId === "line"));
2023-12-16 14:55:56 +00:00
}
return;
}
const nextQuestionId = getNextQuestionId();
console.log(nextQuestionId)
2023-12-16 14:55:56 +00:00
if (nextQuestionId) {
const nextQuestion = getQuestionById(nextQuestionId);
2023-12-29 00:58:19 +00:00
console.log(nextQuestion)
2023-12-16 14:55:56 +00:00
2023-12-29 00:58:19 +00:00
if (nextQuestion?.type && nextQuestion.type === "result") {
showResult(nextQuestion);
2023-12-16 14:55:56 +00:00
} else {
2023-12-29 00:58:19 +00:00
//@ts-ignore
2023-12-16 14:55:56 +00:00
setCurrentQuestion(nextQuestion);
}
2023-12-29 00:58:19 +00:00
} else {
enqueueSnackbar("не могу получить последующий вопрос");
2023-12-16 14:55:56 +00:00
}
2023-12-29 00:58:19 +00:00
2023-12-16 14:55:56 +00:00
};
return (
<Box
sx={{
2023-12-18 11:56:32 +00:00
position: "relative",
2023-12-16 14:55:56 +00:00
padding: "15px 0",
borderTop: `1px solid ${theme.palette.grey[400]}`,
height: '75px',
2023-12-29 00:58:19 +00:00
display: "flex"
2023-12-16 14:55:56 +00:00
}}
>
<Box
sx={{
width: "100%",
maxWidth: "1000px",
padding: "0 10px",
margin: "0 auto",
display: "flex",
alignItems: "center",
gap: "10px",
}}
>
2023-12-29 00:58:19 +00:00
{/*{mode[settings.cfg.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",
2023-12-29 00:58:19 +00:00
color: theme.palette.text.primary,
}}
>
<Typography>Шаг</Typography>
<Typography
sx={{
display: "flex",
justifyContent: "center",
alignItems: "center",
fontWeight: "bold",
borderRadius: "50%",
width: "30px",
height: "30px",
color: "#FFF",
2023-12-29 00:58:19 +00:00
background: theme.palette.primary.main,
}}
>
{stepNumber}
</Typography>
<Typography>Из</Typography>
<Typography sx={{ fontWeight: "bold" }}>
{items.filter(q => q.type !== "result").length}
</Typography>
</Box>
</>
}
2023-12-16 14:55:56 +00:00
<Box
sx={{
display: "flex",
alignItems: "center",
gap: "10px",
marginRight: "auto",
2023-12-29 00:58:19 +00:00
// color: theme.palette.grey1.main,
2023-12-16 14:55:56 +00:00
}}
>
2023-12-29 00:58:19 +00:00
{/* <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.length}
</Typography> */}
2023-12-16 14:55:56 +00:00
</Box>
<Button
disabled={disablePreviousButton}
variant="contained"
2023-12-29 00:58:19 +00:00
sx={{ fontSize: "16px", padding: "10px 15px",}}
2023-12-16 14:55:56 +00:00
onClick={followPreviousStep}
>
2023-12-29 00:58:19 +00:00
{isMobileMini ? (
"←"
) : (
"← Назад"
)}
2023-12-16 14:55:56 +00:00
</Button>
<Button
disabled={disableNextButton}
variant="contained"
sx={{ fontSize: "16px", padding: "10px 15px" }}
onClick={followNextStep}
>
Далее
</Button>
</Box>
</Box>
);
2023-12-29 00:58:19 +00:00
2023-12-16 14:55:56 +00:00
};