refactor: duplicate support inherited collection (#2181)
* refactor: set collection from current collection when deplicate * refactor: set collection from current collection when deplicate * fix: duplicate target collection * refactor: code improve
This commit is contained in:
parent
a3dc6d67e0
commit
ed9d716d7d
@ -6,6 +6,7 @@ import React, { useEffect, useState } from 'react';
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useDesignable } from '../..';
|
import { useDesignable } from '../..';
|
||||||
import { useCollection, useCollectionManager } from '../../../collection-manager';
|
import { useCollection, useCollectionManager } from '../../../collection-manager';
|
||||||
|
import { useRecord } from '../../../record-provider';
|
||||||
import { OpenModeSchemaItems } from '../../../schema-items';
|
import { OpenModeSchemaItems } from '../../../schema-items';
|
||||||
import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings';
|
import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings';
|
||||||
import { useCollectionState } from '../../../schema-settings/DataTemplates/hooks/useCollectionState';
|
import { useCollectionState } from '../../../schema-settings/DataTemplates/hooks/useCollectionState';
|
||||||
@ -61,6 +62,7 @@ export const ActionDesigner = (props) => {
|
|||||||
const { dn } = useDesignable();
|
const { dn } = useDesignable();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const isAction = useLinkageAction();
|
const isAction = useLinkageAction();
|
||||||
|
const record = useRecord();
|
||||||
const isPopupAction = ['create', 'update', 'view', 'customize:popup', 'duplicate'].includes(
|
const isPopupAction = ['create', 'update', 'view', 'customize:popup', 'duplicate'].includes(
|
||||||
fieldSchema['x-action'] || '',
|
fieldSchema['x-action'] || '',
|
||||||
);
|
);
|
||||||
@ -248,7 +250,13 @@ export const ActionDesigner = (props) => {
|
|||||||
<SchemaSettings.ModalItem
|
<SchemaSettings.ModalItem
|
||||||
title={t('Duplicate mode')}
|
title={t('Duplicate mode')}
|
||||||
components={{ Tree }}
|
components={{ Tree }}
|
||||||
scope={{ getEnableFieldTree, collectionName: name, getOnLoadData, getOnCheck }}
|
scope={{
|
||||||
|
getEnableFieldTree,
|
||||||
|
collectionName: fieldSchema['x-component-props']?.duplicateCollection || record?.__collection || name,
|
||||||
|
currentCollection: record?.__collection || name,
|
||||||
|
getOnLoadData,
|
||||||
|
getOnCheck,
|
||||||
|
}}
|
||||||
schema={
|
schema={
|
||||||
{
|
{
|
||||||
type: 'object',
|
type: 'object',
|
||||||
@ -266,7 +274,7 @@ export const ActionDesigner = (props) => {
|
|||||||
},
|
},
|
||||||
collection: {
|
collection: {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
title: '{{ t("Collection") }}',
|
title: '{{ t("Target collection") }}',
|
||||||
required: true,
|
required: true,
|
||||||
description: t('If collection inherits, choose inherited collections as templates'),
|
description: t('If collection inherits, choose inherited collections as templates'),
|
||||||
default: '{{ collectionName }}',
|
default: '{{ collectionName }}',
|
||||||
@ -276,6 +284,17 @@ export const ActionDesigner = (props) => {
|
|||||||
'x-component-props': {
|
'x-component-props': {
|
||||||
options: collectionList,
|
options: collectionList,
|
||||||
},
|
},
|
||||||
|
'x-reactions': [
|
||||||
|
{
|
||||||
|
dependencies: ['.duplicateMode'],
|
||||||
|
fulfill: {
|
||||||
|
state: {
|
||||||
|
disabled: `{{ $deps[0]==="quickDulicate" }}`,
|
||||||
|
value: `{{ $deps[0]==="quickDulicate"? currentCollection:collectionName }}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
},
|
},
|
||||||
duplicateFields: {
|
duplicateFields: {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
@ -319,13 +338,14 @@ export const ActionDesigner = (props) => {
|
|||||||
},
|
},
|
||||||
} as ISchema
|
} as ISchema
|
||||||
}
|
}
|
||||||
onSubmit={({ duplicateMode, duplicateFields }) => {
|
onSubmit={({ duplicateMode, collection, duplicateFields }) => {
|
||||||
const fields = Array.isArray(duplicateFields) ? duplicateFields : duplicateFields.checked || [];
|
const fields = Array.isArray(duplicateFields) ? duplicateFields : duplicateFields.checked || [];
|
||||||
field.componentProps.duplicateMode = duplicateMode;
|
field.componentProps.duplicateMode = duplicateMode;
|
||||||
field.componentProps.duplicateFields = fields;
|
field.componentProps.duplicateFields = fields;
|
||||||
fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
|
fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
|
||||||
fieldSchema['x-component-props'].duplicateMode = duplicateMode;
|
fieldSchema['x-component-props'].duplicateMode = duplicateMode;
|
||||||
fieldSchema['x-component-props'].duplicateFields = fields;
|
fieldSchema['x-component-props'].duplicateFields = fields;
|
||||||
|
fieldSchema['x-component-props'].duplicateCollection = collection;
|
||||||
dn.emit('patch', {
|
dn.emit('patch', {
|
||||||
schema: {
|
schema: {
|
||||||
['x-uid']: fieldSchema['x-uid'],
|
['x-uid']: fieldSchema['x-uid'],
|
||||||
|
@ -1,12 +1,22 @@
|
|||||||
import React, { createContext, useContext, useState } from 'react';
|
|
||||||
import { observer, RecursionField, useField, useFieldSchema } from '@formily/react';
|
|
||||||
import { RecordProvider, ActionContextProvider, useActionContext, useRecord, useCollection } from '../../';
|
|
||||||
import { useTranslation } from 'react-i18next';
|
|
||||||
import { css, cx } from '@emotion/css';
|
import { css, cx } from '@emotion/css';
|
||||||
import { useAPIClient, useBlockRequestContext, useDesignable } from '../../';
|
import { observer, RecursionField, useField, useFieldSchema } from '@formily/react';
|
||||||
import { actionDesignerCss } from './CreateRecordAction';
|
|
||||||
import { fetchTemplateData } from '../../schema-component/antd/form-v2/Templates';
|
|
||||||
import { Button, message } from 'antd';
|
import { Button, message } from 'antd';
|
||||||
|
import React, { createContext, useContext, useState } from 'react';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import {
|
||||||
|
ActionContextProvider,
|
||||||
|
CollectionProvider,
|
||||||
|
RecordProvider,
|
||||||
|
useActionContext,
|
||||||
|
useAPIClient,
|
||||||
|
useBlockRequestContext,
|
||||||
|
useCollection,
|
||||||
|
useCollectionManager,
|
||||||
|
useDesignable,
|
||||||
|
useRecord,
|
||||||
|
} from '../../';
|
||||||
|
import { fetchTemplateData } from '../../schema-component/antd/form-v2/Templates';
|
||||||
|
import { actionDesignerCss } from './CreateRecordAction';
|
||||||
|
|
||||||
const DuplicatefieldsContext = createContext(null);
|
const DuplicatefieldsContext = createContext(null);
|
||||||
|
|
||||||
@ -23,19 +33,31 @@ export const DuplicateAction = observer((props: any) => {
|
|||||||
const { designable } = useDesignable();
|
const { designable } = useDesignable();
|
||||||
const [visible, setVisible] = useState(false);
|
const [visible, setVisible] = useState(false);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const { resource, service, __parent, block } = useBlockRequestContext();
|
const { service, __parent, block } = useBlockRequestContext();
|
||||||
const { duplicateFields, duplicateMode = 'quickDulicate' } = fieldSchema['x-component-props'];
|
const { duplicateFields, duplicateMode = 'quickDulicate', duplicateCollection } = fieldSchema['x-component-props'];
|
||||||
const { id } = useRecord();
|
const record = useRecord();
|
||||||
|
const { id, __collection } = record;
|
||||||
const ctx = useActionContext();
|
const ctx = useActionContext();
|
||||||
const { name } = useCollection();
|
const { name } = useCollection();
|
||||||
|
const { getCollectionFields } = useCollectionManager();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const template = { key: 'duplicate', dataId: id, default: true, fields: duplicateFields || [], collection: name };
|
const collectionFields = getCollectionFields(__collection);
|
||||||
|
const template = {
|
||||||
|
key: 'duplicate',
|
||||||
|
dataId: id,
|
||||||
|
default: true,
|
||||||
|
fields:
|
||||||
|
duplicateFields?.filter((v) => {
|
||||||
|
return collectionFields.find((k) => k.name === v);
|
||||||
|
}) || [],
|
||||||
|
collection: __collection,
|
||||||
|
};
|
||||||
const isLinkBtn = fieldSchema['x-component'] === 'Action.Link';
|
const isLinkBtn = fieldSchema['x-component'] === 'Action.Link';
|
||||||
const handelQuickDuplicate = async () => {
|
const handelQuickDuplicate = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const data = await fetchTemplateData(api, template, t);
|
const data = await fetchTemplateData(api, template, t);
|
||||||
await resource.create({
|
await api.resource(__collection || name).create({
|
||||||
values: {
|
values: {
|
||||||
...data,
|
...data,
|
||||||
},
|
},
|
||||||
@ -52,7 +74,6 @@ export const DuplicateAction = observer((props: any) => {
|
|||||||
console.error(error); // Handle or log the error appropriately
|
console.error(error); // Handle or log the error appropriately
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handelDuplicate = () => {
|
const handelDuplicate = () => {
|
||||||
if (!disabled && !loading) {
|
if (!disabled && !loading) {
|
||||||
if (duplicateFields?.length > 0) {
|
if (duplicateFields?.length > 0) {
|
||||||
@ -111,9 +132,13 @@ export const DuplicateAction = observer((props: any) => {
|
|||||||
{loading ? t('Duplicating') : children || t('Duplicate')}
|
{loading ? t('Duplicating') : children || t('Duplicate')}
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
|
<CollectionProvider name={duplicateCollection || name}>
|
||||||
|
<RecordProvider record={{ ...record, __collection: duplicateCollection || __collection }}>
|
||||||
<ActionContextProvider value={{ ...ctx, visible, setVisible }}>
|
<ActionContextProvider value={{ ...ctx, visible, setVisible }}>
|
||||||
<RecursionField schema={fieldSchema} basePath={field.address} onlyRenderProperties />
|
<RecursionField schema={fieldSchema} basePath={field.address} onlyRenderProperties />
|
||||||
</ActionContextProvider>
|
</ActionContextProvider>
|
||||||
|
</RecordProvider>
|
||||||
|
</CollectionProvider>
|
||||||
</div>
|
</div>
|
||||||
</DuplicatefieldsContext.Provider>
|
</DuplicatefieldsContext.Provider>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user