Skip to content

Commit a625161

Browse files
committed
🐛 [#4727] Transform initialValue on user variable dataType change
1 parent 7bae4ca commit a625161

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

src/openforms/js/components/admin/form_design/form-creation-form.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import {
7070
getUniqueKey,
7171
parseValidationErrors,
7272
slugify,
73+
transformInitialValue,
7374
updateKeyReferencesInLogic,
7475
updateRemovedKeyInLogic,
7576
} from './utils';
@@ -784,6 +785,14 @@ function reducer(draft, action) {
784785
draft.formVariables[index][propertyName] = propertyValue;
785786
}
786787

788+
// When dataType changes, a data transformation is needed
789+
if (propertyName === 'dataType') {
790+
draft.formVariables[index]['initialValue'] = transformInitialValue(
791+
propertyValue,
792+
originalVariable.initialValue
793+
);
794+
}
795+
787796
// Check if there are errors that need to be reset
788797
if (draft.formVariables[index].errors) {
789798
const errorKeys = propertyName === '' ? Object.keys(propertyValue) : [propertyName];
@@ -1229,6 +1238,42 @@ const FormCreationForm = ({formUuid, formUrl, formHistoryUrl, outgoingRequestsUr
12291238
payload: pluginId,
12301239
});
12311240
};
1241+
const onUserDefinedVariableChange = async (key, propertyName, propertyValue) => {
1242+
const originalVariable = state.formVariables.find(variable => variable.key === key);
1243+
// Just dispatch if anything other than dataType changes
1244+
// or if the initialValue is null/undefined
1245+
if (
1246+
propertyName !== 'dataType' ||
1247+
originalVariable?.initialValue == null ||
1248+
originalVariable?.initialValue === ''
1249+
) {
1250+
dispatch({
1251+
type: 'CHANGE_USER_DEFINED_VARIABLE',
1252+
payload: {key, propertyName, propertyValue},
1253+
});
1254+
return;
1255+
}
1256+
1257+
// Check if the dataType change is intentional.
1258+
if (
1259+
propertyName === 'dataType' &&
1260+
!window.confirm(
1261+
intl.formatMessage({
1262+
description:
1263+
'Changing user variable data type and transforming initial value confirmation message',
1264+
defaultMessage:
1265+
'Changing the data type requires the initial value to be changed. This will reset the initial value back to the empty value. Are you sure that you want to do this?',
1266+
})
1267+
)
1268+
) {
1269+
return;
1270+
}
1271+
1272+
dispatch({
1273+
type: 'CHANGE_USER_DEFINED_VARIABLE',
1274+
payload: {key, propertyName, propertyValue},
1275+
});
1276+
};
12321277

12331278
if (loading || state.submitting) {
12341279
return <Loader />;
@@ -1268,8 +1313,8 @@ const FormCreationForm = ({formUuid, formUrl, formHistoryUrl, outgoingRequestsUr
12681313
<div className="fetch-error">
12691314
<FormattedMessage
12701315
description="Generic admin error message"
1271-
defaultMessage={`Sorry! Something unexpected went wrong.<br></br>Contact your
1272-
technical administrator to investigate, or perhaps more information is
1316+
defaultMessage={`Sorry! Something unexpected went wrong.<br></br>Contact your
1317+
technical administrator to investigate, or perhaps more information is
12731318
available in the <link>outgoing request logs</link>.`}
12741319
values={{
12751320
br: () => <br />,
@@ -1506,12 +1551,7 @@ const FormCreationForm = ({formUuid, formUrl, formHistoryUrl, outgoingRequestsUr
15061551
variables={state.formVariables}
15071552
onAdd={() => dispatch({type: 'ADD_USER_DEFINED_VARIABLE'})}
15081553
onDelete={key => dispatch({type: 'DELETE_USER_DEFINED_VARIABLE', payload: key})}
1509-
onChange={(key, propertyName, propertyValue) =>
1510-
dispatch({
1511-
type: 'CHANGE_USER_DEFINED_VARIABLE',
1512-
payload: {key, propertyName, propertyValue},
1513-
})
1514-
}
1554+
onChange={onUserDefinedVariableChange}
15151555
onFieldChange={onFieldChange}
15161556
/>
15171557
</TabPanel>

src/openforms/js/components/admin/form_design/utils.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ const checkKeyChange = (mutationType, newComponent, oldComponent) => {
5757
return newComponent.key !== oldComponent.key;
5858
};
5959

60+
const transformInitialValue = (newType, originalValue) => {
61+
switch (newType) {
62+
case 'array':
63+
return [];
64+
65+
case 'boolean':
66+
case 'int':
67+
case 'float':
68+
return undefined;
69+
70+
case 'object':
71+
return {};
72+
73+
case 'string':
74+
case 'datetime':
75+
case 'date':
76+
case 'time':
77+
return '';
78+
79+
default:
80+
return originalValue;
81+
}
82+
};
83+
6084
const updateKeyReferencesInLogic = (existingLogicRules, originalKey, newKey) => {
6185
for (const rule of existingLogicRules) {
6286
if (!JSON.stringify(rule).includes(originalKey)) continue;
@@ -219,6 +243,7 @@ export {
219243
getFormComponents,
220244
findComponent,
221245
checkKeyChange,
246+
transformInitialValue,
222247
updateKeyReferencesInLogic,
223248
updateRemovedKeyInLogic,
224249
getUniqueKey,

0 commit comments

Comments
 (0)