fix: follow question logic
This commit is contained in:
parent
e016862740
commit
0fbce2ae96
@ -1,9 +1,12 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { Box, Typography, Button, useTheme } from "@mui/material";
|
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 { getQuestionByContentId } from "@root/questions/actions";
|
||||||
import { enqueueSnackbar } from "notistack";
|
import { enqueueSnackbar } from "notistack";
|
||||||
|
|
||||||
@ -12,10 +15,7 @@ type FooterProps = {
|
|||||||
question: QuizQuestionBase;
|
question: QuizQuestionBase;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Footer = ({
|
export const Footer = ({ setCurrentQuestion, question }: FooterProps) => {
|
||||||
setCurrentQuestion,
|
|
||||||
question,
|
|
||||||
}: FooterProps) => {
|
|
||||||
const [disabledQuestionsId, setDisabledQuestionsId] = useState<Set<string>>(
|
const [disabledQuestionsId, setDisabledQuestionsId] = useState<Set<string>>(
|
||||||
new Set()
|
new Set()
|
||||||
);
|
);
|
||||||
@ -24,60 +24,67 @@ export const Footer = ({
|
|||||||
|
|
||||||
const followPreviousStep = () => {
|
const followPreviousStep = () => {
|
||||||
if (question?.content.rule.parentId !== "root") {
|
if (question?.content.rule.parentId !== "root") {
|
||||||
const parent = getQuestionByContentId(question?.content.rule.parentId)
|
const parent = getQuestionByContentId(question?.content.rule.parentId);
|
||||||
if (parent) {
|
if (parent) {
|
||||||
setCurrentQuestion(parent)
|
setCurrentQuestion(parent);
|
||||||
} else {
|
} else {
|
||||||
enqueueSnackbar("не могу получить предыдущий вопрос")
|
enqueueSnackbar("не могу получить предыдущий вопрос");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
enqueueSnackbar("вы находитесь на первом вопросе")
|
enqueueSnackbar("вы находитесь на первом вопросе");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const followNextStep = () => {
|
const followNextStep = () => {
|
||||||
const answers = getAnswersByQuestionId(question.content.id) || []
|
if (answers.length) {
|
||||||
console.log(answers)
|
let readyBeNextQuestion = "";
|
||||||
if (answers) {
|
|
||||||
|
|
||||||
let readyBeNextQuestion = ""
|
|
||||||
question.content.rule.main.forEach(({ next, rules }) => {
|
question.content.rule.main.forEach(({ next, rules }) => {
|
||||||
console.log({ next, rules })
|
console.log({ next, rules });
|
||||||
|
|
||||||
console.log("[storeAnswers] ", rules[0].answers)
|
console.log("[storeAnswers] ", rules[0].answers);
|
||||||
console.log("[answers.answer] ", [answers.answer])
|
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++) // Цикл по всем элементам бОльшего массива
|
for (
|
||||||
if (rules[0].answers[i] !== [answers.answer][i]) readyBeNextQuestion = next; // Если хоть один элемент отличается, массивы не равны
|
var i = 0;
|
||||||
|
i < longerArray;
|
||||||
|
i++ // Цикл по всем элементам бОльшего массива
|
||||||
})
|
) {
|
||||||
|
debugger;
|
||||||
|
if (rules[0].answers[i] === answers.at(-1)?.answer)
|
||||||
|
readyBeNextQuestion = next; // Если хоть один элемент отличается, массивы не равны
|
||||||
|
}
|
||||||
|
});
|
||||||
if (readyBeNextQuestion) {
|
if (readyBeNextQuestion) {
|
||||||
console.log("мы нашли совпадение в " + readyBeNextQuestion)
|
console.log("мы нашли совпадение в " + readyBeNextQuestion);
|
||||||
|
|
||||||
const nextQuestion = getQuestionByContentId(readyBeNextQuestion)
|
const nextQuestion = getQuestionByContentId(readyBeNextQuestion);
|
||||||
console.log("next question ", nextQuestion)
|
|
||||||
|
console.log("next question ", nextQuestion);
|
||||||
if (nextQuestion) {
|
if (nextQuestion) {
|
||||||
console.log("я устанавливаю следующий вопрос " + question.title)
|
console.log("я устанавливаю следующий вопрос " + question.title);
|
||||||
setCurrentQuestion(nextQuestion)
|
setCurrentQuestion(nextQuestion);
|
||||||
return
|
return;
|
||||||
} else {
|
} else {
|
||||||
enqueueSnackbar("не могу получить последующий вопрос")
|
enqueueSnackbar("не могу получить последующий вопрос");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const nextQuestion = getQuestionByContentId(question.content.rule.default)
|
const nextQuestion = getQuestionByContentId(
|
||||||
console.log("я устанавливаю дефолтный вопрос")
|
question.content.rule.default
|
||||||
|
);
|
||||||
|
console.log("я устанавливаю дефолтный вопрос");
|
||||||
if (nextQuestion) {
|
if (nextQuestion) {
|
||||||
setCurrentQuestion(nextQuestion)
|
setCurrentQuestion(nextQuestion);
|
||||||
} else {
|
} else {
|
||||||
enqueueSnackbar("не могу получить последующий вопрос (дефолтный)")
|
enqueueSnackbar("не могу получить последующий вопрос (дефолтный)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -33,9 +33,3 @@ export const updateAnswer = (questionId: string, answer: string) => {
|
|||||||
|
|
||||||
useQuizViewStore.setState({ answers });
|
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