From 3f955b622184719155a92d6e340e2013e3368e67 Mon Sep 17 00:00:00 2001 From: robinvandermolen Date: Thu, 24 Oct 2024 12:48:50 +0200 Subject: [PATCH] :bug: [#4727] Transform initialValue on user variable dataType change Backport-of: #4797 --- src/openforms/js/compiled-lang/en.json | 30 +++-------- src/openforms/js/compiled-lang/nl.json | 30 +++-------- .../admin/form_design/form-creation-form.js | 52 ++++++++++++++++--- .../js/components/admin/form_design/utils.js | 25 +++++++++ src/openforms/js/lang/en.json | 5 ++ src/openforms/js/lang/nl.json | 5 ++ 6 files changed, 93 insertions(+), 54 deletions(-) diff --git a/src/openforms/js/compiled-lang/en.json b/src/openforms/js/compiled-lang/en.json index 1129fbe0ed..2e7241a838 100644 --- a/src/openforms/js/compiled-lang/en.json +++ b/src/openforms/js/compiled-lang/en.json @@ -923,12 +923,6 @@ "value": "The text that will be displayed in the form step to save the current information. Leave blank to get value from global configuration." } ], - "8M403q": [ - { - "type": 0, - "value": "Soft required fields should be filled out, but empty values don't block the users' progress. Sometimes this is needed for legal reasons. A component cannot be hard and soft required at the same time." - } - ], "8NbOpb": [ { "type": 0, @@ -2715,12 +2709,6 @@ "value": "Select existing form definition" } ], - "QL4SGQ": [ - { - "type": 0, - "value": "Soft required" - } - ], "QLTh2N": [ { "type": 0, @@ -3793,6 +3781,12 @@ "value": "Login required?" } ], + "af22yB": [ + { + "type": 0, + "value": "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?" + } + ], "aqYeqv": [ { "type": 0, @@ -4801,12 +4795,6 @@ "value": "Session will expire" } ], - "jtWzSW": [ - { - "type": 0, - "value": "option 2: € 15,99" - } - ], "k1+ljn": [ { "type": 0, @@ -5521,12 +5509,6 @@ "value": "Regular expression for city" } ], - "sxdJ/8": [ - { - "type": 0, - "value": "option 1: € 10,99" - } - ], "t5fg/K": [ { "type": 0, diff --git a/src/openforms/js/compiled-lang/nl.json b/src/openforms/js/compiled-lang/nl.json index 4a819d6bea..bfdf41ab56 100644 --- a/src/openforms/js/compiled-lang/nl.json +++ b/src/openforms/js/compiled-lang/nl.json @@ -927,12 +927,6 @@ "value": "De tekst op de knop om de gegevens van de huidige stap op te slaan en het formulier te onderbreken. Laat dit veld leeg om de standaardinstelling te gebruiken." } ], - "8M403q": [ - { - "type": 0, - "value": "Aangeraden velden moeten in principe ingevuld worden, maar ontbrekende waarden blokkeren de voortgang niet. Dit is soms nodig voor juridische redenen. Een component kan niet tegelijk verplicht en aangeraden zijn." - } - ], "8NbOpb": [ { "type": 0, @@ -2732,12 +2726,6 @@ "value": "Selecteer een bestaande formulierdefinitie" } ], - "QL4SGQ": [ - { - "type": 0, - "value": "Aangeraden (niet-blokkerend verplicht)" - } - ], "QLTh2N": [ { "type": 0, @@ -3811,6 +3799,12 @@ "value": "Vereist authenticatie" } ], + "af22yB": [ + { + "type": 0, + "value": "Het veranderen van het datatype vereist een verandering aan de beginwaarde. Dit zal de beginwaarde terugbrengen naar de standaardwaarde. Weet je zeker dat je dit wilt doen?" + } + ], "aqYeqv": [ { "type": 0, @@ -4823,12 +4817,6 @@ "value": "Sessie gaat vervallen" } ], - "jtWzSW": [ - { - "type": 0, - "value": "optie 2: € 15,99" - } - ], "k1+ljn": [ { "type": 0, @@ -5543,12 +5531,6 @@ "value": "Reguliere expressie" } ], - "sxdJ/8": [ - { - "type": 0, - "value": "optie 1: € 10,99" - } - ], "t5fg/K": [ { "type": 0, diff --git a/src/openforms/js/components/admin/form_design/form-creation-form.js b/src/openforms/js/components/admin/form_design/form-creation-form.js index 6a6752140d..adc94c80b6 100644 --- a/src/openforms/js/components/admin/form_design/form-creation-form.js +++ b/src/openforms/js/components/admin/form_design/form-creation-form.js @@ -70,6 +70,7 @@ import { getUniqueKey, parseValidationErrors, slugify, + transformInitialValue, updateKeyReferencesInLogic, updateRemovedKeyInLogic, } from './utils'; @@ -784,6 +785,14 @@ function reducer(draft, action) { draft.formVariables[index][propertyName] = propertyValue; } + // When dataType changes, a data transformation is needed + if (propertyName === 'dataType') { + draft.formVariables[index]['initialValue'] = transformInitialValue( + propertyValue, + originalVariable.initialValue + ); + } + // Check if there are errors that need to be reset if (draft.formVariables[index].errors) { const errorKeys = propertyName === '' ? Object.keys(propertyValue) : [propertyName]; @@ -1229,6 +1238,42 @@ const FormCreationForm = ({formUuid, formUrl, formHistoryUrl}) => { payload: pluginId, }); }; + 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 || + originalVariable?.initialValue === '' + ) { + dispatch({ + type: 'CHANGE_USER_DEFINED_VARIABLE', + payload: {key, propertyName, propertyValue}, + }); + return; + } + + // Check if the dataType change is intentional. + if ( + propertyName === 'dataType' && + !window.confirm( + intl.formatMessage({ + description: + 'Changing user variable data type and transforming initial value confirmation message', + defaultMessage: + '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?', + }) + ) + ) { + return; + } + + dispatch({ + type: 'CHANGE_USER_DEFINED_VARIABLE', + payload: {key, propertyName, propertyValue}, + }); + }; if (loading || state.submitting) { return ; @@ -1496,12 +1541,7 @@ const FormCreationForm = ({formUuid, formUrl, formHistoryUrl}) => { variables={state.formVariables} onAdd={() => dispatch({type: 'ADD_USER_DEFINED_VARIABLE'})} onDelete={key => dispatch({type: 'DELETE_USER_DEFINED_VARIABLE', payload: key})} - onChange={(key, propertyName, propertyValue) => - dispatch({ - type: 'CHANGE_USER_DEFINED_VARIABLE', - payload: {key, propertyName, propertyValue}, - }) - } + onChange={onUserDefinedVariableChange} onFieldChange={onFieldChange} /> diff --git a/src/openforms/js/components/admin/form_design/utils.js b/src/openforms/js/components/admin/form_design/utils.js index 683d0892b6..8ada4cddb1 100644 --- a/src/openforms/js/components/admin/form_design/utils.js +++ b/src/openforms/js/components/admin/form_design/utils.js @@ -57,6 +57,30 @@ const checkKeyChange = (mutationType, newComponent, oldComponent) => { return newComponent.key !== oldComponent.key; }; +const transformInitialValue = (newType, originalValue) => { + switch (newType) { + case 'array': + return []; + + case 'boolean': + case 'int': + case 'float': + return undefined; + + case 'object': + return {}; + + case 'string': + case 'datetime': + case 'date': + case 'time': + return ''; + + default: + return originalValue; + } +}; + const updateKeyReferencesInLogic = (existingLogicRules, originalKey, newKey) => { for (const rule of existingLogicRules) { if (!JSON.stringify(rule).includes(originalKey)) continue; @@ -219,6 +243,7 @@ export { getFormComponents, findComponent, checkKeyChange, + transformInitialValue, updateKeyReferencesInLogic, updateRemovedKeyInLogic, getUniqueKey, diff --git a/src/openforms/js/lang/en.json b/src/openforms/js/lang/en.json index da39e0f709..ddadbf4601 100644 --- a/src/openforms/js/lang/en.json +++ b/src/openforms/js/lang/en.json @@ -1704,6 +1704,11 @@ "description": "Form step login required label", "originalDefault": "Login required?" }, + "af22yB": { + "defaultMessage": "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?", + "description": "Changing user variable data type and transforming initial value confirmation message", + "originalDefault": "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?" + }, "auZOcD": { "defaultMessage": "Mapping expression", "description": "Service fetch configuration modal form mapping expression field label", diff --git a/src/openforms/js/lang/nl.json b/src/openforms/js/lang/nl.json index 69b4252c31..ecaa5ad58c 100644 --- a/src/openforms/js/lang/nl.json +++ b/src/openforms/js/lang/nl.json @@ -1719,6 +1719,11 @@ "description": "Form step login required label", "originalDefault": "Login required?" }, + "af22yB": { + "defaultMessage": "Het veranderen van het datatype vereist een verandering aan de beginwaarde. Dit zal de beginwaarde terugbrengen naar de standaardwaarde. Weet je zeker dat je dit wilt doen?", + "description": "Changing user variable data type and transforming initial value confirmation message", + "originalDefault": "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?" + }, "auZOcD": { "defaultMessage": "Mappingexpressie", "description": "Service fetch configuration modal form mapping expression field label",