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

60 lines
2.2 KiB
TypeScript
Raw Normal View History

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-09-28 12:33:02 +00:00
import DraggableListItem from "./DraggableListItem";
2023-08-11 07:25:28 +00:00
import type { DropResult } from "react-beautiful-dnd";
2023-11-14 20:15:52 +00:00
import { useCurrentQuiz } from "@root/quizes/hooks";
import { useQuestionArray } from "@root/questions/hooks";
import useSWR from "swr";
import { questionApi } from "@api/question";
import { setQuestions } from "@root/questions/actions";
import { isAxiosError } from "axios";
import { devlog } from "@frontend/kitui";
import { enqueueSnackbar } from "notistack";
2023-08-11 07:25:28 +00:00
2023-11-14 20:15:52 +00:00
export const DraggableList = () => {
const { quiz } = useCurrentQuiz();
useSWR(["questions", quiz?.id], ([, id]) => questionApi.getList({ quiz_id: id }), {
onSuccess: setQuestions,
onError: error => {
const message = isAxiosError<string>(error) ? (error.response?.data ?? "") : "";
devlog("Error getting question list", error);
enqueueSnackbar(`Не удалось получить вопросы. ${message}`);
}
});
const questions = useQuestionArray();
const onDragEnd = ({ destination, source }: DropResult) => { // TODO
// if (destination) {
// const newItems = reorder(
// listQuestions[quizId],
// source.index,
// destination.index
// );
// updateQuestionsListDragAndDrop(quizId, newItems);
// }
};
return (
<DragDropContext onDragEnd={onDragEnd}>
<Droppable droppableId="droppable-list">
{(provided, snapshot) => (
<Box ref={provided.innerRef} {...provided.droppableProps}>
{questions.map((question, index) => (
<DraggableListItem
key={question.id}
question={question}
isDragging={snapshot.isDraggingOver}
index={index}
/>
))}
{provided.placeholder}
</Box>
)}
</Droppable>
</DragDropContext>
);
2023-08-11 07:25:28 +00:00
};