Fix state mutation serverless action (#8580)

In this PR:

- Ensure the `setNestedValue` does a deep copy of the provided object
and doesn't mutate it directly.
- Store the form's state in a `useState` instead of relying entirely on
the backend's state. This makes the form more resilient to slow network
connections.
- Ensure the input settings are reset when selecting another function.
- The Inner component now expects the serverless functions data to be
resolved before being mounted, so I split it.

Closes https://github.com/twentyhq/twenty/issues/8523
This commit is contained in:
Baptiste Devessier
2024-11-20 10:15:55 +01:00
committed by GitHub
parent a744515303
commit c133129eb0
5 changed files with 272 additions and 199 deletions

View File

@ -8,4 +8,19 @@ describe('setNestedValue', () => {
const expectedResult = { a: { b: newValue } };
expect(setNestedValue(obj, path, newValue)).toEqual(expectedResult);
});
it('should not mutate the initial object', () => {
const expectedObject = { a: { b: 'b' } };
const initialObject = structuredClone(expectedObject);
const path = ['a', 'b'];
const newValue = 'bb';
const updatedObject = setNestedValue(initialObject, path, newValue);
expect(initialObject).toEqual(expectedObject);
expect(updatedObject).not.toBe(initialObject);
expect(updatedObject.a).not.toBe(initialObject.a);
});
});

View File

@ -1,5 +1,5 @@
export const setNestedValue = (obj: any, path: string[], value: any) => {
const newObj = { ...obj };
const newObj = structuredClone(obj);
path.reduce((o, key, index) => {
if (index === path.length - 1) {
o[key] = value;