Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optional id in action form fields #1174

Merged
merged 22 commits into from
Sep 23, 2024
Merged
5 changes: 5 additions & 0 deletions packages/_example/src/forest/customizations/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export default (collection: CardCustomizer) =>
defaultValue: 80,
if: ctx => ['Gold'].includes(ctx.formValues.Plan),
},
{
type: 'Number',
id: 'test-price',
label: 'price',
},
{ type: 'Layout', component: 'Separator' },
{ type: 'Layout', component: 'HtmlBlock', content: '<h3>constraints:</h3>' },
{
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/src/utils/forest-schema/action-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class ForestValueConverter {
const data = {};

for (const [key, value] of Object.entries(rawData)) {
const field = fields.find(f => f.label === key);
const field = fields.find(f => f.id === key);

// Skip fields from the default form
if (!SchemaGeneratorActions.defaultFields.map(f => f.field).includes(key)) {
Expand Down
11 changes: 7 additions & 4 deletions packages/agent/src/utils/forest-schema/generator-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default class SchemaGeneratorActions {
static defaultFields: ForestServerActionField[] = [
{
field: 'Loading...',
label: 'Loading...',
type: 'String',
isReadOnly: true,
defaultValue: 'Form is loading',
Expand Down Expand Up @@ -98,10 +99,12 @@ export default class SchemaGeneratorActions {

/** Build schema for given field */
static buildFieldSchema(dataSource: DataSource, field: ActionField): ForestServerActionField {
const { label, description, isRequired, isReadOnly, watchChanges, type } = field;
const output = { description, isRequired, isReadOnly } as Record<string, unknown>;
const { id, label, description, isRequired, isReadOnly, watchChanges, type } = field;
const output = { field: id, label, description, isRequired, isReadOnly } as Record<
string,
unknown
>;

output.field = label;
output.value = ForestValueConverter.valueToForest(field, field.value);

if (watchChanges) output.hook = 'changeHook';
Expand Down Expand Up @@ -215,7 +218,7 @@ export default class SchemaGeneratorActions {
return {
type: 'Layout',
component: 'Input',
fieldId: element.label,
fieldId: element.id,
};
}
}
12 changes: 7 additions & 5 deletions packages/agent/test/routes/modification/action/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ describe('ActionRoute', () => {
actions: { MySingleAction: { scope: 'Single' } },
fields: { id: factories.columnSchema.uuidPrimaryKey().build() },
},
getForm: jest.fn().mockResolvedValue([{ type: 'String', label: 'firstname' }]),
getForm: jest
.fn()
.mockResolvedValue([{ type: 'String', label: 'firstname', id: 'firstname' }]),
execute: jest.fn().mockResolvedValue({
type: 'Error',
message: 'the result does not matter',
Expand Down Expand Up @@ -574,7 +576,7 @@ describe('ActionRoute', () => {
);

expect(context.response.body).toEqual({
fields: [{ field: 'firstname', type: 'String' }],
fields: [{ field: 'firstname', type: 'String', label: 'firstname' }],
layout: [],
});
});
Expand All @@ -583,15 +585,15 @@ describe('ActionRoute', () => {
const context = createMockContext(baseContext);

dataSource.getCollection('books').getForm = jest.fn().mockResolvedValue([
{ type: 'String', label: 'firstname' },
{ type: 'String', id: 'firstname', label: 'firstname' },
{ type: 'Layout', component: 'Separator' },
]);

// @ts-expect-error: test private method
await route.handleHook(context);

expect(context.response.body).toEqual({
fields: [{ field: 'firstname', type: 'String' }],
fields: [{ field: 'firstname', type: 'String', label: 'firstname' }],
layout: [{ component: 'input', fieldId: 'firstname' }, { component: 'separator' }],
});
});
Expand Down Expand Up @@ -641,7 +643,7 @@ describe('ActionRoute', () => {
);

expect(context.response.body).toEqual({
fields: [{ field: 'firstname', type: 'String' }],
fields: [{ field: 'firstname', type: 'String', label: 'firstname' }],
layout: [],
});
});
Expand Down
Loading
Loading