import React, { useState, useEffect } from "react"; import { Space, Radio, Button, Drawer, Form, Cascader, Input, Modal, message, Image, Tooltip, } from "antd"; import styles from "./update.module.less"; import { course, department } from "../../../api/index"; import { UploadImageButton } from "../../../compenents"; import { getHost } from "../../../utils/index"; const { confirm } = Modal; interface PropInterface { id: number; open: boolean; onCancel: () => void; } interface Option { value: string | number; label: string; children?: Option[]; } export const CourseUpdate: React.FC = ({ id, open, onCancel, }) => { const [form] = Form.useForm(); const defaultThumb1 = getHost() + "thumb/thumb1.png"; const defaultThumb2 = getHost() + "thumb/thumb2.png"; const defaultThumb3 = getHost() + "thumb/thumb3.png"; const [loading, setLoading] = useState(true); const [departments, setDepartments] = useState([]); const [categories, setCategories] = useState([]); const [thumb, setThumb] = useState(""); const [type, setType] = useState("open"); useEffect(() => { if (id === 0) { return; } getCategory(); }, [id]); 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); } getParams(categories); }); }; const getParams = (cats: any) => { department.departmentList().then((res: any) => { const departments = res.data.departments; if (JSON.stringify(departments) !== "{}") { const new_arr: Option[] = checkArr(departments, 0); setDepartments(new_arr); } getDetail(departments, cats); }); }; const getDetail = (deps: any, cats: any) => { course.course(id).then((res: any) => { let box = res.data.dep_ids; let depIds: any[] = []; let type = res.data.dep_ids.length > 0 ? "elective" : "open"; if (box.length > 0) { for (let i = 0; i < box.length; i++) { let item = checkChild(deps, box[i]); let arr: any[] = []; if (item === undefined) { arr.push(box[i]); } else if (item.parent_chain === "") { arr.push(box[i]); } else { let new_arr = item.parent_chain.split(","); new_arr.map((num: any) => { arr.push(Number(num)); }); arr.push(box[i]); } depIds.push(arr); } } else { depIds = res.data.dep_ids; } let chapterType = res.data.chapters.length > 0 ? 1 : 0; form.setFieldsValue({ title: res.data.course.title, thumb: res.data.course.thumb, dep_ids: depIds, category_ids: res.data.category_ids, isRequired: res.data.course.isRequired, type: type, short_desc: res.data.course.short_desc, hasChapter: chapterType, }); setType(type); setThumb(res.data.course.thumb); }); }; const checkChild = (departments: any[], id: number) => { for (let key in departments) { for (let i = 0; i < departments[key].length; i++) { if (departments[key][i].id === id) { return departments[key][i]; } } } }; 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[] = []; if (type === "elective") { for (let i = 0; i < values.dep_ids.length; i++) { dep_ids.push(values.dep_ids[i][values.dep_ids[i].length - 1]); } } course .updateCourse( id, values.title, values.thumb, values.short_desc, 1, values.isRequired, dep_ids, values.category_ids, [], [] ) .then((res: any) => { message.success("保存成功!"); onCancel(); }); }; const onFinishFailed = (errorInfo: any) => { console.log("Failed:", errorInfo); }; const getType = (e: any) => { setType(e.target.value); }; return ( <> } width={634} >
必修课 选修课 公开课 部门课 {type === "elective" && ( )}
{ setThumb(defaultThumb1); form.setFieldsValue({ thumb: defaultThumb1, }); }} >
{ setThumb(defaultThumb2); form.setFieldsValue({ thumb: defaultThumb2, }); }} >
{ setThumb(defaultThumb3); form.setFieldsValue({ thumb: defaultThumb3, }); }} >
{ setThumb(url); form.setFieldsValue({ thumb: url }); }} > (推荐尺寸:400x300px)
); };