mirror of
https://github.com/PlayEdu/backend
synced 2025-06-19 01:57:04 +08:00
147 lines
4.2 KiB
TypeScript
147 lines
4.2 KiB
TypeScript
import React, { useState, useEffect } from "react";
|
|
import { Row, Col, Form, Input, Cascader, Button, message } from "antd";
|
|
import styles from "./create.module.less";
|
|
import { department } from "../../api/index";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { BackBartment } from "../../compenents";
|
|
|
|
interface Option {
|
|
value: string | number;
|
|
label: string;
|
|
children?: Option[];
|
|
}
|
|
|
|
export const DepartmentCreatePage: React.FC = () => {
|
|
const navigate = useNavigate();
|
|
const [form] = Form.useForm();
|
|
const [loading, setLoading] = useState<boolean>(true);
|
|
const [categories, setCategories] = useState<any>([]);
|
|
const [parent_id, setParentId] = useState<number>(0);
|
|
|
|
useEffect(() => {
|
|
getParams();
|
|
}, []);
|
|
|
|
const getParams = () => {
|
|
department.createDepartment().then((res: any) => {
|
|
const departments = res.data.departments;
|
|
const new_arr: Option[] = checkArr(departments, 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) => {
|
|
department
|
|
.storeDepartment(values.name, parent_id || 0, values.sort)
|
|
.then((res: any) => {
|
|
message.success("保存成功!");
|
|
navigate(-1);
|
|
});
|
|
};
|
|
|
|
const onFinishFailed = (errorInfo: any) => {
|
|
console.log("Failed:", errorInfo);
|
|
};
|
|
|
|
const handleChange = (value: any) => {
|
|
if (value !== undefined) {
|
|
let it = value[value.length - 1];
|
|
setParentId(it);
|
|
} else {
|
|
setParentId(0);
|
|
}
|
|
};
|
|
|
|
const displayRender = (label: any, selectedOptions: any) => {
|
|
return label[label.length - 1];
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<Row className="playedu-main-body">
|
|
<Col>
|
|
<div className="float-left mb-24">
|
|
<BackBartment title="新建部门" />
|
|
</div>
|
|
<div className="float-left">
|
|
<Form
|
|
form={form}
|
|
name="basic"
|
|
labelCol={{ span: 8 }}
|
|
wrapperCol={{ span: 16 }}
|
|
style={{ width: 600 }}
|
|
initialValues={{ remember: true }}
|
|
onFinish={onFinish}
|
|
onFinishFailed={onFinishFailed}
|
|
autoComplete="off"
|
|
>
|
|
<Form.Item label="父级" name="parent_id">
|
|
<Cascader
|
|
style={{ width: 300 }}
|
|
allowClear
|
|
placeholder="请选择部门"
|
|
onChange={handleChange}
|
|
options={categories}
|
|
changeOnSelect
|
|
expand-trigger="hover"
|
|
displayRender={displayRender}
|
|
/>
|
|
</Form.Item>
|
|
<Form.Item
|
|
label="部门名"
|
|
name="name"
|
|
rules={[{ required: true, message: "请输入部门名!" }]}
|
|
>
|
|
<Input style={{ width: 300 }} placeholder="请输入部门名" />
|
|
</Form.Item>
|
|
<Form.Item
|
|
label="Sort"
|
|
name="sort"
|
|
rules={[{ required: true, message: "请输入Sort!" }]}
|
|
>
|
|
<Input
|
|
type="number"
|
|
style={{ width: 300 }}
|
|
placeholder="请输入Sort"
|
|
/>
|
|
</Form.Item>
|
|
<Form.Item wrapperCol={{ offset: 8, span: 16 }}>
|
|
<Button type="primary" htmlType="submit">
|
|
保存
|
|
</Button>
|
|
<Button
|
|
className="ml-15"
|
|
htmlType="button"
|
|
onClick={() => navigate(-1)}
|
|
>
|
|
取消
|
|
</Button>
|
|
</Form.Item>
|
|
</Form>
|
|
</div>
|
|
</Col>
|
|
</Row>
|
|
</>
|
|
);
|
|
};
|