diff --git a/src/pages/course/compenents/create.module.less b/src/pages/course/compenents/create.module.less new file mode 100644 index 0000000..e69de29 diff --git a/src/pages/course/compenents/create.tsx b/src/pages/course/compenents/create.tsx new file mode 100644 index 0000000..b7d27a1 --- /dev/null +++ b/src/pages/course/compenents/create.tsx @@ -0,0 +1,201 @@ +import React, { useState, useEffect } from "react"; +import { + Space, + Switch, + Button, + Drawer, + Form, + Cascader, + Input, + message, +} from "antd"; +import styles from "./create.module.less"; +import { course, department } from "../../../api/index"; +import { UploadImageButton } from "../../../compenents"; + +interface PropInterface { + open: boolean; + onCancel: () => void; +} + +interface Option { + value: string | number; + label: string; + children?: Option[]; +} + +export const CourseCreate: React.FC = ({ open, onCancel }) => { + const [form] = Form.useForm(); + const [loading, setLoading] = useState(true); + const [departments, setDepartments] = useState([]); + const [categories, setCategories] = useState([]); + const [thumb, setThumb] = useState(""); + + useEffect(() => { + getParams(); + getCategory(); + }, []); + + useEffect(() => { + form.setFieldsValue({ + title: "", + thumb: "", + dep_ids: [], + category_ids: [], + }); + setThumb(""); + }, [form, open]); + + const getParams = () => { + department.departmentList().then((res: any) => { + const departments = res.data.departments; + if (JSON.stringify(departments) !== "{}") { + const new_arr: Option[] = checkArr(departments, 0); + setDepartments(new_arr); + } + }); + }; + + const getCategory = () => { + course.createCourse().then((res: any) => { + const categories = res.data.categories; + if (JSON.stringify(categories) !== "{}") { + const new_arr: Option[] = checkArr(categories, 0); + setCategories(new_arr); + } + }); + }; + + const checkArr = (departments: any[], id: number) => { + const arr = []; + for (let i = 0; i < departments[id].length; i++) { + if (!departments[departments[id][i].id]) { + arr.push({ + label: departments[id][i].name, + value: departments[id][i].id, + }); + } else { + const new_arr: Option[] = checkArr(departments, departments[id][i].id); + arr.push({ + label: departments[id][i].name, + value: departments[id][i].id, + children: new_arr, + }); + } + } + return arr; + }; + + const onFinish = (values: any) => { + let dep_ids: any[] = []; + for (let i = 0; i < values.dep_ids.length; i++) { + dep_ids.push(values.dep_ids[i][values.dep_ids[i].length - 1]); + } + let category_ids: any[] = []; + for (let j = 0; j < values.category_ids.length; j++) { + category_ids.push( + values.category_ids[j][values.category_ids[j].length - 1] + ); + } + course + .storeCourse(values.title, values.thumb, 1, dep_ids, category_ids) + .then((res: any) => { + message.success("保存成功!"); + onCancel(); + }); + }; + + const onFinishFailed = (errorInfo: any) => { + console.log("Failed:", errorInfo); + }; + + return ( + <> + + + + + } + width={634} + > +
+
+ + + + + + + + + + +
+
+ { + setThumb(url); + form.setFieldsValue({ thumb: url }); + }} + > +
+ {thumb && ( + + )} +
+
+
+
+
+ + ); +}; diff --git a/src/pages/course/index.tsx b/src/pages/course/index.tsx index 34d96be..381f147 100644 --- a/src/pages/course/index.tsx +++ b/src/pages/course/index.tsx @@ -18,6 +18,7 @@ import { dateFormat } from "../../utils/index"; import { Link, useNavigate } from "react-router-dom"; import { TreeDepartment, TreeCategory, PerButton } from "../../compenents"; import type { TabsProps } from "antd"; +import { CourseCreate } from "./compenents/create"; const { confirm } = Modal; @@ -46,6 +47,10 @@ export const CoursePage = () => { const [tabKey, setTabKey] = useState(1); const [pureTotal, setPureTotal] = useState(0); + const [createVisible, setCreateVisible] = useState(false); + const [updateVisible, setUpdateVisible] = useState(false); + const [cid, setCid] = useState(0); + const items: TabsProps["items"] = [ { key: "1", @@ -234,17 +239,15 @@ export const CoursePage = () => {
{selLabel}
- - } - p="course" - onClick={() => null} - disabled={null} - /> - + } + p="course" + onClick={() => setCreateVisible(true)} + disabled={null} + />
@@ -282,6 +285,13 @@ export const CoursePage = () => { pagination={paginationProps} rowKey={(record) => record.id} /> + { + setCreateVisible(false); + setRefresh(!refresh); + }} + />