refactor branching logic
remove result question calculation
This commit is contained in:
parent
c1ccc5d7c5
commit
fd1dd8624a
@ -5,8 +5,8 @@ import QuizAnswerer from "./QuizAnswerer";
|
|||||||
import { QuizIdContext } from "./contexts/QuizIdContext";
|
import { QuizIdContext } from "./contexts/QuizIdContext";
|
||||||
import { RootContainerWidthContext } from "./contexts/RootContainerWidthContext";
|
import { RootContainerWidthContext } from "./contexts/RootContainerWidthContext";
|
||||||
|
|
||||||
|
const defaultQuizId = "45ef7f9c-784d-4e58-badb-f6b337f08ba0"; // branching
|
||||||
const defaultQuizId = "45ef7f9c-784d-4e58-badb-f6b337f08ba0";
|
// const defaultQuizId = "a9d31460-132a-4479-a3f0-90241498b6f9"; // linear
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
const quizId = useParams().quizId ?? defaultQuizId;
|
const quizId = useParams().quizId ?? defaultQuizId;
|
||||||
|
@ -6,7 +6,7 @@ import { devtools } from "zustand/middleware";
|
|||||||
import type { Moment } from "moment";
|
import type { Moment } from "moment";
|
||||||
import { QuizStep } from "@model/settingsData";
|
import { QuizStep } from "@model/settingsData";
|
||||||
|
|
||||||
type Answer = {
|
type QuestionAnswer = {
|
||||||
questionId: string;
|
questionId: string;
|
||||||
answer: string | string[] | Moment;
|
answer: string | string[] | Moment;
|
||||||
};
|
};
|
||||||
@ -17,7 +17,7 @@ type OwnVariant = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
interface QuizViewStore {
|
interface QuizViewStore {
|
||||||
answers: Answer[];
|
answers: QuestionAnswer[];
|
||||||
ownVariants: OwnVariant[];
|
ownVariants: OwnVariant[];
|
||||||
currentQuizStep: QuizStep;
|
currentQuizStep: QuizStep;
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import { QuizQuestionResult } from "@model/questionTypes/result";
|
|
||||||
import { AnyTypedQuizQuestion } from "@model/questionTypes/shared";
|
import { AnyTypedQuizQuestion } from "@model/questionTypes/shared";
|
||||||
import { setCurrentQuizStep, useQuizViewStore } from "@stores/quizView/store";
|
import { setCurrentQuizStep, useQuizViewStore } from "@stores/quizView/store";
|
||||||
import { useCallback, useDebugValue, useMemo, useState } from "react";
|
import { useCallback, useDebugValue, useMemo, useState } from "react";
|
||||||
import { isResultQuestionEmpty } from "../../pages/ViewPublicationPage/tools/checkEmptyData";
|
import { isResultQuestionEmpty } from "../../pages/ViewPublicationPage/tools/checkEmptyData";
|
||||||
import { useQuizData } from "./useQuizData";
|
import { useQuizData } from "./useQuizData";
|
||||||
|
import moment from "moment";
|
||||||
|
|
||||||
|
|
||||||
export function useQuestionFlowControl() {
|
export function useQuestionFlowControl() {
|
||||||
@ -31,41 +31,19 @@ export function useQuestionFlowControl() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const nextQuestionId = useMemo(() => {
|
const nextQuestionId = useMemo(() => {
|
||||||
console.log("Смотрим какой вопрос будет дальше. Что у нас сегодня вкусненького? Щя покажу от какого вопроса мы ищем следующий шаг");
|
const questionAnswer = answers.find(({ questionId }) => questionId === currentQuestion.id);
|
||||||
console.log(currentQuestion);
|
|
||||||
console.log("От вот этого /|");
|
|
||||||
|
|
||||||
//вопрос обязателен, анализируем ответ и условия ветвления
|
if (questionAnswer && !moment.isMoment(questionAnswer.answer)) {
|
||||||
if (answers.length) {
|
const userAnswers = Array.isArray(questionAnswer.answer) ? questionAnswer.answer : [questionAnswer.answer];
|
||||||
let readyBeNextQuestion = "";
|
|
||||||
const answer = answers.find(({ questionId }) => questionId === currentQuestion.id);
|
|
||||||
|
|
||||||
currentQuestion.content.rule.main.forEach(({ next, rules }) => {
|
for (const branchingRule of currentQuestion.content.rule.main) {
|
||||||
const longerArray = Math.max(
|
if (userAnswers.some(answer => branchingRule.rules[0].answers.includes(answer))) {
|
||||||
rules[0].answers.length,
|
return branchingRule.next;
|
||||||
answer?.answer && Array.isArray(answer?.answer) ? answer?.answer.length : [answer?.answer].length
|
|
||||||
);
|
|
||||||
|
|
||||||
for (let i = 0; i < longerArray; i++) {
|
|
||||||
if (Array.isArray(answer?.answer)) {
|
|
||||||
if (answer?.answer.find((item) => String(item === rules[0].answers[i]))) {
|
|
||||||
readyBeNextQuestion = next; // Если хоть один элемент отличается, массивы не равны
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (String(rules[0].answers[i]) === answer?.answer) {
|
|
||||||
readyBeNextQuestion = next; // Если хоть один элемент отличается, массивы не равны
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
if (readyBeNextQuestion) return readyBeNextQuestion;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!currentQuestion.required) {//вопрос не обязателен и не нашли совпадений между ответами и условиями ветвления
|
if (!currentQuestion.required) {//вопрос не обязателен и не нашли совпадений между ответами и условиями ветвления
|
||||||
console.log("вопрос не обязателен ищем дальше");
|
|
||||||
const defaultNextQuestionId = currentQuestion.content.rule.default;
|
const defaultNextQuestionId = currentQuestion.content.rule.default;
|
||||||
if (defaultNextQuestionId.length > 1 && defaultNextQuestionId !== " ") return defaultNextQuestionId;
|
if (defaultNextQuestionId.length > 1 && defaultNextQuestionId !== " ") return defaultNextQuestionId;
|
||||||
//Вопросы типа страница, ползунок, своё поле для ввода и дата не могут иметь больше 1 ребёнка. Пользователь не может настроить там дефолт
|
//Вопросы типа страница, ползунок, своё поле для ввода и дата не могут иметь больше 1 ребёнка. Пользователь не может настроить там дефолт
|
||||||
@ -77,7 +55,6 @@ export function useQuestionFlowControl() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//ничё не нашли, ищем резулт
|
//ничё не нашли, ищем резулт
|
||||||
console.log("ничё не нашли, ищем резулт ");
|
|
||||||
return questions.find(q => {
|
return questions.find(q => {
|
||||||
return q.type === "result" && q.content.rule.parentId === currentQuestion.content.id;
|
return q.type === "result" && q.content.rule.parentId === currentQuestion.content.id;
|
||||||
})?.id;
|
})?.id;
|
||||||
@ -91,42 +68,31 @@ export function useQuestionFlowControl() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const nextQuestion = linearQuestionIndex !== null
|
const nextQuestion = linearQuestionIndex !== null
|
||||||
? questions[linearQuestionIndex + 1]
|
? questions[linearQuestionIndex + 1] ?? questions.find(question =>
|
||||||
|
question.type === "result" && question.content.rule.parentId === "line"
|
||||||
|
)
|
||||||
: questions.find(q => q.id === nextQuestionId || q.content.id === nextQuestionId);
|
: questions.find(q => q.id === nextQuestionId || q.content.id === nextQuestionId);
|
||||||
|
|
||||||
const resultQuestion = useMemo(() => {
|
|
||||||
let resultQuestion: QuizQuestionResult | undefined;
|
|
||||||
|
|
||||||
if (currentQuestion.type === "result") resultQuestion = currentQuestion;
|
|
||||||
if (settings.cfg.haveRoot) {
|
|
||||||
resultQuestion = questions.find((question): question is QuizQuestionResult => {
|
|
||||||
return question.type === "result" && question.content.rule.parentId === currentQuestion.content.id;
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
resultQuestion = questions.find((question): question is QuizQuestionResult => {
|
|
||||||
return question.type === "result" && question.content.rule.parentId === "line";
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (resultQuestion && !isResultQuestionEmpty(resultQuestion)) return resultQuestion;
|
|
||||||
}, [currentQuestion, questions, settings.cfg.haveRoot]);
|
|
||||||
|
|
||||||
const showResult = useCallback(() => {
|
const showResult = useCallback(() => {
|
||||||
if (!resultQuestion) throw new Error("Result question not found");
|
if (nextQuestion?.type !== "result") throw new Error("Current question is not result");
|
||||||
|
if (isResultQuestionEmpty(nextQuestion)) {
|
||||||
setCurrentQuestion(resultQuestion);
|
|
||||||
|
|
||||||
if (settings.cfg.resultInfo.showResultForm === "after") {
|
|
||||||
setCurrentQuizStep("contactform");
|
setCurrentQuizStep("contactform");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}, [resultQuestion, settings.cfg.resultInfo.showResultForm]);
|
|
||||||
|
setCurrentQuestion(nextQuestion);
|
||||||
|
if (settings.cfg.resultInfo.showResultForm === "after") setCurrentQuizStep("contactform");
|
||||||
|
}, [nextQuestion, settings.cfg.resultInfo.showResultForm]);
|
||||||
|
|
||||||
const showResultAfterContactForm = useCallback(() => {
|
const showResultAfterContactForm = useCallback(() => {
|
||||||
if (!resultQuestion) throw new Error("Result question not found");
|
if (currentQuestion.type !== "result") throw new Error("Current question is not result");
|
||||||
if (resultQuestion.type !== "result") throw new Error("Current question is not a result question");
|
if (isResultQuestionEmpty(currentQuestion)) {
|
||||||
|
console.warn("Result question is empty");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
setCurrentQuizStep("question");
|
setCurrentQuizStep("question");
|
||||||
}, [resultQuestion]);
|
}, [currentQuestion]);
|
||||||
|
|
||||||
const moveToPrevQuestion = useCallback(() => {
|
const moveToPrevQuestion = useCallback(() => {
|
||||||
if (!prevQuestion) throw new Error("Previous question not found");
|
if (!prevQuestion) throw new Error("Previous question not found");
|
||||||
@ -156,15 +122,14 @@ export function useQuestionFlowControl() {
|
|||||||
|
|
||||||
useDebugValue({
|
useDebugValue({
|
||||||
linearQuestionIndex,
|
linearQuestionIndex,
|
||||||
currentQuestion,
|
currentQuestionTitle: currentQuestion.title,
|
||||||
prevQuestion,
|
prevQuestionTitle: prevQuestion?.title,
|
||||||
nextQuestion,
|
nextQuestionTitle: nextQuestion?.title,
|
||||||
resultQuestion,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
currentQuestion,
|
currentQuestion,
|
||||||
currentQuestionStepNumber: linearQuestionIndex && linearQuestionIndex + 1,
|
currentQuestionStepNumber: linearQuestionIndex === null ? null : linearQuestionIndex + 1,
|
||||||
isNextButtonEnabled,
|
isNextButtonEnabled,
|
||||||
isPreviousButtonEnabled,
|
isPreviousButtonEnabled,
|
||||||
moveToPrevQuestion,
|
moveToPrevQuestion,
|
||||||
|
Loading…
Reference in New Issue
Block a user