2023-08-18 11:16:56 +00:00
|
|
|
import { Box } from "@mui/material";
|
|
|
|
import { DragDropContext } from "react-beautiful-dnd";
|
|
|
|
|
|
|
|
import { StrictModeDroppable } from "./StrictModeDroppable";
|
|
|
|
import { AnswerItem } from "./AnswerItem";
|
|
|
|
|
|
|
|
import { updateVariants } from "@root/questions";
|
|
|
|
|
|
|
|
import { reorder } from "./helper";
|
|
|
|
|
|
|
|
import type { DropResult } from "react-beautiful-dnd";
|
|
|
|
import type { Variants } from "@root/questions";
|
|
|
|
|
|
|
|
type AnswerDraggableListProps = {
|
|
|
|
variants: Variants[];
|
|
|
|
totalIndex: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AnswerDraggableList = ({
|
|
|
|
variants,
|
|
|
|
totalIndex,
|
|
|
|
}: AnswerDraggableListProps) => {
|
|
|
|
const onDragEnd = ({ destination, source }: DropResult) => {
|
|
|
|
if (destination) {
|
|
|
|
const newItems = reorder(variants, source.index, destination.index);
|
|
|
|
|
|
|
|
updateVariants(totalIndex, newItems);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DragDropContext onDragEnd={onDragEnd}>
|
2023-08-18 13:43:18 +00:00
|
|
|
<StrictModeDroppable droppableId="droppable-answer-list">
|
2023-08-18 11:16:56 +00:00
|
|
|
{(provided) => (
|
|
|
|
<Box ref={provided.innerRef} {...provided.droppableProps}>
|
|
|
|
{variants.map((variant, index) => (
|
|
|
|
<AnswerItem
|
2023-08-18 15:39:41 +00:00
|
|
|
key={index}
|
2023-08-18 11:16:56 +00:00
|
|
|
index={index}
|
|
|
|
totalIndex={totalIndex}
|
|
|
|
variants={variants}
|
|
|
|
variant={variant}
|
|
|
|
/>
|
|
|
|
))}
|
2023-08-18 13:43:18 +00:00
|
|
|
{provided.placeholder}
|
2023-08-18 11:16:56 +00:00
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
</StrictModeDroppable>
|
|
|
|
</DragDropContext>
|
|
|
|
);
|
|
|
|
};
|