mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 09:17:23 +00:00
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 { useDesignable } from '../..';
|
||||
import { useCollection, useCollectionManager } from '../../../collection-manager';
|
||||
import { useRecord } from '../../../record-provider';
|
||||
import { OpenModeSchemaItems } from '../../../schema-items';
|
||||
import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings';
|
||||
import { useCollectionState } from '../../../schema-settings/DataTemplates/hooks/useCollectionState';
|
||||
@ -61,6 +62,7 @@ export const ActionDesigner = (props) => {
|
||||
const { dn } = useDesignable();
|
||||
const { t } = useTranslation();
|
||||
const isAction = useLinkageAction();
|
||||
const record = useRecord();
|
||||
const isPopupAction = ['create', 'update', 'view', 'customize:popup', 'duplicate'].includes(
|
||||
fieldSchema['x-action'] || '',
|
||||
);
|
||||
@ -248,7 +250,13 @@ export const ActionDesigner = (props) => {
|
||||
<SchemaSettings.ModalItem
|
||||
title={t('Duplicate mode')}
|
||||
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={
|
||||
{
|
||||
type: 'object',
|
||||
@ -266,7 +274,7 @@ export const ActionDesigner = (props) => {
|
||||
},
|
||||
collection: {
|
||||
type: 'string',
|
||||
title: '{{ t("Collection") }}',
|
||||
title: '{{ t("Target collection") }}',
|
||||
required: true,
|
||||
description: t('If collection inherits, choose inherited collections as templates'),
|
||||
default: '{{ collectionName }}',
|
||||
@ -276,6 +284,17 @@ export const ActionDesigner = (props) => {
|
||||
'x-component-props': {
|
||||
options: collectionList,
|
||||
},
|
||||
'x-reactions': [
|
||||
{
|
||||
dependencies: ['.duplicateMode'],
|
||||
fulfill: {
|
||||
state: {
|
||||
disabled: `{{ $deps[0]==="quickDulicate" }}`,
|
||||
value: `{{ $deps[0]==="quickDulicate"? currentCollection:collectionName }}`,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
duplicateFields: {
|
||||
type: 'array',
|
||||
@ -319,13 +338,14 @@ export const ActionDesigner = (props) => {
|
||||
},
|
||||
} as ISchema
|
||||
}
|
||||
onSubmit={({ duplicateMode, duplicateFields }) => {
|
||||
onSubmit={({ duplicateMode, collection, duplicateFields }) => {
|
||||
const fields = Array.isArray(duplicateFields) ? duplicateFields : duplicateFields.checked || [];
|
||||
field.componentProps.duplicateMode = duplicateMode;
|
||||
field.componentProps.duplicateFields = fields;
|
||||
fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
|
||||
fieldSchema['x-component-props'].duplicateMode = duplicateMode;
|
||||
fieldSchema['x-component-props'].duplicateFields = fields;
|
||||
fieldSchema['x-component-props'].duplicateCollection = collection;
|
||||
dn.emit('patch', {
|
||||
schema: {
|
||||
['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 { useAPIClient, useBlockRequestContext, useDesignable } from '../../';
|
||||
import { actionDesignerCss } from './CreateRecordAction';
|
||||
import { fetchTemplateData } from '../../schema-component/antd/form-v2/Templates';
|
||||
import { observer, RecursionField, useField, useFieldSchema } from '@formily/react';
|
||||
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);
|
||||
|
||||
@ -23,19 +33,31 @@ export const DuplicateAction = observer((props: any) => {
|
||||
const { designable } = useDesignable();
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const { resource, service, __parent, block } = useBlockRequestContext();
|
||||
const { duplicateFields, duplicateMode = 'quickDulicate' } = fieldSchema['x-component-props'];
|
||||
const { id } = useRecord();
|
||||
const { service, __parent, block } = useBlockRequestContext();
|
||||
const { duplicateFields, duplicateMode = 'quickDulicate', duplicateCollection } = fieldSchema['x-component-props'];
|
||||
const record = useRecord();
|
||||
const { id, __collection } = record;
|
||||
const ctx = useActionContext();
|
||||
const { name } = useCollection();
|
||||
const { getCollectionFields } = useCollectionManager();
|
||||
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 handelQuickDuplicate = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const data = await fetchTemplateData(api, template, t);
|
||||
await resource.create({
|
||||
await api.resource(__collection || name).create({
|
||||
values: {
|
||||
...data,
|
||||
},
|
||||
@ -52,7 +74,6 @@ export const DuplicateAction = observer((props: any) => {
|
||||
console.error(error); // Handle or log the error appropriately
|
||||
}
|
||||
};
|
||||
|
||||
const handelDuplicate = () => {
|
||||
if (!disabled && !loading) {
|
||||
if (duplicateFields?.length > 0) {
|
||||
@ -111,9 +132,13 @@ export const DuplicateAction = observer((props: any) => {
|
||||
{loading ? t('Duplicating') : children || t('Duplicate')}
|
||||
</Button>
|
||||
)}
|
||||
<ActionContextProvider value={{ ...ctx, visible, setVisible }}>
|
||||
<RecursionField schema={fieldSchema} basePath={field.address} onlyRenderProperties />
|
||||
</ActionContextProvider>
|
||||
<CollectionProvider name={duplicateCollection || name}>
|
||||
<RecordProvider record={{ ...record, __collection: duplicateCollection || __collection }}>
|
||||
<ActionContextProvider value={{ ...ctx, visible, setVisible }}>
|
||||
<RecursionField schema={fieldSchema} basePath={field.address} onlyRenderProperties />
|
||||
</ActionContextProvider>
|
||||
</RecordProvider>
|
||||
</CollectionProvider>
|
||||
</div>
|
||||
</DuplicatefieldsContext.Provider>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user