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

41 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-08-11 13:59:00 +00:00
import { useState } from "react";
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
import { DraggableListItem } from "./DraggableListItem";
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-15 12:37:12 +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-06 13:20:12 +00:00
{listQuestions[quizId]?.map((_, index) => (
2023-09-15 12:37:12 +00:00
<DraggableListItem key={index} index={index} isDragging={snapshot.isDraggingOver} />
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>
);
};