import { DeleteOutlined, EditFilled, EditOutlined, FieldTimeOutlined, FileAddOutlined, InfoCircleOutlined, } from "@ant-design/icons"; import { Button, Card, DatePicker, Image, Input, Popconfirm, Space, Switch, Table, Modal, Tooltip, Form, } from "antd"; import dayjs, { Dayjs } from "dayjs"; import { useEffect, useState } from "react"; import { removeCourse, selectChapterList, selectCourseList, updateChapter, updateCourse, createChapter, removeChapter, } from "../../../api"; import { IChapter } from "../../../api/dto"; import { useMount } from "../../../hooks"; import "./index.less"; interface IEditItem { key: string; value: string | boolean | number; } const defaultEditItem = { key: "", value: "" }; export default function List() { const [courseList, setCourseList] = useState([]); const [chapterList, setChapterList] = useState([]); const [editChapterItem, setEditChapterItem] = useState(defaultEditItem); const [editCourseItem, setEditCourseItem] = useState(defaultEditItem); const [addChapterForm] = Form.useForm(); const onConfirmEditCourseItem = (record: any) => { const { key, value } = editCourseItem; updateCourse({ ...record, [key]: value }).then((res: any) => { if (res?.code === 10000) { renderCourseTable(); setEditCourseItem(defaultEditItem); } }); }; const onConfirmEditChapterItem = (record: any) => { const { key, value } = editChapterItem; updateChapter({ ...record, [key]: value }).then((res: any) => { if (res?.code === 10000) { const { chapter_course_id } = record; renderChapterList(chapter_course_id); setEditChapterItem(defaultEditItem); } }); }; const onConfirmDeleteCourseItem = (record: any) => { const { course_id } = record; removeCourse({ course_id }).then((res: any) => { if (res?.code === 10000) renderCourseTable(); }); }; const onAddChapter = (record: any) => { const { course_id: chapter_course_id } = record; Modal.info({ title: "添加章节", icon: , content: (
), onOk: async () => { const origin = addChapterForm.getFieldValue("chapterList"); const chapterList = origin .split("\n") .filter((i: string) => i.replace(/\s/, "").length > 0) .map((row: string) => { const [chapter_level, chapter_title, chapter_file_id] = row.split("|"); return !chapter_file_id ? { chapter_level, chapter_title, chapter_course_id } : { chapter_level, chapter_title, chapter_file_id, chapter_course_id, }; }); createChapter(chapterList).then((res: any) => { if (res?.code === 10000) renderChapterList(chapter_course_id); }); }, okText: "确认", cancelText: "取消", }); }; const onConfirmDeleteChapterItem = (record: any) => { const { chapter_id, chapter_course_id } = record; removeChapter({ chapter_id }).then((res: any) => { if (res?.code === 10000) renderChapterList(chapter_course_id); }); }; const columns = [ { title: "课程", dataIndex: "course_title", key: "course_title", render: (_: any, record: any) => { return ( {record.course_title} setEditCourseItem({ key: "course_title", value: e.target.value, }) } /> } onConfirm={() => onConfirmEditCourseItem(record)} okText="确定" cancelText="取消" > ); }, }, ]; useEffect(() => {}, [courseList]); const renderCourseTable = () => { selectCourseList({ all: true }).then((res) => { const { data } = res; setCourseList(data.map((i: any) => ({ ...i, key: i.course_id }))); }); }; useMount(() => { renderCourseTable(); }); const expandedRowRender = () => { const col: any = [ { title: "级别", dataIndex: "chapter_level", key: "chapter_level", render: (_: any, record: any) => { return ( {record.chapter_level} setEditChapterItem({ key: "chapter_level", value: e.target.value, }) } /> } onConfirm={() => onConfirmEditChapterItem(record)} okText="确定" cancelText="取消" > ); }, }, ]; return ; }; const renderChapterList = (chapter_course_id: string) => { selectChapterList({ chapter_course_id }).then((res: any) => { const { data = [] } = res; setChapterList(data.map((i: any) => ({ ...i, key: i.chapter_id }))); }); }; const onExpand = (expand: boolean, record: any) => { if (expand) { const { course_id: chapter_course_id } = record; renderChapterList(chapter_course_id); } }; return (
); }