修改操作定时任务交互

This commit is contained in:
whyour 2021-04-05 22:49:36 +08:00
parent 21d164218f
commit 0333365762

View File

@ -110,7 +110,7 @@ const Crontab = () => {
<Tooltip title="运行"> <Tooltip title="运行">
<a <a
onClick={() => { onClick={() => {
runCron(record); runCron(record, index);
}} }}
> >
<PlayCircleOutlined /> <PlayCircleOutlined />
@ -125,7 +125,7 @@ const Crontab = () => {
<FileTextOutlined /> <FileTextOutlined />
</a> </a>
</Tooltip> </Tooltip>
<MoreBtn key="more" record={record} /> <MoreBtn key="more" record={record} index={index} />
</Space> </Space>
), ),
}, },
@ -134,7 +134,7 @@ const Crontab = () => {
const [width, setWdith] = useState('100%'); const [width, setWdith] = useState('100%');
const [marginLeft, setMarginLeft] = useState(0); const [marginLeft, setMarginLeft] = useState(0);
const [marginTop, setMarginTop] = useState(-72); const [marginTop, setMarginTop] = useState(-72);
const [value, setValue] = useState(); const [value, setValue] = useState<any[]>([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [isModalVisible, setIsModalVisible] = useState(false); const [isModalVisible, setIsModalVisible] = useState(false);
const [editedCron, setEditedCron] = useState(); const [editedCron, setEditedCron] = useState();
@ -155,12 +155,12 @@ const Crontab = () => {
setIsModalVisible(true); setIsModalVisible(true);
}; };
const editCron = (record: any) => { const editCron = (record: any, index: number) => {
setEditedCron(record); setEditedCron(record);
setIsModalVisible(true); setIsModalVisible(true);
}; };
const delCron = (record: any) => { const delCron = (record: any, index: number) => {
Modal.confirm({ Modal.confirm({
title: '确认删除', title: '确认删除',
content: ( content: (
@ -176,7 +176,9 @@ const Crontab = () => {
notification.success({ notification.success({
message: '删除成功', message: '删除成功',
}); });
getCrons(); const result = [...value];
result.splice(index, 1);
setValue(result);
} else { } else {
notification.error({ notification.error({
message: data, message: data,
@ -190,7 +192,7 @@ const Crontab = () => {
}); });
}; };
const runCron = (record: any) => { const runCron = (record: any, index: number) => {
Modal.confirm({ Modal.confirm({
title: '确认运行', title: '确认运行',
content: ( content: (
@ -202,7 +204,19 @@ const Crontab = () => {
request request
.get(`${config.apiPrefix}crons/${record._id}/run`) .get(`${config.apiPrefix}crons/${record._id}/run`)
.then((data: any) => { .then((data: any) => {
getCrons(); if (data.code === 200) {
const result = [...value];
result.splice(index, 1, {
...record,
status: CrontabStatus.running,
});
console.log(result);
setValue(result);
} else {
notification.error({
message: data,
});
}
}); });
}, },
onCancel() { onCancel() {
@ -211,12 +225,15 @@ const Crontab = () => {
}); });
}; };
const enabledOrDisabledCron = (record: any) => { const enabledOrDisabledCron = (record: any, index: number) => {
Modal.confirm({ Modal.confirm({
title: '确认禁用', title: `确认${
record.status === CrontabStatus.disabled ? '启用' : '禁用'
}`,
content: ( content: (
<> <>
<Text type="warning">{record.name}</Text> {record.status === CrontabStatus.disabled ? '启用' : '禁用'}
<Text type="warning">{record.name}</Text>
</> </>
), ),
onOk() { onOk() {
@ -232,9 +249,20 @@ const Crontab = () => {
.then((data: any) => { .then((data: any) => {
if (data.code === 200) { if (data.code === 200) {
notification.success({ notification.success({
message: '禁用成功', message: `${
record.status === CrontabStatus.disabled ? '启用' : '禁用'
}`,
}); });
getCrons(); const newStatus =
record.status === CrontabStatus.disabled
? CrontabStatus.idle
: CrontabStatus.disabled;
const result = [...value];
result.splice(index, 1, {
...record,
status: newStatus,
});
setValue(result);
} else { } else {
notification.error({ notification.error({
message: data, message: data,
@ -267,12 +295,13 @@ const Crontab = () => {
const MoreBtn: React.FC<{ const MoreBtn: React.FC<{
record: any; record: any;
}> = ({ record }) => ( index: number;
}> = ({ record, index }) => (
<Dropdown <Dropdown
arrow arrow
trigger={['click', 'hover']} trigger={['click', 'hover']}
overlay={ overlay={
<Menu onClick={({ key }) => action(key, record)}> <Menu onClick={({ key }) => action(key, record, index)}>
<Menu.Item key="edit" icon={<EditOutlined />}> <Menu.Item key="edit" icon={<EditOutlined />}>
</Menu.Item> </Menu.Item>
@ -304,16 +333,16 @@ const Crontab = () => {
</Dropdown> </Dropdown>
); );
const action = (key: string | number, record: any) => { const action = (key: string | number, record: any, index: number) => {
switch (key) { switch (key) {
case 'edit': case 'edit':
editCron(record); editCron(record, index);
break; break;
case 'enableordisable': case 'enableordisable':
enabledOrDisabledCron(record); enabledOrDisabledCron(record, index);
break; break;
case 'delete': case 'delete':
delCron(record); delCron(record, index);
break; break;
default: default:
break; break;