From 78b9d668313b9773132a94a5c16f5a78f2a452b3 Mon Sep 17 00:00:00 2001 From: IlyaDoronin Date: Thu, 21 Dec 2023 17:56:29 +0300 Subject: [PATCH] feat: DeleteNodeModal --- package.json | 1 + .../Questions/BranchingMap/CsComponent.tsx | 10 +- src/pages/Questions/DeleteNodeModal/index.tsx | 131 +++++++++++------- src/stores/uiTools/store.ts | 2 - yarn.lock | 5 + 5 files changed, 94 insertions(+), 55 deletions(-) diff --git a/package.json b/package.json index 43f2c04c..f79c3503 100755 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "cypress-file-upload": "^5.0.8", "cytoscape": "^3.26.0", "cytoscape-popper": "^2.0.0", + "date-fns": "^3.0.4", "dayjs": "^1.11.10", "emoji-mart": "^5.5.2", "file-saver": "^2.0.5", diff --git a/src/pages/Questions/BranchingMap/CsComponent.tsx b/src/pages/Questions/BranchingMap/CsComponent.tsx index dd6777fc..31f6385a 100644 --- a/src/pages/Questions/BranchingMap/CsComponent.tsx +++ b/src/pages/Questions/BranchingMap/CsComponent.tsx @@ -130,7 +130,9 @@ function CsComponent({ if (Object.keys(targetQuestion).length !== 0 && parentNodeContentId && parentNodeChildren !== undefined) { clearDataAfterAddNode({ parentNodeContentId, targetQuestion, parentNodeChildren }) cy?.data('changed', true) - createFrontResult(quiz?.backendId, targetQuestion.content.id) + if (quiz) { + createFrontResult(quiz.backendId, targetQuestion.content.id) + } const es = cy?.add([ { data: { @@ -264,14 +266,16 @@ function CsComponent({ }} autoungrabify={true} /> - + ); }; function Clear() { const quiz = useCurrentQuiz(); - updateRootContentId(quiz?.id, "") + if (quiz) { + updateRootContentId(quiz?.id, ""); + } clearRuleForAll() return <> } diff --git a/src/pages/Questions/DeleteNodeModal/index.tsx b/src/pages/Questions/DeleteNodeModal/index.tsx index 0e6a21fc..56c64de2 100644 --- a/src/pages/Questions/DeleteNodeModal/index.tsx +++ b/src/pages/Questions/DeleteNodeModal/index.tsx @@ -1,61 +1,82 @@ -import { useState, useRef, useEffect, useLayoutEffect } from "react"; +import { useState, useEffect } from "react"; import { Box, Button, - FormControl, FormControlLabel, - Link, Modal, - Radio, - RadioGroup, - Tooltip, Typography, useTheme, Checkbox, } from "@mui/material"; -import { enqueueSnackbar } from "notistack"; -import { - AnyTypedQuizQuestion, - createBranchingRuleMain, -} from "../../../model/questionTypes/shared"; -import { Select } from "../Select"; +import { isSameDay, isSameMonth, isSameYear, getHours } from "date-fns"; -import RadioCheck from "@ui_kit/RadioCheck"; -import RadioIcon from "@ui_kit/RadioIcon"; -import InfoIcon from "@icons/Info"; -import { DeleteIcon } from "@icons/questionsPage/deleteIcon"; - -import { - getQuestionById, - getQuestionByContentId, - updateQuestion, -} from "@root/questions/actions"; -import { updateOpenedModalSettingsId } from "@root/uiTools/actions"; +import { getQuestionByContentId } from "@root/questions/actions"; +import { updateDeleteId } from "@root/uiTools/actions"; import { useUiTools } from "@root/uiTools/store"; -export const DeleteNodeModal = () => { - const { deleteNodeId } = useUiTools(); - const targetQuestion = getQuestionById(deleteNodeId); +import { CheckboxIcon } from "@icons/Checkbox"; - const saveData = () => { - // if (parentQuestion !== null) { - // updateQuestion( - // parentQuestion.content.id, - // (question) => (question.content = parentQuestion.content) - // ); - // } - // handleClose(); +type DeleteNodeModalProps = { + removeNode?: (id: string) => void; +}; + +export const DeleteNodeModal = ({ removeNode }: DeleteNodeModalProps) => { + const [skipModal, setSkipModal] = useState(false); + const { deleteNodeId } = useUiTools(); + const targetQuestion = getQuestionByContentId(deleteNodeId); + const theme = useTheme(); + + useEffect(() => { + const isSkipModal = getIsSkipModal(); + + if (isSkipModal) { + deleteNode(); + } + }, [deleteNodeId]); + + const getLastDeletionNodeTime = () => + Number(localStorage.getItem("lastDeletionNodeTime") || 0); + + const updateLastDeletionNodeTime = () => { + const now = new Date().getTime(); + + localStorage.setItem("lastDeletionNodeTime", String(now)); + }; + + const getIsSkipModal = () => { + const lastDeletionNodeTime = getLastDeletionNodeTime(); + const now = new Date().getTime(); + + console.log("hours", getHours(lastDeletionNodeTime)); + + const sameYear = isSameYear(lastDeletionNodeTime, now); + const sameMonth = isSameMonth(lastDeletionNodeTime, now); + const sameDay = isSameDay(lastDeletionNodeTime, now); + + return sameYear && sameMonth && sameDay; + }; + + const deleteNode = () => { + if (skipModal) { + updateLastDeletionNodeTime(); + } + + if (deleteNodeId) { + removeNode?.(deleteNodeId || ""); + } + handleClose(); }; const handleClose = () => { - updateOpenedModalSettingsId(); + updateDeleteId(""); }; return ( <> - + { alignItems: "center", }} > - - {targetQuestion?.title} - - - - - - + {targetQuestion?.title} - + + Вы правда хотите удалить этот узел? Данные его настроек и настроек + его потомков будут утеряны. + { + } + checkedIcon={} + onChange={(_, value) => setSkipModal(value)} + checked={skipModal} + /> + } + label="Не спрашивать больше сегодня" + sx={{ + userSelect: "none", + margin: "0 0 10px", + color: theme.palette.grey2.main, + }} + /> diff --git a/src/stores/uiTools/store.ts b/src/stores/uiTools/store.ts index c8cd887f..52803348 100644 --- a/src/stores/uiTools/store.ts +++ b/src/stores/uiTools/store.ts @@ -10,7 +10,6 @@ export type UiTools = { canCreatePublic: boolean; whyCantCreatePublic: Record//ид вопроса и список претензий к нему openModalInfoWhyCantCreate: boolean; -lastDeletionNodeTime: number | null; deleteNodeId: string | null; }; export type WhyCantCreatePublic = { @@ -28,7 +27,6 @@ const initialState: UiTools = { canCreatePublic: false, whyCantCreatePublic: {}, openModalInfoWhyCantCreate: false, -lastDeletionNodeTime: null, deleteNodeId: null, }; diff --git a/yarn.lock b/yarn.lock index 5c6fbdc9..2b3e7508 100755 --- a/yarn.lock +++ b/yarn.lock @@ -4204,6 +4204,11 @@ data-urls@^2.0.0: whatwg-mimetype "^2.3.0" whatwg-url "^8.0.0" +date-fns@^3.0.4: + version "3.0.4" + resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-3.0.4.tgz#56eb7916d8d4666bf9be168ef84bed0deef1077c" + integrity sha512-+daptVi95nJ/D4TPS7/gbUqneVMA0Izr4qYAsL2ZZwtcDtEP8Zge765mbXhTqWYdTSG79/VtbBigQTluiE9DBQ== + dayjs@^1.10.4, dayjs@^1.11.10: version "1.11.10" resolved "https://registry.npmjs.org/dayjs/-/dayjs-1.11.10.tgz"