fix setting current question to result
This commit is contained in:
parent
9459970624
commit
0391f44828
@ -4,6 +4,7 @@ import { useCallback, useDebugValue, useMemo, useState } from "react";
|
||||
import { isResultQuestionEmpty } from "../../components/ViewPublicationPage/tools/checkEmptyData";
|
||||
import moment from "moment";
|
||||
import { useQuizData } from "@contexts/QuizDataContext";
|
||||
import { enqueueSnackbar } from "notistack";
|
||||
|
||||
|
||||
export function useQuestionFlowControl() {
|
||||
@ -35,12 +36,12 @@ export function useQuestionFlowControl() {
|
||||
const nextQuestionIdPointsLogic = () => {
|
||||
return questions.find(question =>
|
||||
question.type === "result" && question.content.rule.parentId === "line"
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
const nextQuestionIdMainLogic = () => {
|
||||
const questionAnswer = answers.find(({ questionId }) => questionId === currentQuestion.id);
|
||||
|
||||
|
||||
//Если ответ существует и не является объектом момента
|
||||
if (questionAnswer && !moment.isMoment(questionAnswer.answer)) {
|
||||
//Ответы должны храниться в массиве
|
||||
@ -72,10 +73,10 @@ export function useQuestionFlowControl() {
|
||||
};
|
||||
const calculateNextQuestionId = useMemo(() => {
|
||||
if (Boolean(settings.cfg.score)) {
|
||||
return nextQuestionIdPointsLogic()
|
||||
return nextQuestionIdPointsLogic();
|
||||
}
|
||||
return nextQuestionIdMainLogic()
|
||||
}, [answers, currentQuestion, questions])
|
||||
return nextQuestionIdMainLogic();
|
||||
}, [answers, currentQuestion, questions]);
|
||||
|
||||
const prevQuestion = linearQuestionIndex !== null
|
||||
? questions[linearQuestionIndex - 1]
|
||||
@ -86,50 +87,50 @@ export function useQuestionFlowControl() {
|
||||
|
||||
const findResultPointsLogic = () => {
|
||||
const results = questions
|
||||
.filter(e => e.type === "result" && e.content.rule.minScore !== undefined && e.content.rule.minScore <= pointsSum)
|
||||
const numbers = results.map(e => e.type === "result" && e.content.rule.minScore !== undefined ? e.content.rule.minScore : 0)
|
||||
const indexOfNext = Math.max(...numbers)
|
||||
console.log(results)
|
||||
console.log(numbers)
|
||||
console.log(indexOfNext)
|
||||
return results[numbers.indexOf(indexOfNext)]
|
||||
}
|
||||
.filter(e => e.type === "result" && e.content.rule.minScore !== undefined && e.content.rule.minScore <= pointsSum);
|
||||
const numbers = results.map(e => e.type === "result" && e.content.rule.minScore !== undefined ? e.content.rule.minScore : 0);
|
||||
const indexOfNext = Math.max(...numbers);
|
||||
console.log(results);
|
||||
console.log(numbers);
|
||||
console.log(indexOfNext);
|
||||
return results[numbers.indexOf(indexOfNext)];
|
||||
};
|
||||
|
||||
const getNextQuestion = () => {
|
||||
let next
|
||||
console.log(11111111111)
|
||||
let next;
|
||||
console.log(11111111111);
|
||||
//Искать можно двумя логиками. Основной и балловой
|
||||
if (Boolean(settings.cfg.score)) {
|
||||
//Балловая
|
||||
console.log(222222222)
|
||||
console.log(222222222);
|
||||
|
||||
|
||||
//Ищем линейно
|
||||
if (linearQuestionIndex !== null) {
|
||||
console.log(33333333333)
|
||||
console.log(33333333333);
|
||||
|
||||
|
||||
next = questions[linearQuestionIndex + 1]
|
||||
console.log(4444444)
|
||||
console.log("перед ифом", next)
|
||||
if (next?.type === "result" || next == undefined) next = findResultPointsLogic()
|
||||
console.log(5555555555)
|
||||
next = questions[linearQuestionIndex + 1];
|
||||
console.log(4444444);
|
||||
console.log("перед ифом", next);
|
||||
if (next?.type === "result" || next == undefined) next = findResultPointsLogic();
|
||||
console.log(5555555555);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
console.log(6666666)
|
||||
console.log(6666666);
|
||||
//Основная
|
||||
if (linearQuestionIndex !== null) {
|
||||
console.log(777777777)
|
||||
console.log(777777777);
|
||||
//Ищем линейно
|
||||
next = questions[linearQuestionIndex + 1] ?? questions.find(question =>
|
||||
question.type === "result" && question.content.rule.parentId === "line"
|
||||
)
|
||||
);
|
||||
} else {
|
||||
console.log(88888888888888)
|
||||
console.log(88888888888888);
|
||||
//Ищем ветвлением
|
||||
next = questions.find(q => q.id === calculateNextQuestionId || q.content.id === calculateNextQuestionId);
|
||||
}
|
||||
@ -137,23 +138,22 @@ export function useQuestionFlowControl() {
|
||||
console.log("next", next);
|
||||
if (!next && currentQuestion.type !== "result") throw new Error("Не найден следующий вопрос");
|
||||
return next;
|
||||
}
|
||||
const nextQuestion = getNextQuestion()
|
||||
};
|
||||
const nextQuestion = getNextQuestion();
|
||||
const showResult = useCallback(() => {
|
||||
if (nextQuestion?.type !== "result") throw new Error("Current question is not result");
|
||||
if (isResultQuestionEmpty(nextQuestion)) {
|
||||
setCurrentQuizStep("contactform");
|
||||
return;
|
||||
}
|
||||
|
||||
setCurrentQuestion(nextQuestion);
|
||||
if (settings.cfg.resultInfo.showResultForm === "after") setCurrentQuizStep("contactform");
|
||||
if (
|
||||
settings.cfg.resultInfo.showResultForm === "after"
|
||||
|| isResultQuestionEmpty(nextQuestion)
|
||||
) setCurrentQuizStep("contactform");
|
||||
}, [nextQuestion, settings.cfg.resultInfo.showResultForm]);
|
||||
|
||||
const showResultAfterContactForm = useCallback(() => {
|
||||
if (currentQuestion.type !== "result") throw new Error("Current question is not result");
|
||||
if (isResultQuestionEmpty(currentQuestion)) {
|
||||
console.warn("Result question is empty");
|
||||
enqueueSnackbar("Данные отправлены");
|
||||
return;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user