Setting enums for collection fields are failing to load properly
// if there is not a user on this stub, make other actions available
if (!userId) {
fields.Status.enums = Object.values(STUB_STATE_ACTIONS_MAP);
fields.Status.defaultValue = OUTREACH_ATTEMPT_VIP_REQUEST; // <-- doesn't work..
} else {
fields.Status.enums = Object.values(USER_STATE_ACTIONS_MAP);
fields.Status.defaultValue = OUTREACH_ATTEMPT_SCREEN_REQUEST;
}
Observed behavior
Neither load nor change hooks update the field values for the enums; especially the default value. In the case above, when !userId is true, it will load the right actions, but have the defaultValue of the else condition. Furthermore, if I console the fields in either hook, it is never set in fields, but shows them in the dropdown. Eg. the field enums getting sent from the server are not the same as the ones in the dropdown.
Expected behavior
I would expect that the values of the enums and the default value would change upon load or change when the userId exists or not. This is never the case, though I can confirm that the userId will exist or not at times, so its a bit odd.
I was able however to get a workaround for the time being in that I can call 2 seperate actions for when there a userId or not, and then load the fields from a utils file showing the respective fields for each case. Not ideal as i have to create 2 actions, but if its the only work around, then that’ll work for me for now.
But if I’m doing this in a load or change hook, I already have that coming out from Fields as a prop.
hooks: {
load: async ({ fields, record }) => {
const { userId } = record.dataValues;
if (!userId) {
// if there is not user on this stub, make other actions available
fields.Status.enums = Object.values(STUB_STATE_ACTIONS_MAP);
fields.Status.defaultValue = OUTREACH_ATTEMPT_VIP_REQUEST;
} else {
fields.Status.enums = Object.values(USER_STATE_ACTIONS_MAP);
fields.Status.defaultValue = OUTREACH_ATTEMPT_SCREEN_REQUEST; // <-- doesn't work..
}
return fields;
}
}
Or are you saying I need to explicitly find and replace? I think not if we’re able to do this just fine on other fields without an issue:
if (!userId) {
fields.UserID.description = 'No User Found';
fields.UserID.isRequired = false;
fields.UserID.value = null;
}
I see! I don’t understand why, but defaultValue is not handled correctly when you update a field like this.
I will open a bug report about this.
I suggest you to use value instead of defaultValue it should auto select the value of your Status enums.
Let me know if that help.