diff --git a/src/pages/startPage/EditPage.tsx b/src/pages/startPage/EditPage.tsx index 0cc5d781..1781259f 100755 --- a/src/pages/startPage/EditPage.tsx +++ b/src/pages/startPage/EditPage.tsx @@ -46,6 +46,7 @@ import { clearAuthToken } from "@frontend/kitui"; import { logout } from "@api/auth"; import { AnyTypedQuizQuestion } from "@model/questionTypes/shared"; import { ModalInfoWhyCantCreate } from "./ModalInfoWhyCantCreate"; +import { checkQuestionHint } from "@utils/checkQuestionHint"; import { type } from "os"; export default function EditPage() { @@ -95,28 +96,9 @@ export default function EditPage() { [] ); - - const updateQuestionHint = useDebouncedCallback((questions: AnyTypedQuizQuestion[]) => { - const problems: any = {} - - questions.forEach((question) => { - //Если не участвует в ветвлении, или безтиповый, или резулт - он нам не интересен - if (question.type === null - || question.type === "result" - || question.content.rule.parentId.length === 0) return - - //если есть дети, но нет дефолта - логическая ошибка. Так нельзя - if (question.content.rule.children.length > 0 && question.content.rule.default.length === 0) { - problems[question.content.id] = { - name: question.title, - problems: ["Не выбран дефолтный вопрос"] - } - } - - }) - + const problems = checkQuestionHint(questions) useUiTools.setState({ whyCantCreatePublic: problems }) if (Object.keys(problems).length > 0) { updateQuiz(quiz?.id, (state) => { state.status = "stop" }) diff --git a/src/utils/checkQuestionHint.ts b/src/utils/checkQuestionHint.ts new file mode 100644 index 00000000..1e96eb56 --- /dev/null +++ b/src/utils/checkQuestionHint.ts @@ -0,0 +1,66 @@ +import { AnyTypedQuizQuestion, QuestionBranchingRuleMain } from "@model/questionTypes/shared"; +import { WhyCantCreatePublic } from "@root/uiTools/store"; +import { getQuestionByContentId } from "@root/questions/actions"; + +export const checkQuestionHint = (questions: AnyTypedQuizQuestion): Record => { + + const problems: any = {} + + const pushProblem = (id: string, problem: string, title: string) => { + //Если первый вопрос с проблемой - создаём запись. Если не первый - добавляем проблему + if (id in problems) { + problems[id].problems.push(problem) + } else { + problems[id] = { + name: title, + problems: [problem] + } + } + } + + questions.forEach((question: AnyTypedQuizQuestion) => { + //Если не участвует в ветвлении, или безтиповый, или резулт - он нам не интересен + if (question.type === null + || question.type === "result" + || question.content.rule.parentId.length === 0) return + + //если есть дети, но нет дефолта - логическая ошибка. Так нельзя + if (question.content.rule.children.length > 0 && question.content.rule.default.length === 0) { + pushProblem(question.content.id, "Не выбран дефолтный вопрос", question.title) + } + + //Rules вопроса не должны совпадать + const buffer: QuestionBranchingRuleMain[] = [] + question.content.rule.main.forEach((condition: QuestionBranchingRuleMain) => { + buffer.forEach((oldCondition: QuestionBranchingRuleMain) => { + if (areRulesEqual(condition.rules, oldCondition.rules)) { + pushProblem( + question.content.id, + `У вопроса "${getQuestionByContentId(condition.next)?.title || "noname"}" и "${getQuestionByContentId(oldCondition.next)?.title || "noname"}" одинаковые условия ветвления`, + question.title + ) + } + }) + buffer.push(condition) + }) + }) + + return problems +} + +const areRulesEqual = (first: any, second: any) => { + const firstArray = first[0].answers + const secondArray = second[0].answers + if (firstArray.length === secondArray.length) { + if (firstArray.length > 1) { + if ( + firstArray.every((element: any, index: number) => element === secondArray[index]) + && + first.or === second.or + ) return true; + } else { + if (firstArray[0] === secondArray[0]) return true; + } + } + return false; +}; \ No newline at end of file diff --git a/tsconfig.extend.json b/tsconfig.extend.json index 81832f9f..eabc7e3d 100755 --- a/tsconfig.extend.json +++ b/tsconfig.extend.json @@ -16,6 +16,9 @@ ], "@model/*": [ "./model/*" + ], + "@utils/*": [ + "./utils/*" ] } }