From 74b618e0c1c357ab00e97b9bea140ce3ce1b8be7 Mon Sep 17 00:00:00 2001 From: ditiqi Date: Thu, 6 Feb 2025 19:23:38 +0800 Subject: [PATCH] add --- .../form/CourseContentForm/LectureList.tsx | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 apps/web/src/components/models/course/editor/form/CourseContentForm/LectureList.tsx diff --git a/apps/web/src/components/models/course/editor/form/CourseContentForm/LectureList.tsx b/apps/web/src/components/models/course/editor/form/CourseContentForm/LectureList.tsx new file mode 100644 index 0000000..7ca5171 --- /dev/null +++ b/apps/web/src/components/models/course/editor/form/CourseContentForm/LectureList.tsx @@ -0,0 +1,150 @@ +import { + PlusOutlined, + DragOutlined, + DeleteOutlined, + CaretRightOutlined, + SaveOutlined, +} from "@ant-design/icons"; +import { + Form, + Alert, + Button, + Input, + Select, + Space, + Collapse, + message, +} from "antd"; +import React, { useCallback, useEffect, useState } from "react"; +import { + DndContext, + closestCenter, + KeyboardSensor, + PointerSensor, + useSensor, + useSensors, + DragEndEvent, +} from "@dnd-kit/core"; +import { api, emitDataChange } from "@nice/client"; +import { + arrayMove, + SortableContext, + sortableKeyboardCoordinates, + useSortable, + verticalListSortingStrategy, +} from "@dnd-kit/sortable"; +import { CSS } from "@dnd-kit/utilities"; +import QuillEditor from "@web/src/components/common/editor/quill/QuillEditor"; +import { TusUploader } from "@web/src/components/common/uploader/TusUploader"; +import { Lecture, LectureType, PostType } from "@nice/common"; +import { useCourseEditor } from "../../context/CourseEditorContext"; +import { usePost } from "@nice/client"; +import toast from "react-hot-toast"; +import { CourseContentFormHeader } from "./CourseContentFormHeader"; +import { CourseSectionEmpty } from "./CourseSectionEmpty"; +import { LectureData, SectionData } from "./interface"; +import { SortableLecture } from "./SortableLecture"; + +interface LectureListProps { + field: SectionData; + sectionId: string; +} + +export const LectureList: React.FC = ({ + field, + sectionId, +}) => { + const { softDeleteByIds } = usePost(); + const { data: lectures = [], isLoading } = ( + api.post.findMany as any + ).useQuery( + { + where: { + parentId: sectionId, + type: PostType.LECTURE, + deletedAt: null, + }, + }, + { + enabled: !!sectionId, + } + ); + useEffect(() => { + if (lectures && !isLoading) { + setItems(lectures); + } + }, [lectures, isLoading]); + const [items, setItems] = useState(lectures); + + const sensors = useSensors( + useSensor(PointerSensor), + useSensor(KeyboardSensor, { + coordinateGetter: sortableKeyboardCoordinates, + }) + ); + + 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 ( +
+
{ + console.log(lectures); + }}> + 123 +
+ + + {items.map((lecture) => ( + { + if (lecture?.id) { + await softDeleteByIds.mutateAsync({ + ids: [lecture.id], + }); + } + setItems(lectures); + }} + sectionFieldKey={sectionId} + /> + ))} + + + +
+ ); +};