Fix enum defaultValue issues (#5307)

This PR fixes several issues:
- enum naming should be: {tableName}_{fieldName}_enum and respecting the
case
- defaultValue format handled in the FE should respect the one in the BE

In my opinion we should refactor the defaultValue:
- we should respect backend format: "'myDefault'" for constant default
and "0" for float, "now" for expressions, "true" for booleans. we can
rename it to defaultValueExpression if it is more clear but we should
not maintain a parallel system
- we should deprecate option: isDefaultValue which is confusing
- we should re-work backend to have a more unified approach between
fields and avoid having if everywhere about select, multiselect, and
currency cases. one unified "computeDefaultValue" function should do the
job

What is still broken:
- currency default Value on creation. I think we should do the refactor
first
- select default value edition.
These cases do not break the schema but are ignored currently
This commit is contained in:
Charles Bochet
2024-05-06 17:00:38 +02:00
committed by GitHub
parent ff77a4ee21
commit 2c9f50ecb1
9 changed files with 39 additions and 26 deletions

View File

@ -26,6 +26,7 @@ export const useFieldMetadataItem = () => {
) => {
const formattedInput = formatFieldMetadataItemInput(input);
debugger;
const defaultValue = getDefaultValueForBackend(
input.defaultValue ?? formattedInput.defaultValue,
input.type,
@ -51,24 +52,24 @@ export const useFieldMetadataItem = () => {
| 'options'
>,
) => {
const formattedInput = formatFieldMetadataItemInput(input);
const defaultValue = input.defaultValue
? typeof input.defaultValue == 'string'
? `'${input.defaultValue}'`
: input.defaultValue
: formattedInput.defaultValue ?? undefined;
// In Edit mode, all options need an id,
// so we generate an id for newly created options.
const inputOptions = input.options?.map((option: FieldMetadataOption) =>
option.id ? option : { ...option, id: v4() },
);
const formattedInput = formatFieldMetadataItemInput({
...input,
options: inputOptions,
});
const defaultValue = input.defaultValue ?? formattedInput.defaultValue;
return updateOneFieldMetadataItem({
fieldMetadataIdToUpdate: input.id,
updatePayload: formatFieldMetadataItemInput({
...input,
updatePayload: {
...formattedInput,
defaultValue,
// In Edit mode, all options need an id,
// so we generate an id for newly created options.
options: input.options?.map((option: FieldMetadataOption) =>
option.id ? option : { ...option, id: v4() },
),
}),
},
});
};

View File

@ -28,7 +28,13 @@ export const useUpdateOneFieldMetadataItem = () => {
fieldMetadataIdToUpdate: UpdateOneFieldMetadataItemMutationVariables['idToUpdate'];
updatePayload: Pick<
UpdateOneFieldMetadataItemMutationVariables['updatePayload'],
'description' | 'icon' | 'isActive' | 'label' | 'name'
| 'description'
| 'icon'
| 'isActive'
| 'label'
| 'name'
| 'defaultValue'
| 'options'
>;
}) => {
return await mutate({