diff --git a/apps/web/src/components/models/course/editor/form/CourseContentForm/CourseContentForm.tsx b/apps/web/src/components/models/course/editor/form/CourseContentForm/CourseContentForm.tsx new file mode 100644 index 0000000..ee0916a --- /dev/null +++ b/apps/web/src/components/models/course/editor/form/CourseContentForm/CourseContentForm.tsx @@ -0,0 +1,121 @@ +import { PlusOutlined } from "@ant-design/icons"; +import { Button } from "antd"; +import React, { useEffect, useState } from "react"; +import { + DndContext, + closestCenter, + KeyboardSensor, + PointerSensor, + useSensor, + useSensors, + DragEndEvent, +} from "@dnd-kit/core"; +import { api } from "@nice/client"; +import { + arrayMove, + SortableContext, + sortableKeyboardCoordinates, + verticalListSortingStrategy, +} from "@dnd-kit/sortable"; +import { PostType } from "@nice/common"; +import { useCourseEditor } from "../../context/CourseEditorContext"; +import { usePost } from "@nice/client"; +import { CourseContentFormHeader } from "./CourseContentFormHeader"; +import { CourseSectionEmpty } from "./CourseSectionEmpty"; +import { SortableSection } from "./SortableSection"; +import { LectureList } from "./LectureList"; + +const CourseContentForm: React.FC = () => { + const { editId } = useCourseEditor(); + const sensors = useSensors( + useSensor(PointerSensor), + useSensor(KeyboardSensor, { + coordinateGetter: sortableKeyboardCoordinates, + }) + ); + const { softDeleteByIds } = usePost(); + const { data: sections = [], isLoading } = api.post.findMany.useQuery( + { + where: { + parentId: editId, + type: PostType.SECTION, + deletedAt: null, + }, + }, + { + enabled: !!editId, + } + ); + + const [items, setItems] = useState(sections); + useEffect(() => { + if (sections && !isLoading) { + setItems(sections); + } + }, [sections]); + const handleDragEnd = (event: DragEndEvent) => { + const { active, over } = event; + if (!over || active.id === over.id) return; + + setItems((items) => { + const oldIndex = items.findIndex((item) => item.id === active.id); + const newIndex = items.findIndex((item) => item.id === over.id); + return arrayMove(items, oldIndex, newIndex); + }); + }; + + return ( +
+ + + {items.length === 0 ? ( + + ) : ( + + + {items?.map((section, index) => ( + { + if (section?.id) { + await softDeleteByIds.mutateAsync({ + ids: [section.id], + }); + } + setItems(sections); + }}> + + + ))} + + + )} + + +
+ ); +}; + +export default CourseContentForm;