From ed9d716d7d86304efcc513e2f9175164bbf931da Mon Sep 17 00:00:00 2001 From: katherinehhh Date: Thu, 6 Jul 2023 17:13:20 +0800 Subject: [PATCH] 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 --- .../antd/action/Action.Designer.tsx | 26 ++++++++- .../components/DuplicateAction.tsx | 57 +++++++++++++------ 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/action/Action.Designer.tsx b/packages/core/client/src/schema-component/antd/action/Action.Designer.tsx index 7a6f5d0bf7..dcac17f522 100644 --- a/packages/core/client/src/schema-component/antd/action/Action.Designer.tsx +++ b/packages/core/client/src/schema-component/antd/action/Action.Designer.tsx @@ -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) => { { }, 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'], diff --git a/packages/core/client/src/schema-initializer/components/DuplicateAction.tsx b/packages/core/client/src/schema-initializer/components/DuplicateAction.tsx index 92933756eb..434690c74b 100644 --- a/packages/core/client/src/schema-initializer/components/DuplicateAction.tsx +++ b/packages/core/client/src/schema-initializer/components/DuplicateAction.tsx @@ -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')} )} - - - + + + + + + +