From 0fbce2ae9645091b2142dbe74ade8882e7064c2e Mon Sep 17 00:00:00 2001 From: IlyaDoronin Date: Mon, 4 Dec 2023 14:47:10 +0300 Subject: [PATCH] fix: follow question logic --- src/pages/ViewPublicationPage/Footer.tsx | 95 +++++++++++++----------- src/stores/quizView.ts | 6 -- 2 files changed, 51 insertions(+), 50 deletions(-) diff --git a/src/pages/ViewPublicationPage/Footer.tsx b/src/pages/ViewPublicationPage/Footer.tsx index b8b97d61..95a672aa 100644 --- a/src/pages/ViewPublicationPage/Footer.tsx +++ b/src/pages/ViewPublicationPage/Footer.tsx @@ -1,9 +1,12 @@ import { useState, useEffect } from "react"; import { Box, Typography, Button, useTheme } from "@mui/material"; -import { useQuizViewStore, getAnswersByQuestionId } from "@root/quizView"; +import { useQuizViewStore } from "@root/quizView"; -import type { AnyTypedQuizQuestion, QuizQuestionBase } from "../../model/questionTypes/shared"; +import type { + AnyTypedQuizQuestion, + QuizQuestionBase, +} from "../../model/questionTypes/shared"; import { getQuestionByContentId } from "@root/questions/actions"; import { enqueueSnackbar } from "notistack"; @@ -12,10 +15,7 @@ type FooterProps = { question: QuizQuestionBase; }; -export const Footer = ({ - setCurrentQuestion, - question, -}: FooterProps) => { +export const Footer = ({ setCurrentQuestion, question }: FooterProps) => { const [disabledQuestionsId, setDisabledQuestionsId] = useState>( new Set() ); @@ -24,60 +24,67 @@ export const Footer = ({ const followPreviousStep = () => { if (question?.content.rule.parentId !== "root") { - const parent = getQuestionByContentId(question?.content.rule.parentId) + const parent = getQuestionByContentId(question?.content.rule.parentId); if (parent) { - setCurrentQuestion(parent) + setCurrentQuestion(parent); } else { - enqueueSnackbar("не могу получить предыдущий вопрос") + enqueueSnackbar("не могу получить предыдущий вопрос"); } } else { - enqueueSnackbar("вы находитесь на первом вопросе") + enqueueSnackbar("вы находитесь на первом вопросе"); } }; const followNextStep = () => { - const answers = getAnswersByQuestionId(question.content.id) || [] - console.log(answers) - if (answers) { + if (answers.length) { + let readyBeNextQuestion = ""; - let readyBeNextQuestion = "" question.content.rule.main.forEach(({ next, rules }) => { - console.log({ next, rules }) + console.log({ next, rules }); - console.log("[storeAnswers] ", rules[0].answers) - console.log("[answers.answer] ", [answers.answer]) + console.log("[storeAnswers] ", rules[0].answers); + console.log("[answers.answer] ", [answers.at(-1)?.answer]); - let longerArray = Math.max(rules[0].answers.length, [answers.answer].length) + let longerArray = Math.max( + rules[0].answers.length, + [answers.at(-1)?.answer].length + ); - for (var i = 0; i < longerArray; i++) // Цикл по всем эле­мен­там бОльшего массива - if (rules[0].answers[i] !== [answers.answer][i]) readyBeNextQuestion = next; // Ес­ли хоть один эле­мент от­ли­ча­ет­ся, мас­си­вы не рав­ны - - - }) - if (readyBeNextQuestion) { - console.log("мы нашли совпадение в " + readyBeNextQuestion) - - const nextQuestion = getQuestionByContentId(readyBeNextQuestion) - console.log("next question ", nextQuestion) - if (nextQuestion) { - console.log("я устанавливаю следующий вопрос " + question.title) - setCurrentQuestion(nextQuestion) - return - } else { - enqueueSnackbar("не могу получить последующий вопрос") - } - } else { - const nextQuestion = getQuestionByContentId(question.content.rule.default) - console.log("я устанавливаю дефолтный вопрос") - if (nextQuestion) { - setCurrentQuestion(nextQuestion) - } else { - enqueueSnackbar("не могу получить последующий вопрос (дефолтный)") - } + for ( + var i = 0; + i < longerArray; + i++ // Цикл по всем эле­мен­там бОльшего массива + ) { + debugger; + if (rules[0].answers[i] === answers.at(-1)?.answer) + readyBeNextQuestion = next; // Ес­ли хоть один эле­мент от­ли­ча­ет­ся, мас­си­вы не рав­ны } + }); + if (readyBeNextQuestion) { + console.log("мы нашли совпадение в " + readyBeNextQuestion); + const nextQuestion = getQuestionByContentId(readyBeNextQuestion); + + console.log("next question ", nextQuestion); + if (nextQuestion) { + console.log("я устанавливаю следующий вопрос " + question.title); + setCurrentQuestion(nextQuestion); + return; + } else { + enqueueSnackbar("не могу получить последующий вопрос"); + } + } else { + const nextQuestion = getQuestionByContentId( + question.content.rule.default + ); + console.log("я устанавливаю дефолтный вопрос"); + if (nextQuestion) { + setCurrentQuestion(nextQuestion); + } else { + enqueueSnackbar("не могу получить последующий вопрос (дефолтный)"); + } + } } - }; return ( diff --git a/src/stores/quizView.ts b/src/stores/quizView.ts index 218c87ab..0f515b32 100644 --- a/src/stores/quizView.ts +++ b/src/stores/quizView.ts @@ -33,9 +33,3 @@ export const updateAnswer = (questionId: string, answer: string) => { useQuizViewStore.setState({ answers }); }; - -export const getAnswersByQuestionId = (questionId: string) => { - if (questionId === null) return null; - const answers = [...useQuizViewStore.getState().answers]; - return answers.find(a => a.questionId === questionId) || null; -} \ No newline at end of file