From 0cdc7797f8607a9324dfaaf8a09c0c4a0df18848 Mon Sep 17 00:00:00 2001 From: Nastya Date: Wed, 2 Apr 2025 01:32:10 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=80=D1=8F=D0=B4=D0=BE=D0=BA=20=D0=B4=D0=BB=D1=8F=20=D0=B0?= =?UTF-8?q?=D0=BD=D0=BA=D0=B5=D1=82=D1=8B=20=D0=B2=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D1=82=D0=B0=D1=81=D0=BA=D0=B8=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=B2=D0=BE=D0=BF=D1=80=D0=BE=D1=81=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Form/FormDraggableList/index.tsx | 4 ++-- src/stores/questions/actions.ts | 23 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/pages/Questions/Form/FormDraggableList/index.tsx b/src/pages/Questions/Form/FormDraggableList/index.tsx index 0f670568..54c2fd18 100644 --- a/src/pages/Questions/Form/FormDraggableList/index.tsx +++ b/src/pages/Questions/Form/FormDraggableList/index.tsx @@ -1,5 +1,5 @@ import { Box } from "@mui/material"; -import { reorderQuestions } from "@root/questions/actions"; +import { reorderQuestionsForm } from "@root/questions/actions"; import type { DropResult } from "react-beautiful-dnd"; import { DragDropContext, Droppable } from "react-beautiful-dnd"; import { FormDraggableListItem, FormItem } from "./FormDraggableListItem"; @@ -9,7 +9,7 @@ export const FormDraggableList = () => { const questions = useQuestions().questions.filter((q) => q.type !== "result"); const onDragEnd = ({ destination, source }: DropResult) => { if (destination) { - reorderQuestions(source.index, destination.index); + reorderQuestionsForm(source.index, destination.index); } }; diff --git a/src/stores/questions/actions.ts b/src/stores/questions/actions.ts index a920240f..5a479490 100644 --- a/src/stores/questions/actions.ts +++ b/src/stores/questions/actions.ts @@ -143,23 +143,28 @@ export const cleanQuestions = () => type: "cleanQuestions", } ); +export const reorderQuestionsForm = (sourceIndex: number, destinationIndex: number) => { + if (sourceIndex === destinationIndex) return; + + if (sourceIndex === 0) return; + + if (destinationIndex === 0) return; -const setQuestionBackendId = (questionId: string, backendId: number) => setProducedState( (state) => { - const question = state.questions.find((q) => q.id === questionId); - if (!question) return; - if (question.type === null) throw new Error("Cannot set backend id for untyped question"); - - question.backendId = backendId; + const [removed] = state.questions.splice(sourceIndex, 1); + state.questions.splice(destinationIndex, 0, removed); }, { - type: "setQuestionBackendId", - questionId: questionId, - backendId, + type: "reorderQuestionsForm", + sourceIndex, + destinationIndex, } ); + updateQuestionOrders(); +}; + const updateQuestionOrders = () => { const questions = useQuestionsStore .getState()