fix: follow question logic
This commit is contained in:
parent
e016862740
commit
0fbce2ae96
@ -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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user