frontAnswerer/src/utils/hooks/useQuestionFlowControl.ts

140 lines
5.9 KiB
TypeScript
Raw Normal View History

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";
import moment from "moment";
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;
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(() => {
const questionAnswer = answers.find(({ questionId }) => questionId === currentQuestion.id);
if (questionAnswer && !moment.isMoment(questionAnswer.answer)) {
const userAnswers = Array.isArray(questionAnswer.answer) ? questionAnswer.answer : [questionAnswer.answer];
for (const branchingRule of currentQuestion.content.rule.main) {
if (userAnswers.some(answer => branchingRule.rules[0].answers.includes(answer))) {
return branchingRule.next;
}
}
}
if (!currentQuestion.required) {//вопрос не обязателен и не нашли совпадений между ответами и условиями ветвления
2024-02-10 10:46:13 +00:00
const defaultNextQuestionId = currentQuestion.content.rule.default;
if (defaultNextQuestionId.length > 1 && defaultNextQuestionId !== " ") return defaultNextQuestionId;
//Вопросы типа страница, ползунок, своё поле для ввода и дата не могут иметь больше 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-10 10:46:13 +00:00
const nextQuestion = linearQuestionIndex !== null
? 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-10 10:46:13 +00:00
const showResult = useCallback(() => {
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");
return;
}
setCurrentQuestion(nextQuestion);
if (settings.cfg.resultInfo.showResultForm === "after") setCurrentQuizStep("contactform");
}, [nextQuestion, settings.cfg.resultInfo.showResultForm]);
2024-02-10 10:46:13 +00:00
const showResultAfterContactForm = useCallback(() => {
if (currentQuestion.type !== "result") throw new Error("Current question is not result");
if (isResultQuestionEmpty(currentQuestion)) {
console.warn("Result question is empty");
return;
}
2024-02-10 10:46:13 +00:00
setCurrentQuizStep("question");
}, [currentQuestion]);
2024-02-10 10:46:13 +00:00
const moveToPrevQuestion = useCallback(() => {
if (!prevQuestion) throw new Error("Previous question not found");
2024-02-10 10:46:13 +00:00
setCurrentQuestion(prevQuestion);
}, [prevQuestion]);
2024-02-10 10:46:13 +00:00
const moveToNextQuestion = useCallback(() => {
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-10 10:46:13 +00:00
setCurrentQuestion(nextQuestion);
}, [nextQuestion, showResult]);
2024-02-10 10:46:13 +00:00
const isPreviousButtonEnabled = Boolean(prevQuestion);
2024-02-10 10:46:13 +00:00
const isNextButtonEnabled = useMemo(() => {
const hasAnswer = answers.some(({ questionId }) => questionId === currentQuestion.id);
if ("required" in currentQuestion.content && currentQuestion.content.required) {
2024-02-10 10:46:13 +00:00
return hasAnswer;
}
2024-02-10 10:46:13 +00:00
return Boolean(nextQuestion);
}, [answers, currentQuestion.content, currentQuestion.id, nextQuestion]);
useDebugValue({
2024-02-10 10:46:13 +00:00
linearQuestionIndex,
currentQuestionTitle: currentQuestion.title,
prevQuestionTitle: prevQuestion?.title,
nextQuestionTitle: nextQuestion?.title,
});
return {
currentQuestion,
currentQuestionStepNumber: linearQuestionIndex === null ? null : linearQuestionIndex + 1,
2024-02-10 10:46:13 +00:00
isNextButtonEnabled,
isPreviousButtonEnabled,
moveToPrevQuestion,
moveToNextQuestion,
showResultAfterContactForm,
};
}