From 7285dd04ee01b0bcbbff6b4e3f87b645110d44a5 Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 21 Mar 2022 15:23:59 +0800 Subject: [PATCH] feat: save as template --- packages/app/src/pages/index.tsx | 2 -- .../collection-manager/CollectionField.tsx | 1 - .../SchemaTemplateManagerProvider.tsx | 18 +++---------- .../src/actions/ui-schema-action.ts | 26 ++++++++++++++++++- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/packages/app/src/pages/index.tsx b/packages/app/src/pages/index.tsx index d7f71112f..0332f8a56 100644 --- a/packages/app/src/pages/index.tsx +++ b/packages/app/src/pages/index.tsx @@ -35,8 +35,6 @@ import { I18nextProvider } from 'react-i18next'; import { Link, NavLink } from 'react-router-dom'; import apiClient from './apiClient'; -console.log = (...args) => null; - apiClient.axios.interceptors.response.use( (response) => response, (error) => { diff --git a/packages/client/src/collection-manager/CollectionField.tsx b/packages/client/src/collection-manager/CollectionField.tsx index 1ca072f76..48c2910a8 100644 --- a/packages/client/src/collection-manager/CollectionField.tsx +++ b/packages/client/src/collection-manager/CollectionField.tsx @@ -26,7 +26,6 @@ const InternalField: React.FC = (props) => { if (!uiSchema) { return; } - console.log('uiSchema', uiSchema); setFieldProps('title', uiSchema.title); setFieldProps('description', uiSchema.description); setFieldProps('initialValue', uiSchema.default); diff --git a/packages/client/src/schema-templates/SchemaTemplateManagerProvider.tsx b/packages/client/src/schema-templates/SchemaTemplateManagerProvider.tsx index d71f345fe..0cf7772d0 100644 --- a/packages/client/src/schema-templates/SchemaTemplateManagerProvider.tsx +++ b/packages/client/src/schema-templates/SchemaTemplateManagerProvider.tsx @@ -62,7 +62,7 @@ export const useSchemaTemplateManager = () => { const { mode, template } = options; if (mode === 'copy') { const { data } = await api.request({ - url: `/uiSchemas:getJsonSchema/${template.uid}`, + url: `/uiSchemas:getJsonSchema/${template.uid}?includeAsyncNode=true`, }); const s = data?.data || {}; regenerateUid(s); @@ -79,7 +79,7 @@ export const useSchemaTemplateManager = () => { }, async copyTemplateSchema(template) { const { data } = await api.request({ - url: `/uiSchemas:getJsonSchema/${template.uid}`, + url: `/uiSchemas:getJsonSchema/${template.uid}?includeAsyncNode=true`, }); const s = data?.data || {}; regenerateUid(s); @@ -88,26 +88,16 @@ export const useSchemaTemplateManager = () => { async saveAsTemplate(values) { const { uid: schemaId } = values; const key = uid(); - await api.resource('uiSchemaTemplates').create({ + await api.resource('uiSchemas').saveAsTemplate({ + filterByTk: schemaId, values: { key, ...values, }, }); - await api.request({ - url: `/uiSchemas:clearAncestor/${schemaId}`, - }); await refresh(); return { key }; }, - async duplicate(template) { - const { data } = await api.request({ - url: `/uiSchemas:getJsonSchema/${template.uid}`, - }); - const s = data?.data || {}; - regenerateUid(s); - return s; - }, getTemplateBySchemaId(schemaId) { return templates?.find((template) => template.uid === schemaId); }, diff --git a/packages/plugin-ui-schema-storage/src/actions/ui-schema-action.ts b/packages/plugin-ui-schema-storage/src/actions/ui-schema-action.ts index a1e4a19c2..e21290d67 100644 --- a/packages/plugin-ui-schema-storage/src/actions/ui-schema-action.ts +++ b/packages/plugin-ui-schema-storage/src/actions/ui-schema-action.ts @@ -1,7 +1,7 @@ import { Context } from '@nocobase/actions'; +import { ActionParams } from '@nocobase/resourcer'; import lodash from 'lodash'; import UiSchemaRepository from '../repository'; -import { ActionParams } from '@nocobase/resourcer'; const getRepositoryFromCtx = (ctx: Context) => { return ctx.db.getCollection('uiSchemas').repository as UiSchemaRepository; @@ -64,6 +64,30 @@ export const uiSchemaActions = { insertAfterBegin: insertPositionActionBuilder('afterBegin'), insertBeforeEnd: insertPositionActionBuilder('beforeEnd'), insertAfterEnd: insertPositionActionBuilder('afterEnd'), + + async saveAsTemplate(ctx: Context, next) { + const { filterByTk, values } = ctx.action.params; + const db = ctx.db; + const transaction = await db.sequelize.transaction(); + try { + await db.getRepository('uiSchemaTemplates').create({ + values: { + ...values, + uid: filterByTk, + }, + transaction, + }); + await db.getRepository('uiSchemas').clearAncestor(filterByTk, { transaction }); + ctx.body = { + result: 'ok', + }; + await transaction.commit(); + } catch (error) { + await transaction.rollback(); + throw error; + } + await next(); + }, }; function insertPositionActionBuilder(position: 'beforeBegin' | 'afterBegin' | 'beforeEnd' | 'afterEnd') {