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

52 lines
2.0 KiB
TypeScript
Raw Normal View History

import { questionApi } from "@api/question";
import { devlog } from "@frontend/kitui";
2023-08-11 07:25:28 +00:00
import { Box } from "@mui/material";
import { reorderQuestions, setQuestions } from "@root/questions/actions";
import { useQuestionsStore } from "@root/questions/store";
2023-11-14 20:15:52 +00:00
import { useCurrentQuiz } from "@root/quizes/hooks";
import { isAxiosError } from "axios";
import { enqueueSnackbar } from "notistack";
import type { DropResult } from "react-beautiful-dnd";
import { DragDropContext, Droppable } from "react-beautiful-dnd";
import useSWR from "swr";
import DraggableListItem from "./DraggableListItem";
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 = useQuestionsStore(state => state.questions);
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
};
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
};