fix: follow question logic

This commit is contained in:
IlyaDoronin 2023-12-04 14:47:10 +03:00
parent e016862740
commit 0fbce2ae96
2 changed files with 51 additions and 50 deletions

@ -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<Set<string>>(
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 (

@ -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;
}