import { useState, useEffect } from "react"; import { Box, Button, useTheme } from "@mui/material"; import { useQuizViewStore } from "@root/quizView"; import { useCurrentQuiz } from "@root/quizes/hooks"; import { useQuestionsStore } from "@root/questions/store"; import type { AnyTypedQuizQuestion, QuizQuestionBase, } from "../../model/questionTypes/shared"; import { getQuestionByContentId } from "@root/questions/actions"; 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 [disablePreviousButton, setDisablePreviousButton] = useState(false); const [disableNextButton, setDisableNextButton] = useState(false); const quiz = useCurrentQuiz(); const { answers } = useQuizViewStore(); const questions = useQuestionsStore().questions as AnyTypedQuizQuestion[]; const theme = useTheme(); const linear = !questions.find( ({ content }) => content.rule.parentId === "root" ); useEffect(() => { // Логика для аргумента disabled у кнопки "Назад" if (linear) { const questionIndex = questions.findIndex(({ id }) => id === question.id); const previousQuestion = questions[questionIndex - 1]; 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.content.id ); 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 = getQuestionByContentId( question.content.rule.default ); if (nextQuestion?.type) { setDisableNextButton(false); } } }, [question, answers]); const showResult = () => { const resultQuestion = questions.find( ({ type, content }) => type === "result" && content.rule.parentId === question.content.id ); if (quiz?.config.resultInfo.when !== "after" && resultQuestion) { setShowResultForm(true); } else { setShowContactForm(true); } }; 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]) ) ) { readyBeNextQuestion = next; // Ес­ли хоть один эле­мент от­ли­ча­ет­ся, мас­си­вы не рав­ны } return; } if (String(rules[0].answers[i]) === answer?.answer) { readyBeNextQuestion = next; // Ес­ли хоть один эле­мент от­ли­ча­ет­ся, мас­си­вы не рав­ны } } } ); return readyBeNextQuestion; } }; const followPreviousStep = () => { if (linear) { const questionIndex = questions.findIndex(({ id }) => id === question.id); const previousQuestion = questions[questionIndex - 1]; if (previousQuestion) { setCurrentQuestion(previousQuestion); } return; } if (question?.content.rule.parentId !== "root") { const parent = getQuestionByContentId(question?.content.rule.parentId); if (parent?.type) { setCurrentQuestion(parent); } else { enqueueSnackbar("не могу получить предыдущий вопрос"); } } else { enqueueSnackbar("вы находитесь на первом вопросе"); } }; const followNextStep = () => { if (linear) { const questionIndex = questions.findIndex(({ id }) => id === question.id); const nextQuestion = questions[questionIndex + 1]; if (nextQuestion && nextQuestion.type !== "result") { setCurrentQuestion(nextQuestion); } else { showResult(); } return; } const nextQuestionId = getNextQuestionId(); if (nextQuestionId) { const nextQuestion = getQuestionByContentId(nextQuestionId); if (nextQuestion?.type && nextQuestion.type !== "result") { setCurrentQuestion(nextQuestion); return; } else { enqueueSnackbar("не могу получить последующий вопрос"); } } else { const nextQuestion = getQuestionByContentId( question.content.rule.default ); if (nextQuestion?.type && nextQuestion.type !== "result") { setCurrentQuestion(nextQuestion); } else { showResult(); } } }; return ( {/* Шаг {stepNumber} */} {/* */} {/* Из {questions.length} */} ); };