import { useEffect, useRef, useState } from 'react'; import { Drawer, Form, Input, InputNumber, Radio, Button, Space, Card } from 'antd'; import { App } from 'antd'; import { getRole, createRole, updateRole } from '@/api/system/role'; import type { SystemRole } from '@/api/system/role'; import { useTranslation } from 'react-i18next'; const DEFAULT_FORM = { sort: 0, status: 0 }; interface RoleDrawerProps { open: boolean; id: number | null; onSuccess: () => void; onClose: () => void; } const RoleDrawer: React.FC = ({ open, id, onSuccess, onClose }) => { const { t } = useTranslation('system/role'); const { t: tc } = useTranslation('common'); const { message } = App.useApp(); const [form] = Form.useForm(); const [confirmLoading, setConfirmLoading] = useState(false); // 保留服务端原始字段,避免编辑时丢掉未在表单里出现的字段 const originData = useRef>({}); const isEdit = id != null; useEffect(() => { if (open) { form.resetFields(); originData.current = {}; if (id) { getRole(id).then(({ data }) => { originData.current = { ...data }; form.setFieldsValue(data); }).catch(() => { }); } else { form.setFieldsValue(DEFAULT_FORM); } } }, [open, id, form]); const handleOk = async () => { let values: SystemRole; try { values = await form.validateFields(); } catch { return; } setConfirmLoading(true); try { const payload = { ...originData.current, ...values, ...(id != null ? { id } : {}) } as SystemRole; if (isEdit) { await updateRole(payload); message.success(tc('success')); } else { await createRole(payload); message.success(tc('success')); } onSuccess(); } finally { setConfirmLoading(false); } }; return ( } >
{tc('enable')} {tc('disable')}
); }; export default RoleDrawer;