diff --git a/src/pages/resource/courseware/compenents/update-dialog/index.tsx b/src/pages/resource/courseware/compenents/update-dialog/index.tsx new file mode 100644 index 0000000..69b82de --- /dev/null +++ b/src/pages/resource/courseware/compenents/update-dialog/index.tsx @@ -0,0 +1,153 @@ +import React, { useState, useEffect } from "react"; +import { Modal, Form, Input, message, TreeSelect } from "antd"; +import { resource, resourceCategory } from "../../../../../api/index"; + +interface PropInterface { + id: number; + open: boolean; + onCancel: () => void; + onSuccess: () => void; +} + +export const CoursewareUpdateDialog: React.FC = ({ + id, + open, + onCancel, + onSuccess, +}) => { + const [form] = Form.useForm(); + const [loading, setLoading] = useState(true); + const [categories, setCategories] = useState([]); + + useEffect(() => { + if (id === 0) { + return; + } + if (open) { + getCategory(); + getDetail(); + } + }, [id, open]); + + const getCategory = () => { + resourceCategory.resourceCategoryList().then((res: any) => { + const categories = res.data.categories; + if (JSON.stringify(categories) !== "{}") { + const new_arr: any = checkArr(categories, 0, null); + setCategories(new_arr); + } + }); + }; + + const getDetail = () => { + resource.videoDetail(id).then((res: any) => { + let data = res.data.resources; + form.setFieldsValue({ + name: data.name, + category_id: res.data.category_ids, + }); + }); + }; + + const checkArr = (departments: any[], id: number, counts: any) => { + const arr = []; + for (let i = 0; i < departments[id].length; i++) { + if (!departments[departments[id][i].id]) { + arr.push({ + title: departments[id][i].name, + value: departments[id][i].id, + }); + } else { + const new_arr: any = checkArr( + departments, + departments[id][i].id, + counts + ); + arr.push({ + title: departments[id][i].name, + value: departments[id][i].id, + children: new_arr, + }); + } + } + return arr; + }; + + const onFinish = (values: any) => { + if (loading) { + return; + } + setLoading(true); + if (Array.isArray(values.category_id)) { + values.category_id = values.category_id[0]; + } + resource + .videoUpdate(id, values) + .then((res: any) => { + setLoading(false); + message.success("保存成功!"); + onSuccess(); + }) + .catch((e) => { + setLoading(false); + }); + }; + + const onFinishFailed = (errorInfo: any) => { + console.log("Failed:", errorInfo); + }; + + return ( + <> + form.submit()} + onCancel={() => onCancel()} + maskClosable={false} + > +
+
+ + + + + + +
+
+
+ + ); +}; diff --git a/src/pages/resource/courseware/index.tsx b/src/pages/resource/courseware/index.tsx index b51ee2a..a889b38 100644 --- a/src/pages/resource/courseware/index.tsx +++ b/src/pages/resource/courseware/index.tsx @@ -16,12 +16,8 @@ import { useLocation } from "react-router-dom"; import { DownOutlined, ExclamationCircleFilled } from "@ant-design/icons"; import type { ColumnsType } from "antd/es/table"; import { dateFormat } from "../../../utils/index"; -import { - TreeCategory, - DurationText, - PerButton, - UploadCoursewareButton, -} from "../../../compenents"; +import { TreeCategory, UploadCoursewareButton } from "../../../compenents"; +import { CoursewareUpdateDialog } from "./compenents/update-dialog"; const { confirm } = Modal; @@ -378,6 +374,15 @@ const ResourceCoursewarePage = () => { )} + setUpdateVisible(false)} + onSuccess={() => { + setUpdateVisible(false); + setRefresh(!refresh); + }} + > );