frontPanel/src/pages/Questions/DraggableList/index.tsx

38 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-08-11 07:25:28 +00:00
import { Box } from "@mui/material";
2023-11-29 13:49:52 +00:00
import { reorderQuestions } from "@root/questions/actions";
import { useQuestions } from "@root/questions/hooks";
import type { DropResult } from "react-beautiful-dnd";
import { DragDropContext, Droppable } from "react-beautiful-dnd";
import DraggableListItem from "./DraggableListItem";
2023-08-11 07:25:28 +00:00
2023-11-14 20:15:52 +00:00
export const DraggableList = () => {
2023-11-29 13:49:52 +00:00
const { questions, isLoading } = useQuestions();
2023-11-14 20:15:52 +00:00
const onDragEnd = ({ destination, source }: DropResult) => {
if (destination) reorderQuestions(source.index, destination.index);
2023-11-14 20:15:52 +00:00
};
2023-11-27 23:07:24 +00:00
if (isLoading && !questions) return <Box>Загрузка вопросов...</Box>;
2023-11-14 20:15:52 +00:00
return (
<DragDropContext onDragEnd={onDragEnd}>
<Droppable droppableId="droppable-list">
{(provided, snapshot) => (
<Box ref={provided.innerRef} {...provided.droppableProps}>
{questions.map((question, index) => (
<DraggableListItem
2023-11-27 23:07:24 +00:00
key={question.id}
2023-11-14 20:15:52 +00:00
question={question}
isDragging={snapshot.isDraggingOver}
index={index}
/>
))}
{provided.placeholder}
</Box>
)}
</Droppable>
</DragDropContext>
);
2023-08-11 07:25:28 +00:00
};