添加标签功能 (#1026)

* 添加标签功能
This commit is contained in:
kilo5hz
2022-01-07 22:01:13 +08:00
committed by GitHub
parent 5a5f4b8065
commit 89ed8527d6
7 changed files with 262 additions and 44 deletions
+53 -15
View File
@@ -11,6 +11,7 @@ import {
Menu,
Typography,
Input,
Popover,
} from 'antd';
import {
ClockCircleOutlined,
@@ -30,7 +31,7 @@ import {
import config from '@/utils/config';
import { PageContainer } from '@ant-design/pro-layout';
import { request } from '@/utils/http';
import CronModal from './modal';
import CronModal,{ CronLabelModal } from './modal';
import CronLogModal from './logModal';
import cron_parser from 'cron-parser';
import { diffTime } from '@/utils/date';
@@ -77,20 +78,39 @@ const Crontab = ({ headerStyle, isPhone }: any) => {
width: 150,
align: 'center' as const,
render: (text: string, record: any) => (
<a
onClick={() => {
goToScriptManager(record);
}}
>
{record.name || record.id}{' '}
{record.isPinned ? (
<span>
<PushpinOutlined />
</span>
) : (
''
)}
</a>
<>
<a
onClick={() => {
goToScriptManager(record);
}}
>
{record.name || record._id}{' '}
{record.isPinned ? (
<span>
<PushpinOutlined />
</span>
) : (
''
)}
</a>
<span>
{record.labels?.length > 0 && record.labels[0] !== '' ?
<Popover placement='right' trigger={isPhone ? 'click' : 'hover'}
content={
<div>
{record.labels?.map((label: string, i: number) => (
<Tag color="blue"
onClick={() => { onSearch(`label:${label}`) }}>
{label}
</Tag>
))}
</div>
}>
<Tag color="blue">{record.labels[0]}</Tag>
</Popover>
: ''}
</span>
</>
),
sorter: {
compare: (a: any, b: any) => a.name.localeCompare(b.name),
@@ -325,6 +345,7 @@ const Crontab = ({ headerStyle, isPhone }: any) => {
const [value, setValue] = useState<any[]>([]);
const [loading, setLoading] = useState(true);
const [isModalVisible, setIsModalVisible] = useState(false);
const [isLabelModalVisible, setisLabelModalVisible] = useState(false);
const [editedCron, setEditedCron] = useState();
const [searchText, setSearchText] = useState('');
const [isLogModalVisible, setIsLogModalVisible] = useState(false);
@@ -853,6 +874,13 @@ const Crontab = ({ headerStyle, isPhone }: any) => {
>
</Button>
<Button
type="primary"
onClick={() => setisLabelModalVisible(true)}
style={{ marginLeft: 8, marginRight: 8 }}
>
</Button>
<span style={{ marginLeft: 8 }}>
<a>{selectedRowIds?.length}</a>
@@ -893,6 +921,16 @@ const Crontab = ({ headerStyle, isPhone }: any) => {
handleCancel={handleCancel}
cron={editedCron}
/>
<CronLabelModal
visible={isLabelModalVisible}
handleCancel={(needUpdate?: boolean) => {
setisLabelModalVisible(false);
if (needUpdate) {
getCrons();
}
}}
ids={selectedRowIds}
/>
</PageContainer>
);
};
+83 -2
View File
@@ -1,5 +1,5 @@
import React, { useEffect, useState } from 'react';
import { Modal, message, Input, Form } from 'antd';
import { Modal, message, Input, Form, Button } from 'antd';
import { request } from '@/utils/http';
import config from '@/utils/config';
import cronParse from 'cron-parser';
@@ -48,6 +48,9 @@ const CronModal = ({
form
.validateFields()
.then((values) => {
if (typeof values.labels === "string") {
values.labels = values.labels.split(/,|/);
}
handleOk(values);
})
.catch((info) => {
@@ -66,6 +69,9 @@ const CronModal = ({
<Form.Item name="name" label="名称">
<Input placeholder="请输入任务名称" />
</Form.Item>
<Form.Item name="labels" label="标签">
<Input placeholder="请输入任务标签" />
</Form.Item>
<Form.Item
name="command"
label="命令"
@@ -100,4 +106,79 @@ const CronModal = ({
);
};
export default CronModal;
const CronLabelModal = ({
ids,
handleCancel,
visible,
}: {
ids: Array<string>;
visible: boolean;
handleCancel: (needUpdate?: boolean) => void;
}) => {
const [form] = Form.useForm();
const [loading, setLoading] = useState(false);
const update = async (action: string) => {
form
.validateFields()
.then(async (values) => {
if (typeof values.labels === "string") {
values.labels = values.labels.split(/,|/);
}
setLoading(true);
const payload = { ids, labels: values.labels };
const { code, data } = await request.put(`${config.apiPrefix}crons/${action}`, {
data: payload,
});
if (code === 200) {
message.success(action === 'addLabels' ? '添加Labels成功' : '删除Labels成功');
} else {
message.error(data);
}
setLoading(false);
handleCancel(true);
})
.catch((info) => {
console.log('Validate Failed:', info);
});
}
useEffect(() => {
form.resetFields();
}, [ids, visible]);
const buttons = [
<Button onClick={() => handleCancel(false)} key="test">
</Button>,
<Button type="primary" danger onClick={() => update('removeLabels')}>
</Button>,
<Button type="primary" onClick={() => update('addLabels')}>
</Button>
];
return (
<Modal
title='批量修改标签'
visible={visible}
footer={buttons}
forceRender
onCancel={() => handleCancel(false)}
confirmLoading={loading}
>
<Form
form={form}
layout="vertical"
name="form_in_label_modal"
>
<Form.Item name="labels" label="标签">
<Input placeholder="请输入任务标签" />
</Form.Item>
</Form>
</Modal>
);
};
export { CronModal as default, CronLabelModal }
+4 -2
View File
@@ -113,17 +113,19 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
const initGetScript = () => {
const { p, s } = history.location.query as any;
if (s) {
const vkey = `${p}/${s}`;
const obj = {
node: {
title: s,
value: s,
key: p ? `${p}/${s}` : s,
key: p ? vkey : s,
parent: p,
},
};
setExpandedKeys([p]);
onTreeSelect([`${p}/${s}`], obj);
onTreeSelect([vkey], obj);
}
history.push('/script');
};
const onSelect = (value: any, node: any) => {