2024-02-08 13:42:31 +00:00
|
|
|
import { AnyTypedQuizQuestion } from "@model/questionTypes/shared";
|
|
|
|
import { setCurrentQuizStep, useQuizViewStore } from "@stores/quizView/store";
|
|
|
|
import { useCallback, useDebugValue, useMemo, useState } from "react";
|
|
|
|
import { isResultQuestionEmpty } from "../../pages/ViewPublicationPage/tools/checkEmptyData";
|
|
|
|
import { useQuizData } from "./useQuizData";
|
2024-02-10 16:25:06 +00:00
|
|
|
import moment from "moment";
|
2024-02-08 13:42:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
export function useQuestionFlowControl() {
|
|
|
|
const { settings, questions } = useQuizData();
|
|
|
|
const [currentQuestion, setCurrentQuestion] = useState<AnyTypedQuizQuestion>(getFirstQuestion);
|
|
|
|
const answers = useQuizViewStore(state => state.answers);
|
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
const linearQuestionIndex = questions.every(({ content }) => content.rule.parentId !== "root") // null when branching enabled
|
|
|
|
? questions.indexOf(currentQuestion)
|
|
|
|
: null;
|
2024-02-08 13:42:31 +00:00
|
|
|
|
|
|
|
function getFirstQuestion() {
|
|
|
|
if (questions.length === 0) throw new Error("No questions found");
|
|
|
|
|
|
|
|
if (settings.cfg.haveRoot) {
|
|
|
|
const nextQuestion = questions.find(
|
|
|
|
question => question.id === settings.cfg.haveRoot || question.content.id === settings.cfg.haveRoot
|
|
|
|
);
|
|
|
|
if (!nextQuestion) throw new Error("Root question not found");
|
|
|
|
|
|
|
|
return nextQuestion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return questions[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextQuestionId = useMemo(() => {
|
2024-02-10 16:25:06 +00:00
|
|
|
const questionAnswer = answers.find(({ questionId }) => questionId === currentQuestion.id);
|
|
|
|
|
|
|
|
if (questionAnswer && !moment.isMoment(questionAnswer.answer)) {
|
|
|
|
const userAnswers = Array.isArray(questionAnswer.answer) ? questionAnswer.answer : [questionAnswer.answer];
|
2024-02-08 13:42:31 +00:00
|
|
|
|
2024-02-10 16:25:06 +00:00
|
|
|
for (const branchingRule of currentQuestion.content.rule.main) {
|
|
|
|
if (userAnswers.some(answer => branchingRule.rules[0].answers.includes(answer))) {
|
|
|
|
return branchingRule.next;
|
|
|
|
}
|
|
|
|
}
|
2024-02-08 13:42:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!currentQuestion.required) {//вопрос не обязателен и не нашли совпадений между ответами и условиями ветвления
|
2024-02-10 10:46:13 +00:00
|
|
|
const defaultNextQuestionId = currentQuestion.content.rule.default;
|
|
|
|
if (defaultNextQuestionId.length > 1 && defaultNextQuestionId !== " ") return defaultNextQuestionId;
|
2024-02-08 13:42:31 +00:00
|
|
|
//Вопросы типа страница, ползунок, своё поле для ввода и дата не могут иметь больше 1 ребёнка. Пользователь не может настроить там дефолт
|
|
|
|
//Кинуть на ребёнка надо даже если там нет дефолта
|
|
|
|
if (
|
|
|
|
["date", "page", "text", "number"].includes(currentQuestion.type)
|
|
|
|
&& currentQuestion.content.rule.children.length === 1
|
|
|
|
) return currentQuestion.content.rule.children[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
//ничё не нашли, ищем резулт
|
|
|
|
return questions.find(q => {
|
|
|
|
return q.type === "result" && q.content.rule.parentId === currentQuestion.content.id;
|
|
|
|
})?.id;
|
|
|
|
}, [answers, currentQuestion, questions]);
|
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
const prevQuestion = linearQuestionIndex !== null
|
|
|
|
? questions[linearQuestionIndex - 1]
|
|
|
|
: questions.find(q =>
|
|
|
|
q.id === currentQuestion.content.rule.parentId
|
|
|
|
|| q.content.id === currentQuestion.content.rule.parentId
|
|
|
|
);
|
2024-02-08 13:42:31 +00:00
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
const nextQuestion = linearQuestionIndex !== null
|
2024-02-10 16:25:06 +00:00
|
|
|
? questions[linearQuestionIndex + 1] ?? questions.find(question =>
|
|
|
|
question.type === "result" && question.content.rule.parentId === "line"
|
|
|
|
)
|
2024-02-10 10:46:13 +00:00
|
|
|
: questions.find(q => q.id === nextQuestionId || q.content.id === nextQuestionId);
|
2024-02-08 13:42:31 +00:00
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
const showResult = useCallback(() => {
|
2024-02-10 16:25:06 +00:00
|
|
|
if (nextQuestion?.type !== "result") throw new Error("Current question is not result");
|
|
|
|
if (isResultQuestionEmpty(nextQuestion)) {
|
2024-02-10 10:46:13 +00:00
|
|
|
setCurrentQuizStep("contactform");
|
2024-02-10 16:25:06 +00:00
|
|
|
return;
|
2024-02-08 13:42:31 +00:00
|
|
|
}
|
2024-02-10 16:25:06 +00:00
|
|
|
|
|
|
|
setCurrentQuestion(nextQuestion);
|
|
|
|
if (settings.cfg.resultInfo.showResultForm === "after") setCurrentQuizStep("contactform");
|
|
|
|
}, [nextQuestion, settings.cfg.resultInfo.showResultForm]);
|
2024-02-08 13:42:31 +00:00
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
const showResultAfterContactForm = useCallback(() => {
|
2024-02-10 16:25:06 +00:00
|
|
|
if (currentQuestion.type !== "result") throw new Error("Current question is not result");
|
|
|
|
if (isResultQuestionEmpty(currentQuestion)) {
|
|
|
|
console.warn("Result question is empty");
|
|
|
|
return;
|
|
|
|
}
|
2024-02-08 13:42:31 +00:00
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
setCurrentQuizStep("question");
|
2024-02-10 16:25:06 +00:00
|
|
|
}, [currentQuestion]);
|
2024-02-08 13:42:31 +00:00
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
const moveToPrevQuestion = useCallback(() => {
|
|
|
|
if (!prevQuestion) throw new Error("Previous question not found");
|
2024-02-08 13:42:31 +00:00
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
setCurrentQuestion(prevQuestion);
|
|
|
|
}, [prevQuestion]);
|
2024-02-08 13:42:31 +00:00
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
const moveToNextQuestion = useCallback(() => {
|
2024-02-08 13:42:31 +00:00
|
|
|
if (!nextQuestion) throw new Error("Next question not found");
|
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
if (nextQuestion.type === "result") return showResult();
|
2024-02-08 13:42:31 +00:00
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
setCurrentQuestion(nextQuestion);
|
|
|
|
}, [nextQuestion, showResult]);
|
2024-02-08 13:42:31 +00:00
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
const isPreviousButtonEnabled = Boolean(prevQuestion);
|
2024-02-08 13:42:31 +00:00
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
const isNextButtonEnabled = useMemo(() => {
|
|
|
|
const hasAnswer = answers.some(({ questionId }) => questionId === currentQuestion.id);
|
2024-02-08 13:42:31 +00:00
|
|
|
|
|
|
|
if ("required" in currentQuestion.content && currentQuestion.content.required) {
|
2024-02-10 10:46:13 +00:00
|
|
|
return hasAnswer;
|
2024-02-08 13:42:31 +00:00
|
|
|
}
|
|
|
|
|
2024-02-10 10:46:13 +00:00
|
|
|
return Boolean(nextQuestion);
|
|
|
|
}, [answers, currentQuestion.content, currentQuestion.id, nextQuestion]);
|
2024-02-08 13:42:31 +00:00
|
|
|
|
|
|
|
useDebugValue({
|
2024-02-10 10:46:13 +00:00
|
|
|
linearQuestionIndex,
|
2024-02-10 16:25:06 +00:00
|
|
|
currentQuestionTitle: currentQuestion.title,
|
|
|
|
prevQuestionTitle: prevQuestion?.title,
|
|
|
|
nextQuestionTitle: nextQuestion?.title,
|
2024-02-08 13:42:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
currentQuestion,
|
2024-02-10 16:25:06 +00:00
|
|
|
currentQuestionStepNumber: linearQuestionIndex === null ? null : linearQuestionIndex + 1,
|
2024-02-10 10:46:13 +00:00
|
|
|
isNextButtonEnabled,
|
|
|
|
isPreviousButtonEnabled,
|
|
|
|
moveToPrevQuestion,
|
|
|
|
moveToNextQuestion,
|
2024-02-08 13:42:31 +00:00
|
|
|
showResultAfterContactForm,
|
|
|
|
};
|
|
|
|
}
|