новый порядок для анкеты в перетаскивании вопросов
This commit is contained in:
parent
277a8c3076
commit
0cdc7797f8
@ -1,5 +1,5 @@
|
|||||||
import { Box } from "@mui/material";
|
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 type { DropResult } from "react-beautiful-dnd";
|
||||||
import { DragDropContext, Droppable } from "react-beautiful-dnd";
|
import { DragDropContext, Droppable } from "react-beautiful-dnd";
|
||||||
import { FormDraggableListItem, FormItem } from "./FormDraggableListItem";
|
import { FormDraggableListItem, FormItem } from "./FormDraggableListItem";
|
||||||
@ -9,7 +9,7 @@ export const FormDraggableList = () => {
|
|||||||
const questions = useQuestions().questions.filter((q) => q.type !== "result");
|
const questions = useQuestions().questions.filter((q) => q.type !== "result");
|
||||||
const onDragEnd = ({ destination, source }: DropResult) => {
|
const onDragEnd = ({ destination, source }: DropResult) => {
|
||||||
if (destination) {
|
if (destination) {
|
||||||
reorderQuestions(source.index, destination.index);
|
reorderQuestionsForm(source.index, destination.index);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -143,23 +143,28 @@ export const cleanQuestions = () =>
|
|||||||
type: "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(
|
setProducedState(
|
||||||
(state) => {
|
(state) => {
|
||||||
const question = state.questions.find((q) => q.id === questionId);
|
const [removed] = state.questions.splice(sourceIndex, 1);
|
||||||
if (!question) return;
|
state.questions.splice(destinationIndex, 0, removed);
|
||||||
if (question.type === null) throw new Error("Cannot set backend id for untyped question");
|
|
||||||
|
|
||||||
question.backendId = backendId;
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "setQuestionBackendId",
|
type: "reorderQuestionsForm",
|
||||||
questionId: questionId,
|
sourceIndex,
|
||||||
backendId,
|
destinationIndex,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
updateQuestionOrders();
|
||||||
|
};
|
||||||
|
|
||||||
const updateQuestionOrders = () => {
|
const updateQuestionOrders = () => {
|
||||||
const questions = useQuestionsStore
|
const questions = useQuestionsStore
|
||||||
.getState()
|
.getState()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user