Unverified Commit 0afc758e authored by Licho's avatar Licho Committed by GitHub

refactor: some trivial modify on dinky-web document component. (#2092)

* refactor: package uri wrong, and trivial optimization * refactor: some trivial modify on web. * Spotless Apply --------- Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent fadcbdbd
......@@ -21,7 +21,7 @@ import {l} from '@/utils/intl';
/**
* data source type
*/
export const DATA_SOURCE_TYPE: any = [
export const DATA_SOURCE_TYPE = [
{
type: 'MySQL',
}, {
......
......@@ -20,20 +20,20 @@
import {FormInstance} from "antd/es/form/hooks/useForm";
import {Values} from "async-validator";
import {
ProForm,
ProFormItem,
ProFormSelect,
ProFormSwitch,
ProFormText,
ProFormTextArea
ProForm,
ProFormItem,
ProFormSelect,
ProFormSwitch,
ProFormText,
ProFormTextArea
} from "@ant-design/pro-components";
import {FORM_LAYOUT_PUBLIC} from "@/services/constants";
import React, {useEffect, useState} from "react";
import React, {useState} from "react";
import {
DOCUMENT_CATEGORY,
DOCUMENT_FUNCTION_TYPE,
DOCUMENT_SUBTYPE,
VERSIONS
DOCUMENT_CATEGORY,
DOCUMENT_FUNCTION_TYPE,
DOCUMENT_SUBTYPE,
VERSIONS
} from "@/pages/RegCenter/Document/constans";
import {l} from "@/utils/intl";
import CodeEdit from "@/components/CustomEditor/CodeEdit";
......@@ -41,174 +41,129 @@ import {Document} from "@/types/RegCenter/data";
import {DefaultOptionType} from "rc-select/lib/Select";
type DocumentFormProps = {
values: Partial<Document>;
form: FormInstance<Values>
values: Partial<Document>;
form: FormInstance<Values>
}
const CodeEditProps = {
height: "25vh",
lineNumbers: "on",
height: "25vh",
lineNumbers: "on",
}
const DocumentForm: React.FC<DocumentFormProps> = (props) => {
const {values, form} = props;
/**
* status
*/
const [codeFillValue, setCodeFillValue] = useState<string>(values.fillValue || '');
const [categoryList, setCategoryList] = useState<DefaultOptionType []>([]);
const [typeList, setTypeList] = useState<DefaultOptionType []>([]);
const [subTypeList, setSubTypeList] = useState<DefaultOptionType []>([]);
const [versionOptions, setVersionOptions] = useState<DefaultOptionType []>([]);
useEffect(() => {
let versionOptions: DefaultOptionType [] = []
let categoryOptions: DefaultOptionType [] = []
let typeOptions: DefaultOptionType [] = []
let subTypeOptions: DefaultOptionType [] = []
/**
* generate version options
*/
VERSIONS.map((item) => {
return versionOptions.push({
label: item.text,
value: item.value
})
})
setVersionOptions(versionOptions)
/**
* generate category options
*/
DOCUMENT_CATEGORY.map((item) => {
return categoryOptions.push({
label: item.text,
value: item.value
})
})
setCategoryList(categoryOptions)
/**
* generate subType options
*/
DOCUMENT_SUBTYPE.map((item) => {
return subTypeOptions.push({
label: item.text,
value: item.value
})
})
setSubTypeList(subTypeOptions)
/**
* generate type options
*/
DOCUMENT_FUNCTION_TYPE.map((item) => {
return typeOptions.push({
label: item.text,
value: item.value
})
})
setTypeList(typeOptions)
}, [])
/**
* code editor change callback
* @param value
*/
const handleFillValueChange = (value: string) => {
setCodeFillValue(value);
}
/**
* form
* @constructor
*/
const documentFormRender = () => {
return (
<>
<ProFormText
name="name"
label={l('rc.doc.name')}
placeholder={l('rc.doc.namePlaceholder')}
rules={[{required: true, message: l('rc.doc.namePlaceholder')}]}
/>
<ProFormSelect
name="category"
label={l('rc.doc.category')}
rules={[{required: true, message: l('rc.doc.categoryPlaceholder')}]}
options={categoryList}
/>
<ProFormSelect
name="type"
label={l('rc.doc.functionType')}
rules={[{required: true, message: l('rc.doc.typePlaceholder')}]}
options={typeList}
/>
<ProFormSelect
name="subtype"
label={l('rc.doc.subFunctionType')}
rules={[{required: true, message: l('rc.doc.subTypePlaceholder')}]}
options={subTypeList}
/>
<ProFormTextArea
name="description"
label={l('rc.doc.description')}
placeholder={l('rc.doc.descriptionPlaceholder')}
/>
<ProFormItem
name="fillValue"
label={l('rc.doc.fillValue')}
tooltip={l('rc.doc.fillValuePlaceholder')}
rules={[{required: true, message: l('rc.doc.fillValueHelp')}]}
>
<CodeEdit
onChange={(value => {
handleFillValueChange(value)
})}
code={codeFillValue}
language={"sql"}
{...CodeEditProps}
/>
</ProFormItem>
<ProFormSelect
name="version"
label={l('rc.doc.version')}
rules={[{required: true, message: l('rc.doc.versionPlaceholder')}]}
options={versionOptions}
/>
<ProFormSwitch
name="enabled"
label={l('global.table.isEnable')}
checkedChildren={l('button.enable')} unCheckedChildren={l('button.disable')}
/>
</>
);
};
return <>
<ProForm
{...FORM_LAYOUT_PUBLIC}
form={form}
submitter={false}
layout={'horizontal'}
initialValues={values}
const {values, form} = props;
/**
* status
*/
const [codeFillValue, setCodeFillValue] = useState<string>(values.fillValue || '');
const [categoryList] = useState<DefaultOptionType []>(
DOCUMENT_CATEGORY.map((item) => ({label: item.text, value: item.value})));
const [typeList] = useState<DefaultOptionType []>(
DOCUMENT_FUNCTION_TYPE.map((item) => ({label: item.text, value: item.value})));
const [subTypeList] = useState<DefaultOptionType []>(
DOCUMENT_SUBTYPE.map((item) => ({label: item.text, value: item.value})));
const [versionOptions] = useState<DefaultOptionType []>(
VERSIONS.map((item) => ({label: item.text, value: item.value})));
/**
* code editor change callback
* @param value
*/
const handleFillValueChange = (value: string) => {
setCodeFillValue(value);
}
/**
* form
* @constructor
*/
const documentFormRender = () => {
return (
<>
<ProFormText
name="name"
label={l('rc.doc.name')}
placeholder={l('rc.doc.namePlaceholder')}
rules={[{required: true, message: l('rc.doc.namePlaceholder')}]}
/>
<ProFormSelect
name="category"
label={l('rc.doc.category')}
rules={[{required: true, message: l('rc.doc.categoryPlaceholder')}]}
options={categoryList}
/>
<ProFormSelect
name="type"
label={l('rc.doc.functionType')}
rules={[{required: true, message: l('rc.doc.typePlaceholder')}]}
options={typeList}
/>
<ProFormSelect
name="subtype"
label={l('rc.doc.subFunctionType')}
rules={[{required: true, message: l('rc.doc.subTypePlaceholder')}]}
options={subTypeList}
/>
<ProFormTextArea
name="description"
label={l('rc.doc.description')}
placeholder={l('rc.doc.descriptionPlaceholder')}
/>
<ProFormItem
name="fillValue"
label={l('rc.doc.fillValue')}
tooltip={l('rc.doc.fillValuePlaceholder')}
rules={[{required: true, message: l('rc.doc.fillValueHelp')}]}
>
{documentFormRender()}
</ProForm>
</>;
<CodeEdit
onChange={(value => {
handleFillValueChange(value)
})}
code={codeFillValue}
language={"sql"}
{...CodeEditProps}
/>
</ProFormItem>
<ProFormSelect
name="version"
label={l('rc.doc.version')}
rules={[{required: true, message: l('rc.doc.versionPlaceholder')}]}
options={versionOptions}
/>
<ProFormSwitch
name="enabled"
label={l('global.table.isEnable')}
checkedChildren={l('button.enable')} unCheckedChildren={l('button.disable')}
/>
</>
);
};
return <>
<ProForm
{...FORM_LAYOUT_PUBLIC}
form={form}
submitter={false}
layout={'horizontal'}
initialValues={values}
>
{documentFormRender()}
</ProForm>
</>;
};
export default DocumentForm;
\ No newline at end of file
export default DocumentForm;
......@@ -54,13 +54,6 @@ const DocumentModalForm: React.FC<DocumentModalProps> = (props) => {
values
} = props;
/**
* when modalVisible or values changed, set form values
*/
useEffect(() => {
form.setFieldsValue(values);
}, [modalVisible, values, form]);
/**
* handle cancel
*/
......@@ -73,12 +66,10 @@ const DocumentModalForm: React.FC<DocumentModalProps> = (props) => {
*/
const submitForm = async () => {
const fieldsValue = await form.validateFields();
await handleSubmit({...values, ...fieldsValue});
await handleCancel();
handleSubmit({...values, ...fieldsValue});
handleCancel();
};
return <>
<Modal
{...NORMAL_MODAL_OPTIONS}
......@@ -92,4 +83,4 @@ const DocumentModalForm: React.FC<DocumentModalProps> = (props) => {
</>
}
export default DocumentModalForm;
\ No newline at end of file
export default DocumentModalForm;
......@@ -25,17 +25,14 @@ import {l} from "@/utils/intl";
import {Document} from "@/types/RegCenter/data";
import {queryList} from "@/services/api";
import {
DOCUMENT_CATEGORY,
DOCUMENT_CATEGORY_ENUMS,
DOCUMENT_FUNCTION_ENUMS,
DOCUMENT_FUNCTION_TYPE, DOCUMENT_SUBTYPE, DOCUMENT_SUBTYPE_ENUMS
DOCUMENT_CATEGORY,
DOCUMENT_CATEGORY_ENUMS,
DOCUMENT_FUNCTION_ENUMS,
DOCUMENT_FUNCTION_TYPE,
DOCUMENT_SUBTYPE,
DOCUMENT_SUBTYPE_ENUMS
} from "@/pages/RegCenter/Document/constans";
import {
API_CONSTANTS,
PROTABLE_OPTIONS_PUBLIC,
STATUS_ENUM,
STATUS_MAPPING,
} from "@/services/constants";
import {API_CONSTANTS, PROTABLE_OPTIONS_PUBLIC, STATUS_ENUM, STATUS_MAPPING,} from "@/services/constants";
import CodeShow from "@/components/CustomEditor/CodeShow";
import {handleAddOrUpdate, handleRemoveById, updateEnabled} from "@/services/BusinessCrud";
import TextArea from "antd/es/input/TextArea";
......@@ -48,203 +45,208 @@ import DocumentModalForm from "@/pages/RegCenter/Document/components/DocumentMod
const DocumentTableList: React.FC = () => {
const [modalVisible, handleModalVisible] = useState<boolean>(false);
const [updateModalVisible, handleUpdateModalVisible] = useState<boolean>(false);
const [formValues, setFormValues] = useState<Partial<Document>>({});
const actionRef = useRef<ActionType>();
const [loading, setLoading] = useState<boolean>(false);
const [drawerOpen, setDrawerOpen] = useState<boolean>(false);
const handleCancel = () => {
handleModalVisible(false);
handleUpdateModalVisible(false);
setDrawerOpen(false);
}
const executeAndCallbackRefresh = async (callback: () => void) => {
setLoading(true);
await callback();
setLoading(false);
handleCancel();
actionRef.current?.reload?.();
}
/**
* delete document by id
* @param id
*/
const handleDeleteSubmit = async (id: number) => {
await executeAndCallbackRefresh(async () => {
await handleRemoveById(API_CONSTANTS.DOCUMENT_DELETE, id);
})
}
/**
* enable or disable document
* @param value
*/
const handleChangeEnable = async (value: Partial<Document>) => {
await executeAndCallbackRefresh(async () => {
await updateEnabled(API_CONSTANTS.DOCUMENT_ENABLE, {id: value.id});
})
};
/**
* added or update document var
* @param value
*/
const handleAddOrUpdateSubmit = async (value: Partial<Document>) => {
await executeAndCallbackRefresh(async () => {
await handleAddOrUpdate(API_CONSTANTS.DOCUMENT, value);
})
}
const handleClickEdit = (record: Partial<Document>) => {
setFormValues(record);
handleUpdateModalVisible(true);
}
const handleOpenDrawer = (record: Partial<Document>) => {
setFormValues(record)
setDrawerOpen(true)
}
/**
* columns
*/
const columns: ProColumns<Document>[] = [
{
title: l('rc.doc.name'),
dataIndex: 'name',
sorter: true,
width: '20vw',
render: (dom, record) => {
return <a onClick={() => handleOpenDrawer(record)}>{dom}</a>;
},
},
{
title: l('rc.doc.category'),
sorter: true,
dataIndex: 'category',
filterMultiple: false,
filters: DOCUMENT_CATEGORY,
valueEnum: DOCUMENT_CATEGORY_ENUMS,
},
{
title: l('rc.doc.functionType'),
sorter: true,
dataIndex: 'type',
filterMultiple: false,
filters: DOCUMENT_FUNCTION_TYPE,
valueEnum: DOCUMENT_FUNCTION_ENUMS
},
{
title: l('rc.doc.subFunctionType'),
sorter: true,
dataIndex: 'subtype',
filters: DOCUMENT_SUBTYPE,
filterMultiple: false,
valueEnum: DOCUMENT_SUBTYPE_ENUMS,
},
{
title: l('rc.doc.description'),
dataIndex: 'description',
ellipsis: true,
hideInTable: true,
renderText: (text: string) => {
return <TextArea value={text} autoSize readOnly/>;
}
}, {
title: l('rc.doc.fillValue'),
dataIndex: 'fillValue',
hideInTable: true,
hideInSearch: true,
render: (_, record) => {
return <CodeShow width={"85vh"} code={record.fillValue}/>
}
},
{
title: l('rc.doc.version'),
sorter: true,
dataIndex: 'version',
hideInForm: false,
hideInSearch: true,
hideInTable: true,
},
{
title: l('global.table.isEnable'),
dataIndex: 'enabled',
hideInSearch: true,
filters: STATUS_MAPPING(),
filterMultiple: false,
valueEnum: STATUS_ENUM(),
render: (_, record) => {
return <EnableSwitchBtn key={`${record.id}_enable`} disabled={drawerOpen} record={record}
onChange={() => handleChangeEnable(record)}/>
},
},
{
title: l('global.table.createTime'),
dataIndex: 'createTime',
sorter: true,
hideInTable: true,
hideInSearch: true,
valueType: 'dateTime',
},
{
title: l('global.table.lastUpdateTime'),
dataIndex: 'updateTime',
sorter: true,
hideInTable: true,
hideInSearch: true,
valueType: 'dateTime',
},
{
title: l('global.table.operate'),
valueType: 'option',
width: '10vh',
render: (_, record) => [
<EditBtn key={`${record.id}_edit`} onClick={() => handleClickEdit(record)}/>,
<PopconfirmDeleteBtn key={`${record.id}_delete`} onClick={() => handleDeleteSubmit(record.id)}
description={l("rc.doc.deleteConfirm")}/>,
],
},
];
return <>
{/*TABLE*/}
<ProTable<Document>
{...PROTABLE_OPTIONS_PUBLIC}
loading={loading}
headerTitle={l('rc.doc.management')}
actionRef={actionRef}
toolBarRender={() => [<CreateBtn key={"doctable"} onClick={() => handleModalVisible(true)}/>,]}
request={(params, sorter, filter: any) => queryList(API_CONSTANTS.DOCUMENT, {...params, sorter, filter})}
columns={columns}
/>
{/*ADDED*/}
<DocumentModalForm
onSubmit={(value) => handleAddOrUpdateSubmit(value)}
onCancel={() => handleCancel()}
modalVisible={modalVisible}
values={{}}
/>
{/*UPDATED*/}
<DocumentModalForm
onSubmit={(value) => handleAddOrUpdateSubmit(value)}
onCancel={() => handleCancel()}
modalVisible={updateModalVisible}
values={formValues}
/>
{/*DRAWER*/}
<DocumentDrawer onCancel={() => handleCancel()} modalVisible={drawerOpen} values={formValues}
columns={columns}/>
</>
;
const [modalVisible, handleModalVisible] = useState<boolean>(false);
const [updateModalVisible, handleUpdateModalVisible] = useState<boolean>(false);
const [formValues, setFormValues] = useState<Partial<Document>>({});
const actionRef = useRef<ActionType>();
const [loading, setLoading] = useState<boolean>(false);
const [drawerOpen, setDrawerOpen] = useState<boolean>(false);
const handleCancel = () => {
handleModalVisible(false);
handleUpdateModalVisible(false);
setDrawerOpen(false);
}
const executeAndCallbackRefresh = async (callback: () => void) => {
setLoading(true);
await callback();
setLoading(false);
handleCancel();
actionRef.current?.reload?.();
}
/**
* delete document by id
* @param id
*/
const handleDeleteSubmit = async (id: number) => {
await executeAndCallbackRefresh(async () => {
await handleRemoveById(API_CONSTANTS.DOCUMENT_DELETE, id);
})
}
/**
* enable or disable document
* @param value
*/
const handleChangeEnable = async (value: Partial<Document>) => {
await executeAndCallbackRefresh(async () => {
await updateEnabled(API_CONSTANTS.DOCUMENT_ENABLE, {id: value.id});
})
};
/**
* added or update document var
* @param value
*/
const handleAddOrUpdateSubmit = async (value: Partial<Document>) => {
await executeAndCallbackRefresh(async () => {
await handleAddOrUpdate(API_CONSTANTS.DOCUMENT, value);
})
}
const handleClickEdit = (record: Partial<Document>) => {
setFormValues(record);
handleUpdateModalVisible(true);
}
const handleOpenDrawer = (record: Partial<Document>) => {
setFormValues(record)
setDrawerOpen(true)
}
/**
* columns
*/
const columns: ProColumns<Document>[] = [
{
title: l('rc.doc.name'),
dataIndex: 'name',
sorter: true,
width: '20vw',
render: (dom, record) => {
return <a onClick={() => handleOpenDrawer(record)}>{dom}</a>;
},
},
{
title: l('rc.doc.category'),
sorter: true,
dataIndex: 'category',
filterMultiple: false,
filters: DOCUMENT_CATEGORY,
valueEnum: DOCUMENT_CATEGORY_ENUMS,
},
{
title: l('rc.doc.functionType'),
sorter: true,
dataIndex: 'type',
filterMultiple: false,
filters: DOCUMENT_FUNCTION_TYPE,
valueEnum: DOCUMENT_FUNCTION_ENUMS
},
{
title: l('rc.doc.subFunctionType'),
sorter: true,
dataIndex: 'subtype',
filters: DOCUMENT_SUBTYPE,
filterMultiple: false,
valueEnum: DOCUMENT_SUBTYPE_ENUMS,
},
{
title: l('rc.doc.description'),
dataIndex: 'description',
ellipsis: true,
hideInTable: true,
renderText: (text: string) => {
return <TextArea value={text} autoSize readOnly/>;
}
}, {
title: l('rc.doc.fillValue'),
dataIndex: 'fillValue',
hideInTable: true,
hideInSearch: true,
render: (_, record) => {
return <CodeShow width={"85vh"} code={record.fillValue}/>
}
},
{
title: l('rc.doc.version'),
sorter: true,
dataIndex: 'version',
hideInForm: false,
hideInSearch: true,
hideInTable: true,
},
{
title: l('global.table.isEnable'),
dataIndex: 'enabled',
hideInSearch: true,
filters: STATUS_MAPPING(),
filterMultiple: false,
valueEnum: STATUS_ENUM(),
render: (_, record) => {
return <EnableSwitchBtn key={`${record.id}_enable`} disabled={drawerOpen} record={record}
onChange={() => handleChangeEnable(record)}/>
},
},
{
title: l('global.table.createTime'),
dataIndex: 'createTime',
sorter: true,
hideInTable: true,
hideInSearch: true,
valueType: 'dateTime',
},
{
title: l('global.table.lastUpdateTime'),
dataIndex: 'updateTime',
sorter: true,
hideInTable: true,
hideInSearch: true,
valueType: 'dateTime',
},
{
title: l('global.table.operate'),
valueType: 'option',
width: '10vh',
render: (_, record) => [
<EditBtn key={`${record.id}_edit`} onClick={() => handleClickEdit(record)}/>,
<PopconfirmDeleteBtn key={`${record.id}_delete`} onClick={() => handleDeleteSubmit(record.id)}
description={l("rc.doc.deleteConfirm")}/>,
],
},
];
return <>
{/*TABLE*/}
<ProTable<Document>
{...PROTABLE_OPTIONS_PUBLIC}
loading={loading}
headerTitle={l('rc.doc.management')}
actionRef={actionRef}
toolBarRender={() => [<CreateBtn key={"doctable"} onClick={() => handleModalVisible(true)}/>,]}
request={(params, sorter, filter: any) => queryList(API_CONSTANTS.DOCUMENT, {...params, sorter, filter})}
columns={columns}
/>
{/*ADDED*/}
<DocumentModalForm
key={formValues.id}
onSubmit={(value) => handleAddOrUpdateSubmit(value)}
onCancel={() => handleCancel()}
modalVisible={modalVisible}
values={{}}
/>
{/*UPDATED*/}
<DocumentModalForm
key={formValues.id}
onSubmit={(value) => handleAddOrUpdateSubmit(value)}
onCancel={() => handleCancel()}
modalVisible={updateModalVisible}
values={formValues}
/>
{/*DRAWER*/}
<DocumentDrawer
key={formValues.id}
onCancel={() => handleCancel()} modalVisible={drawerOpen} values={formValues}
columns={columns}
/>
</>
;
};
export default DocumentTableList;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment