Skip to content

Commit

Permalink
✨ [#4727] Added initial value transformation cases for all value types
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmolen committed Oct 28, 2024
1 parent d2a0173 commit 0d09286
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1258,14 +1258,21 @@ const FormCreationForm = ({formUuid, formUrl, formHistoryUrl, outgoingRequestsUr
})
);
const onUserDefinedVariableChange = async (key, propertyName, propertyValue) => {
const originalVariable = state.formVariables.find(variable => variable.key === key);
// Just dispatch if anything other than dataType changes
// or if the initialValue is null/undefined
if (propertyName !== 'dataType' || originalVariable?.initialValue == null) {
dispatch({
type: 'CHANGE_USER_DEFINED_VARIABLE',
payload: {key, propertyName, propertyValue},
});
return;
}

// Check if the dataType change is intentional.
// If not, cancel the dispatch
if (
propertyName === 'dataType' &&
checkIfInitialValueTransformationIsNeeded(
propertyValue,
state.formVariables.find(variable => variable.key === key)
) &&
checkIfInitialValueTransformationIsNeeded(propertyValue, originalVariable) &&
!(await confirmDelete())
) {
return;
Expand Down
45 changes: 43 additions & 2 deletions src/openforms/js/components/admin/form_design/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,31 @@ const checkKeyChange = (mutationType, newComponent, oldComponent) => {

const checkIfInitialValueTransformationIsNeeded = (newType, originalVariable) => {
// If the initialValue is null/undefined, we should transform
if (!originalVariable.initialValue) {
if (originalVariable?.initialValue == null) {
return true;
}

switch (newType) {
case 'array':
// If the value isn't in array format, it should be transformd
return !Array.isArray(originalVariable.initialValue);

case 'boolean':
return typeof originalVariable.initialValue !== 'boolean';

case 'object':
return typeof originalVariable.initialValue !== 'object';

case 'int':
case 'float':
return typeof originalVariable.initialValue !== 'number';

case 'string':
case 'datetime':
case 'date':
case 'time':
return typeof originalVariable.initialValue !== 'string';

default:
return false;
}
Expand All @@ -75,10 +92,34 @@ const checkIfInitialValueTransformationIsNeeded = (newType, originalVariable) =>
const transformInitialValue = (newType, originalVariable) => {
switch (newType) {
case 'array':
if (!originalVariable.initialValue || typeof originalVariable.initialValue === 'object') {
if (
originalVariable?.initialValue == null ||
typeof originalVariable.initialValue === 'object'
) {
return [];
}
return [originalVariable.initialValue];

case 'boolean':
return undefined;

case 'int':
return Number.parseInt(originalVariable.initialValue);

case 'float':
return Number.parseFloat(originalVariable.initialValue);

case 'object':
return originalVariable.initialValue == null ? {} : {value: originalVariable.initialValue};

case 'string':
return JSON.stringify(originalVariable.initialValue);

case 'datetime':
case 'date':
case 'time':
return '';

default:
return originalVariable.initialValue;
}
Expand Down

0 comments on commit 0d09286

Please sign in to comment.