Action field values after onChange hook

Expected behavior

Not sure if this is a bug or feature :slight_smile:
I set the onChange hook and don’t set the load hook. I trigger the onChange hook on the UI (setting a new enum for a field) and then close the action form. Then I open it again (for any record including the same one). The form has initial values as described in the action itself (an empty enum).

Actual behavior

The form has some values that were set in the onChange hook for the previous record.

Context

  • Package Version: 8.3.0
  • Express Version: 4.17.1
  • Sequelize Version: 5.22.4
  • Database Dialect: mysql
  • Database Version: 5

Hey @Tatsyana_Slabodchyka,

From what I understand, this is indeed a feature :slight_smile:

Smart action forms are completely reset once the form is close & re-open.
To add default values on form open, I would suggest to add a load hook instead.

However, I would be happy to understand your use case, or why did you expect the onChange enum value to be maintained on form close & open. We spec’d it this way, but we may have missed some specific use cases :slight_smile:

Let me know

No-no, this is the issue - form is no reset, I see previous values. If I add a load hook - the form is cleaned, yes, but without it - it keeps its values

No, my bad, I misread the Expected/Actual title :sweat_smile:
If you experience this, this is indeed a bug - As stated previously, the form should be reset each time it is open.

Could you share the implementation of your smart hook here (Or as a DM if you consider it private?)

Thanks

{
    name: 'Action',
    endpoint: '/endpoint',
    type: 'single',
    fields: [
      {
        field: 'reason',
        type: 'Enum',
        enums: ['any', 'value'],
        isRequired: true,
        hook: 'onReasonChanged',
      },
      {
        field: 'email',
        type: 'Enum',
        enums: [],
        isRequired: false,
      },
    ],
    hooks: {
      change: {
        onReasonChanged: async ({ fields }) => {
          const reasonField = fields.find((field) => field.field === 'email');

          reasonField.enums = ['gmail.com', 'tut.by'];
          reasonField.value = 'gmail.com';

          return fields;
        },
      },
    },
  },

So after I chose any reason I always see the email enum though initially it was empty

Indeed, I’m totally able to reproduce the issue.
I’ll open a bug report on our end, and we will update the thread once this is fixed.

Thanks for the report :pray:

1 Like

Hello,
The bug is fixed and merged.
Thanks for the report :slight_smile:

3 Likes

Hello team! Posting here because I think it’s related. If not - please let me know and I’ll create a new topic

I have a load hook and an enum field (with enums: [] in the field description). In the hook I push some values to the enum.

I open and close the action form several times.
Expected result: the enum contains the values pushed the last time the form was opened.
Actual result: the enum contains ALL the values pushed for all the form openings.

P.S. If I set the enum to [] in the beginning of the hook code - it contains only the expected latest values

I am using forest-express-sequelize@8.3.0

Hello,
Could you share the implementation of your smart hook here (Or as a DM if you consider it private?)
Thanks.

I removed all unnecessary (in my opinion :slight_smile:) details

fields: [
    {
      field: 'serialNumber',
      type: 'Enum',
      enums: [],
      isRequired: false,
    }
  ],
hooks: {
    load: async ({ fields, request }) => {
      try {
        // the proxy to access fields as we used to in old versions, issue remains if I change it to a regular fields array
        const fieldsProxy = new Proxy(fields, actionFieldsProxyHandler);
        // reset previous value, the issue happens if you comment the next line
        fieldsProxy.serialNumber.enums = [];

        const serialNumbers = ['123456', '789012']; // get from db

        serialNumbers.forEach((num) => {
          if (num) { fieldsProxy.serialNumber.enums.push(num.slice(-4)); }
        });

        return fields;
      } catch (e) {
        return fields;
      }
    },
  },

Hello,
This issue is always up to date ?
Sorry for the delay…

Hi! Sorry, didn’t understand your question

Sorry, Are you still blocked ?

I am not since I can reset the array in the beginning of a function. I haven’t checked if the issue still there