анализ условий ветвления (не должно быть одинаковых)
This commit is contained in:
parent
c38c75af9a
commit
22a11d0c0a
@ -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" })
|
||||
|
66
src/utils/checkQuestionHint.ts
Normal file
66
src/utils/checkQuestionHint.ts
Normal file
@ -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<string, WhyCantCreatePublic> => {
|
||||
|
||||
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;
|
||||
};
|
@ -16,6 +16,9 @@
|
||||
],
|
||||
"@model/*": [
|
||||
"./model/*"
|
||||
],
|
||||
"@utils/*": [
|
||||
"./utils/*"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user