fix: anti double submit in form
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { PageAction } from '@/config';
|
||||
import { PageActionType } from '@/config/types';
|
||||
import useSubmitLock from '@/hooks/use-submit-lock';
|
||||
import {
|
||||
AlertBlockInfo,
|
||||
GSDrawer,
|
||||
@@ -63,6 +64,7 @@ const AddModal: React.FC<AddModalProps> = (props) => {
|
||||
const [yamlContent, setYamlContent] = useState<string>('');
|
||||
const [formContent, setFormContent] = useState<FormData>({} as FormData);
|
||||
const alertRef = useRef<HTMLDivElement>(null);
|
||||
const { loading, guard, run, release } = useSubmitLock();
|
||||
|
||||
const showVersionCustomSuffix =
|
||||
currentData?.backend_source === BackendSourceValueMap.BUILTIN ||
|
||||
@@ -92,17 +94,22 @@ const AddModal: React.FC<AddModalProps> = (props) => {
|
||||
};
|
||||
|
||||
const onOk = () => {
|
||||
if (activeKey === 'yaml') {
|
||||
const content = editorRef.current?.getContent();
|
||||
if (content) {
|
||||
onSubmitYaml({ content: content });
|
||||
guard(() => {
|
||||
if (activeKey === 'yaml') {
|
||||
const content = editorRef.current?.getContent();
|
||||
if (!content) {
|
||||
// nothing to submit — drop the lock so the button stays usable
|
||||
release();
|
||||
return;
|
||||
}
|
||||
run(() => onSubmitYaml({ content: content }));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
formRef.current?.submit();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const onFinish = (values: FormData) => {
|
||||
const onFinish = async (values: FormData) => {
|
||||
const versionConfigs = values.version_configs?.reduce(
|
||||
(acc: Record<string, any>, curr) => {
|
||||
if (curr.version_no) {
|
||||
@@ -117,16 +124,18 @@ const AddModal: React.FC<AddModalProps> = (props) => {
|
||||
|
||||
const defaultVersion = values.version_configs?.find((v) => v.is_default);
|
||||
|
||||
onSubmit({
|
||||
...values,
|
||||
parameter_format:
|
||||
values.parameter_format === 'auto' || !values.parameter_format
|
||||
? null
|
||||
: values.parameter_format,
|
||||
default_version: defaultVersion?.version_no || '',
|
||||
// @ts-ignore
|
||||
version_configs: versionConfigs
|
||||
});
|
||||
await run(() =>
|
||||
onSubmit({
|
||||
...values,
|
||||
parameter_format:
|
||||
values.parameter_format === 'auto' || !values.parameter_format
|
||||
? null
|
||||
: values.parameter_format,
|
||||
default_version: defaultVersion?.version_no || '',
|
||||
// @ts-ignore
|
||||
version_configs: versionConfigs
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@@ -279,6 +288,7 @@ const AddModal: React.FC<AddModalProps> = (props) => {
|
||||
<ModalFooter
|
||||
onCancel={onClose}
|
||||
onOk={onOk}
|
||||
loading={loading}
|
||||
style={ModalFooterStyle}
|
||||
></ModalFooter>
|
||||
</>
|
||||
@@ -295,6 +305,7 @@ const AddModal: React.FC<AddModalProps> = (props) => {
|
||||
children: (
|
||||
<BackendForm
|
||||
onFinish={onFinish}
|
||||
onFinishFailed={release}
|
||||
action={action}
|
||||
currentData={formContent as ListItem}
|
||||
ref={formRef}
|
||||
|
||||
@@ -13,10 +13,11 @@ type AddModalProps = {
|
||||
action: PageActionType;
|
||||
currentData?: ListItem;
|
||||
onFinish: (values: FormData) => void;
|
||||
onFinishFailed?: (errorInfo: any) => void;
|
||||
ref?: any;
|
||||
};
|
||||
const BackendForm: React.FC<AddModalProps> = forwardRef(
|
||||
({ action, currentData, onFinish }, ref) => {
|
||||
({ action, currentData, onFinish, onFinishFailed }, ref) => {
|
||||
const intl = useIntl();
|
||||
const [form] = Form.useForm();
|
||||
const [activeKey, setActiveKey] = React.useState<string[]>([]);
|
||||
@@ -26,7 +27,7 @@ const BackendForm: React.FC<AddModalProps> = forwardRef(
|
||||
currentData?.backend_source === BackendSourceValueMap.BUILTIN ||
|
||||
currentData?.backend_source === BackendSourceValueMap.COMMUNITY;
|
||||
|
||||
const onFinishFailed = (errorInfo: any) => {
|
||||
const handleOnFinishFailed = (errorInfo: any) => {
|
||||
const errorFields = errorInfo.errorFields || [];
|
||||
if (errorFields.length > 0) {
|
||||
const versionError = errorFields.find((field: any) =>
|
||||
@@ -38,6 +39,7 @@ const BackendForm: React.FC<AddModalProps> = forwardRef(
|
||||
setActiveKey([]);
|
||||
}
|
||||
}
|
||||
onFinishFailed?.(errorInfo);
|
||||
};
|
||||
|
||||
const handleOnFinish = (values: FormData) => {
|
||||
@@ -106,7 +108,7 @@ const BackendForm: React.FC<AddModalProps> = forwardRef(
|
||||
preserve={false}
|
||||
scrollToFirstError={true}
|
||||
initialValues={_.omit(currentData, ['version_configs'])}
|
||||
onFinishFailed={onFinishFailed}
|
||||
onFinishFailed={handleOnFinishFailed}
|
||||
>
|
||||
<BasicForm></BasicForm>
|
||||
<VersionsForm
|
||||
|
||||
Reference in New Issue
Block a user