Skip to content

Commit

Permalink
feat: action form can be a function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Moreau committed Jul 11, 2024
1 parent b155cb8 commit 8cda02b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export interface BaseAction<
> {
generateFile?: boolean;
scope: Scope;
form?: DynamicField<Context>[];
form?:
| DynamicField<Context>[]
| ((context: ActionContext<TSchema, string>) => DynamicField<Context>[])
| ((context: ActionContext<TSchema, string>) => Promise<DynamicField<Context>[]>);
execute(
context: Context,
resultBuilder: ResultBuilder,
Expand Down

0 comments on commit 8cda02b

Please sign in to comment.