import { ArrowDownOutlined, ArrowRightOutlined } from '@ant-design/icons'; import { BetaSchemaForm, ProForm, ProFormDependency, ProFormSelect, useBreakpoint, } from '@ant-design/pro-components'; import { Alert, App, Col, Divider, Flex, type FormInstance, Modal, Row, Typography, } from 'antd'; import { useEffect, useRef, useState } from 'react'; import useRichFormat from '@/hooks/useRichI18n'; import { $t } from '@/utils/i18n'; import { type SchemaType, schemaMap } from '../../ruleFormSchema'; import { handleEmptyValuesFunc, processSingleRule } from '../../utils'; import AssetsSelect from '../AssetsSelect'; import SymbolSelect from '../SymbolSelect'; type RuleListItem = Partial; type CopyPopupProps = { visible: boolean; formValues: Partial; dataSourceList: API.DataSourceListItem[]; currentRuleType: number; onDone: (refresh?: boolean) => void; }; const { Text } = Typography; const SingleCopyPopup = ({ visible, onDone, currentRuleType, formValues, dataSourceList, }: CopyPopupProps) => { const { richFormat } = useRichFormat(); const screens = useBreakpoint(); const { message, modal } = App.useApp(); const formRef = useRef(null); const [confirmLoading, setConfirmLoading] = useState(false); const [errMsg, setErrMsg] = useState(''); useEffect(() => { if (!visible) { setErrMsg(''); } return () => { setErrMsg(''); }; }, [visible]); const { leftForm, symbolFormShow, assetFormShow, transformInitialValue, transformSubmitValue, } = schemaMap[currentRuleType as unknown as SchemaType]?.({ richFormat }) || {}; const rightDataSourceList = dataSourceList.filter( (item) => item.id !== formValues?.dataSourceId, ); const initialValues = transformInitialValue?.(formValues) || formValues; const rightFirstDataSource = rightDataSourceList[0]; const copyFormValues = { ...initialValues, name: initialValues?.name ? `${initialValues.name} - ${rightFirstDataSource?.sourceName}` : `${Date.now()} - ${rightFirstDataSource?.sourceName}`, groupFilter: '', id: void 0, dataSourceId: rightFirstDataSource?.id || '', }; if (symbolFormShow) { copyFormValues.param2 = ''; } if (assetFormShow) { copyFormValues.param1 = ''; } const formContent = ({ dataSourceList, formAttr, }: { dataSourceList: API.DataSourceListItem[]; formAttr: Record; }) => { return ( {(symbolFormShow || assetFormShow) && ( {({ dataSourceId }) => ( <> {symbolFormShow && ( )} {assetFormShow && ( )} )} )} ); }; return ( onDone()} destroyOnHidden centered width={['xl', 'xxl'].includes(screens || '') ? '60%' : '95%'} confirmLoading={confirmLoading} okText={$t('pages.rules.cloneConfirm')} onOk={async () => { setConfirmLoading(true); const values = formRef.current?.getFieldsValue(); const tmpValues = handleEmptyValuesFunc(values); const submitValues = transformSubmitValue?.(tmpValues) || tmpValues; submitValues.type = currentRuleType; try { await processSingleRule({ payload: submitValues }); message.success($t('pages.tips.operationSuccess')); onDone(true); } catch (error: any) { console.log('error', error); switch (error?.errType) { case 1: case 2: modal.warning({ content: error?.respMessage || $t(error?.errMsg), centered: true, onOk: () => { setErrMsg(''); setConfirmLoading(false); onDone(true); }, }); return; case 3: setErrMsg($t(error?.errMsg)); formRef.current?.setFieldsValue({ ...error.data, }); break; default: break; } } setConfirmLoading(false); }} styles={{ body: { maxHeight: '82vh', overflowY: 'auto', overflowX: 'hidden', marginRight: -10, paddingRight: 8, }, }} >
📋 {$t('pages.rules.originalRule')}
{formContent({ dataSourceList, formAttr: { initialValues, disabled: true }, })}
✏️ {$t('pages.rules.cloneRulePreview')}
{formContent({ dataSourceList: rightDataSourceList, formAttr: { initialValues: copyFormValues, formRef: formRef, disabled: confirmLoading, }, })} {errMsg && ( )}
); }; export default SingleCopyPopup;