From 8cda02bf4d8cdbc964278cf19654a79324001632 Mon Sep 17 00:00:00 2001 From: Nicolas Moreau Date: Thu, 11 Jul 2024 17:41:18 +0100 Subject: [PATCH] feat: action form can be a function --- .../src/decorators/actions/collection.ts | 26 ++++++++++++++----- .../src/decorators/actions/types/actions.ts | 5 +++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/datasource-customizer/src/decorators/actions/collection.ts b/packages/datasource-customizer/src/decorators/actions/collection.ts index 10166621c3..281d04ac10 100644 --- a/packages/datasource-customizer/src/decorators/actions/collection.ts +++ b/packages/datasource-customizer/src/decorators/actions/collection.ts @@ -66,7 +66,13 @@ export default class ActionCollectionDecorator extends CollectionDecorator { const context = this.getContext(caller, action, formValues, filter, used, metas?.changedField); // Convert DynamicField to ActionField in successive steps. - let dynamicFields: DynamicField[] = action.form.map(c => ({ ...c })); + let dynamicFields: DynamicField[]; + + if (this.isHandler(action.form)) { + dynamicFields = (await action.form(context)).map(c => ({ ...c })); + } else { + dynamicFields = action.form.map(c => ({ ...c })); + } if (metas?.searchField) { // in the case of a search hook, @@ -96,12 +102,18 @@ export default class ActionCollectionDecorator extends CollectionDecorator { for (const [name, { form, scope, generateFile }] of Object.entries(this.actions)) { // An action form can be send in the schema to avoid calling the load handler // as long as there is nothing dynamic in it. - const isDynamic = form?.some( - field => - Object.values(field).some(value => typeof value === 'function') || - // A field with a hardcoded file should not be sent to the apimap. it is marked dynamic - (field.type.includes('File') && field.defaultValue), - ); + let isDynamic = false; + + if (this.isHandler(form)) { + isDynamic = true; + } else { + isDynamic = form?.some( + field => + Object.values(field).some(value => typeof value === 'function') || + // A field with a hardcoded file should not be sent to the apimap. it is marked dynamic + (field.type.includes('File') && field.defaultValue), + ); + } newSchema.actions[name] = { scope, generateFile: !!generateFile, staticForm: !isDynamic }; } diff --git a/packages/datasource-customizer/src/decorators/actions/types/actions.ts b/packages/datasource-customizer/src/decorators/actions/types/actions.ts index 68ef4128c2..b67b98df83 100644 --- a/packages/datasource-customizer/src/decorators/actions/types/actions.ts +++ b/packages/datasource-customizer/src/decorators/actions/types/actions.ts @@ -16,7 +16,10 @@ export interface BaseAction< > { generateFile?: boolean; scope: Scope; - form?: DynamicField[]; + form?: + | DynamicField[] + | ((context: ActionContext) => DynamicField[]) + | ((context: ActionContext) => Promise[]>); execute( context: Context, resultBuilder: ResultBuilder,