новый порядок для анкеты в перетаскивании вопросов

This commit is contained in:
Nastya 2025-04-02 01:32:10 +03:00
parent 277a8c3076
commit 0cdc7797f8
2 changed files with 16 additions and 11 deletions

@ -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);
}
};

@ -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()