Skip to content

Commit

Permalink
fix: field formValue is sometimes not correctly provided in execute c…
Browse files Browse the repository at this point in the history
…ontext (#894)

* fix: field formValue is sometimes not correctly provided in execute context [skip ci]

* refactor: remove wrong changes

* test: add unit tests

* chore: remove typings

* test: fix collection unit test

* refactor: rename argument

* refactor: update name in action.ts

---------

Co-authored-by: Nicolas Moreau <nicolas.moreau76@gmail.com>
  • Loading branch information
realSpok and Nicolas Moreau committed Dec 11, 2023
1 parent eaf3804 commit a24aab1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 18 deletions.
2 changes: 2 additions & 0 deletions packages/agent/src/routes/modification/action/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export default class ActionRoute extends CollectionRoute {
this.actionName,
unsafeData,
filterForCaller,
{ includeHiddenFields: true }, // during execute, we need all possible fields
);

// Now that we have the field list, we can parse the data again.
Expand Down Expand Up @@ -156,6 +157,7 @@ export default class ActionRoute extends CollectionRoute {
changedField: body.data.attributes.changed_field,
searchField: body.data.attributes.search_field,
searchValues,
includeHiddenFields: false,
});

context.response.body = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,12 @@ describe('ActionRoute', () => {
searchExtended: false,
segment: null,
},
{ changedField: undefined, searchField: undefined, searchValues: {} },
{
changedField: undefined,
searchField: undefined,
searchValues: {},
includeHiddenFields: false,
},
);

expect(context.response.body).toEqual({ fields: [{ field: 'firstname', type: 'String' }] });
Expand Down Expand Up @@ -585,6 +590,7 @@ describe('ActionRoute', () => {
searchValues: {
firstname: undefined,
},
includeHiddenFields: false,
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CollectionSchema,
DataSourceDecorator,
Filter,
GetFormMetas,
PlainFilter,
RecordData,
} from '@forestadmin/datasource-toolkit';
Expand Down Expand Up @@ -54,11 +55,7 @@ export default class ActionCollectionDecorator extends CollectionDecorator {
name: string,
data?: RecordData,
filter?: Filter,
metas?: {
changedField: string;
searchField?: string | null;
searchValues?: Record<string, string | null>;
},
metas?: GetFormMetas,
): Promise<ActionField[]> {
const action = this.actions[name];
if (!action) return this.childCollection.getForm(caller, name, data, filter, metas);
Expand All @@ -78,7 +75,7 @@ export default class ActionCollectionDecorator extends CollectionDecorator {
}

dynamicFields = await this.dropDefaults(context, dynamicFields, formValues);
dynamicFields = await this.dropIfs(context, dynamicFields);
if (!metas?.includeHiddenFields) dynamicFields = await this.dropIfs(context, dynamicFields);

const fields = await this.dropDeferred(context, metas?.searchValues, dynamicFields);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,75 @@ describe('ActionDecorator', () => {
]);
});
});
describe('with single action with ifs', () => {
beforeEach(() => {
newBooks.addAction('make photocopy', {
scope: 'Single',
execute: () => {},
form: [
{
label: 'noIf',
type: 'String',
},
{
label: 'dynamicIfFalse',
type: 'String',
if: () => false,
},
{
label: 'dynamicIfTrue',
type: 'String',
if: () => true,
},
],
});
});

test('should dropIfs which are false if required', async () => {
const fields = await newBooks.getForm(
factories.caller.build(),
'make photocopy',
undefined,
undefined,
{ includeHiddenFields: false },
);

expect(fields).toEqual([
expect.objectContaining({
label: 'noIf',
type: 'String',
}),
expect.objectContaining({
label: 'dynamicIfTrue',
type: 'String',
}),
]);
});
test('should not dropIfs if required', async () => {
const fields = await newBooks.getForm(
factories.caller.build(),
'make photocopy',
undefined,
undefined,
{ includeHiddenFields: true },
);

expect(fields).toEqual([
expect.objectContaining({
label: 'noIf',
type: 'String',
}),
expect.objectContaining({
label: 'dynamicIfFalse',
type: 'String',
}),
expect.objectContaining({
label: 'dynamicIfTrue',
type: 'String',
}),
]);
});
});

describe('with single action with both load and change hooks', () => {
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ActionField, ActionResult } from '../interfaces/action';
import { Caller } from '../interfaces/caller';
import { Chart } from '../interfaces/chart';
import { Collection, DataSource } from '../interfaces/collection';
import { Collection, DataSource, GetFormMetas } from '../interfaces/collection';
import Aggregation, { AggregateResult } from '../interfaces/query/aggregation';
import PaginatedFilter from '../interfaces/query/filter/paginated';
import Filter from '../interfaces/query/filter/unpaginated';
Expand Down Expand Up @@ -69,11 +69,7 @@ export default class CollectionDecorator implements Collection {
name: string,
data?: RecordData,
filter?: Filter,
metas?: {
changedField?: string;
searchValue?: Record<string, string | null>;
searchField?: string;
},
metas?: GetFormMetas,
): Promise<ActionField[]> {
const refinedFilter = await this.refineFilter(caller, filter);

Expand Down
13 changes: 8 additions & 5 deletions packages/datasource-toolkit/src/interfaces/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export interface DataSource<C extends Collection = Collection> {
renderChart(caller: Caller, name: string): Promise<Chart>;
}

export type GetFormMetas = {
changedField?: string;
searchField?: string | null;
searchValues?: Record<string, string | null>;
includeHiddenFields?: boolean;
};

export interface Collection {
get nativeDriver(): unknown | null;
get dataSource(): DataSource;
Expand All @@ -35,11 +42,7 @@ export interface Collection {
name: string,
formValues?: RecordData,
filter?: Filter,
metas?: {
changedField?: string;
searchValues?: Record<string, string | null>;
searchField?: string;
},
metas?: GetFormMetas,
): Promise<ActionField[]>;

create(caller: Caller, data: RecordData[]): Promise<RecordData[]>;
Expand Down

0 comments on commit a24aab1

Please sign in to comment.