From ac31202af6f5b7caafa376c228a1da43648bfb3e Mon Sep 17 00:00:00 2001 From: IlyaDoronin Date: Tue, 5 Dec 2023 13:24:31 +0300 Subject: [PATCH] feat: View footer buttons disabled logic --- src/pages/ViewPublicationPage/Footer.tsx | 86 ++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/src/pages/ViewPublicationPage/Footer.tsx b/src/pages/ViewPublicationPage/Footer.tsx index 5b46c9c1..4a7c1e70 100644 --- a/src/pages/ViewPublicationPage/Footer.tsx +++ b/src/pages/ViewPublicationPage/Footer.tsx @@ -19,19 +19,95 @@ export const Footer = ({ setCurrentQuestion, question }: FooterProps) => { const [disabledQuestionsId, setDisabledQuestionsId] = useState>( new Set() ); + const [disablePreviousButton, setDisablePreviousButton] = + useState(false); + const [disableNextButton, setDisableNextButton] = useState(false); const { answers } = useQuizViewStore(); const theme = useTheme(); + useEffect(() => { + // Логика для аргумента disabled у кнопки "Назад" + if (question?.content.rule.parentId === "root") { + setDisablePreviousButton(true); + } else { + setDisablePreviousButton(false); + } + + // Логика для аргумента disabled у кнопки "Далее" + const nextQuestionId = getNextQuestionId(); + if (nextQuestionId) { + setDisableNextButton(false); + } else { + const nextQuestion = getQuestionByContentId( + question.content.rule.default + ); + if (nextQuestion?.type) { + setDisableNextButton(false); + } else { + setDisableNextButton(true); + } + } + }, [question]); + + const getNextQuestionId = () => { + if (answers.length) { + const answer = answers.find( + ({ questionId }) => questionId === question.content.id + ); + + let readyBeNextQuestion = ""; + + question.content.rule.main.forEach(({ next, rules }) => { + let longerArray = Math.max( + rules[0].answers.length, + [answer?.answer].length + ); + + for ( + var i = 0; + i < longerArray; + i++ // Цикл по всем эле­мен­там бОльшего массива + ) { + if (rules[0].answers[i] === answer?.answer) { + readyBeNextQuestion = next; // Ес­ли хоть один эле­мент от­ли­ча­ет­ся, мас­си­вы не рав­ны + } + } + }); + + return readyBeNextQuestion; + } + }; + const followPreviousStep = () => { if (question?.content.rule.parentId !== "root") { const parent = getQuestionByContentId(question?.content.rule.parentId); - if (parent) { + if (parent?.type) { setCurrentQuestion(parent); } else { enqueueSnackbar("не могу получить предыдущий вопрос"); } + } + + const nextQuestionId = getNextQuestionId(); + + if (nextQuestionId) { + const nextQuestion = getQuestionByContentId(nextQuestionId); + + if (nextQuestion?.type) { + setCurrentQuestion(nextQuestion); + return; + } else { + enqueueSnackbar("не могу получить последующий вопрос"); + } } else { - enqueueSnackbar("вы находитесь на первом вопросе"); + const nextQuestion = getQuestionByContentId( + question.content.rule.default + ); + if (nextQuestion?.type) { + setCurrentQuestion(nextQuestion); + } else { + enqueueSnackbar("не могу получить последующий вопрос (дефолтный)"); + } } }; @@ -62,7 +138,7 @@ export const Footer = ({ setCurrentQuestion, question }: FooterProps) => { if (readyBeNextQuestion) { const nextQuestion = getQuestionByContentId(readyBeNextQuestion); - if (nextQuestion) { + if (nextQuestion?.type) { setCurrentQuestion(nextQuestion); return; } else { @@ -72,7 +148,7 @@ export const Footer = ({ setCurrentQuestion, question }: FooterProps) => { const nextQuestion = getQuestionByContentId( question.content.rule.default ); - if (nextQuestion) { + if (nextQuestion?.type) { setCurrentQuestion(nextQuestion); } else { enqueueSnackbar("не могу получить последующий вопрос (дефолтный)"); @@ -130,6 +206,7 @@ export const Footer = ({ setCurrentQuestion, question }: FooterProps) => { */}