2023-09-06 13:20:12 +00:00
|
|
|
import { useParams } from "react-router-dom";
|
2023-08-11 07:25:28 +00:00
|
|
|
import { Box } from "@mui/material";
|
2023-08-29 12:39:51 +00:00
|
|
|
import { DragDropContext, Droppable } from "react-beautiful-dnd";
|
2023-08-11 07:25:28 +00:00
|
|
|
|
2023-09-28 12:33:02 +00:00
|
|
|
import DraggableListItem from "./DraggableListItem";
|
2023-08-11 07:25:28 +00:00
|
|
|
|
|
|
|
import { questionStore, updateQuestionsListDragAndDrop } from "@root/questions";
|
|
|
|
|
|
|
|
import { reorder } from "./helper";
|
|
|
|
|
|
|
|
import type { DropResult } from "react-beautiful-dnd";
|
|
|
|
|
|
|
|
export const DraggableList = () => {
|
2023-09-06 13:20:12 +00:00
|
|
|
const quizId = Number(useParams().quizId);
|
2023-08-11 07:25:28 +00:00
|
|
|
const { listQuestions } = questionStore();
|
|
|
|
|
|
|
|
const onDragEnd = ({ destination, source }: DropResult) => {
|
|
|
|
if (destination) {
|
2023-09-25 08:38:06 +00:00
|
|
|
const newItems = reorder(
|
|
|
|
listQuestions[quizId],
|
|
|
|
source.index,
|
|
|
|
destination.index
|
|
|
|
);
|
2023-08-11 07:25:28 +00:00
|
|
|
|
2023-09-06 13:20:12 +00:00
|
|
|
updateQuestionsListDragAndDrop(quizId, newItems);
|
2023-08-11 07:25:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-09-13 10:58:07 +00:00
|
|
|
<DragDropContext onDragEnd={onDragEnd}>
|
2023-08-29 12:39:51 +00:00
|
|
|
<Droppable droppableId="droppable-list">
|
2023-09-13 10:58:07 +00:00
|
|
|
{(provided, snapshot) => (
|
|
|
|
<Box ref={provided.innerRef} {...provided.droppableProps}>
|
2023-09-25 10:57:58 +00:00
|
|
|
{listQuestions[quizId]?.map((_, index) => (
|
2023-09-25 08:38:06 +00:00
|
|
|
<DraggableListItem
|
2023-09-25 10:57:58 +00:00
|
|
|
key={index}
|
2023-09-25 08:38:06 +00:00
|
|
|
index={index}
|
|
|
|
isDragging={snapshot.isDraggingOver}
|
2023-09-28 12:33:02 +00:00
|
|
|
questionData={_}
|
2023-09-25 08:38:06 +00:00
|
|
|
/>
|
2023-08-11 07:25:28 +00:00
|
|
|
))}
|
|
|
|
{provided.placeholder}
|
|
|
|
</Box>
|
|
|
|
)}
|
2023-08-29 12:39:51 +00:00
|
|
|
</Droppable>
|
2023-08-11 07:25:28 +00:00
|
|
|
</DragDropContext>
|
|
|
|
);
|
|
|
|
};
|