frontPanel/src/pages/Questions/BranchingMap/FirstNodeField.tsx

97 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-12-31 02:53:25 +00:00
import { Box } from "@mui/material";
import {
clearRuleForAll,
createResult,
updateQuestion,
2023-12-31 02:53:25 +00:00
} from "@root/questions/actions";
import { updateRootContentId } from "@root/quizes/actions";
import { useCurrentQuiz } from "@root/quizes/hooks";
2024-02-23 14:23:26 +00:00
import { useQuizStore } from "@root/quizes/store";
import {
setOpenedModalQuestions,
updateOpenedModalSettingsId,
} from "@root/uiTools/actions";
import { useUiTools } from "@root/uiTools/store";
2024-01-05 16:48:35 +00:00
import { enqueueSnackbar } from "notistack";
import { useEffect, useLayoutEffect, useRef } from "react";
2023-11-29 15:45:15 +00:00
2024-01-05 16:48:35 +00:00
export const FirstNodeField = () => {
2023-12-31 02:53:25 +00:00
const quiz = useCurrentQuiz();
const modalQuestionTargetContentId = useUiTools(
(state) => state.modalQuestionTargetContentId,
);
2023-12-31 02:53:25 +00:00
useLayoutEffect(() => {
if (!quiz) return;
2023-12-31 02:53:25 +00:00
updateOpenedModalSettingsId();
updateRootContentId(quiz.id, "");
clearRuleForAll();
}, []);
2023-12-31 02:53:25 +00:00
const { dragQuestionContentId } = useUiTools();
const Container = useRef<HTMLDivElement | null>(null);
2023-12-31 02:53:25 +00:00
const modalOpen = () => setOpenedModalQuestions(true);
2023-12-31 02:53:25 +00:00
const newRootNode = () => {
if (quiz) {
if (dragQuestionContentId) {
updateRootContentId(quiz?.id, dragQuestionContentId);
updateQuestion(
dragQuestionContentId,
(question) => (question.content.rule.parentId = "root"),
);
2024-02-23 14:23:26 +00:00
createResult(useQuizStore.getState().editQuizId, dragQuestionContentId);
2023-12-31 02:53:25 +00:00
}
} else {
enqueueSnackbar("Нет информации о взятом опроснике");
2023-11-29 15:45:15 +00:00
}
2023-12-31 02:53:25 +00:00
};
2023-11-29 15:45:15 +00:00
2023-12-31 02:53:25 +00:00
useEffect(() => {
Container.current?.addEventListener("pointerup", newRootNode);
2023-12-31 02:53:25 +00:00
Container.current?.addEventListener("click", modalOpen);
return () => {
Container.current?.removeEventListener("pointerup", newRootNode);
2023-12-31 02:53:25 +00:00
Container.current?.removeEventListener("click", modalOpen);
};
}, [dragQuestionContentId]);
2023-12-31 02:53:25 +00:00
useEffect(() => {
if (quiz) {
if (modalQuestionTargetContentId) {
updateRootContentId(quiz?.id, modalQuestionTargetContentId);
updateQuestion(
modalQuestionTargetContentId,
(question) => (question.content.rule.parentId = "root"),
);
2024-02-23 14:23:26 +00:00
createResult(
useQuizStore.getState().editQuizId,
modalQuestionTargetContentId,
);
2023-12-31 02:53:25 +00:00
}
} else {
enqueueSnackbar("Нет информации о взятом опроснике");
2023-12-31 02:53:25 +00:00
}
}, [modalQuestionTargetContentId]);
2023-12-04 13:33:43 +00:00
2023-12-31 02:53:25 +00:00
return (
<Box
ref={Container}
sx={{
height: "100%",
width: "100%",
backgroundColor: "#f2f3f7",
display: "flex",
alignItems: "center",
justifyContent: "center",
color: "#4d4d4d",
fontSize: "50px",
}}
>
+
</Box>
);
};