-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3989 from open-formulieren/feature/3688-handle-va…
…r-changes [#3688] Handle mutations/deletions of variables
- Loading branch information
Showing
4 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...penforms/js/components/admin/form_design/registrations/userDefinedVariableEditHandlers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Update the mapped properties after the user defined variable change | ||
* @param {Object} registrationBackendOptions The currently configured backend options, | ||
* including the mapped properties. Note that this is | ||
* an immer draft which can be mutated. | ||
* @param {Object} variable The user defined variable that was mutated in some way | ||
* @param {Object|null} originalVariable The user defined variable before it was mutated, null if it is removed. | ||
* @return {Object|null} The updated registrationBackendOptions draft. Return null to indicate | ||
* no changes need to be made. | ||
*/ | ||
const onObjectsAPIUserDefinedVariableEdit = ( | ||
registrationBackendOptions, | ||
variable, | ||
originalVariable | ||
) => { | ||
// If the data type has changed, the mapped target might not be compatible anymore: | ||
const shouldRemove = originalVariable == null || variable.dataType !== originalVariable.dataType; | ||
|
||
if (shouldRemove) { | ||
const matchingMappingIndex = registrationBackendOptions.variablesMapping.findIndex( | ||
mapping => mapping.variableKey === variable.key | ||
); | ||
|
||
if (matchingMappingIndex === -1) return; | ||
registrationBackendOptions.variablesMapping.splice(matchingMappingIndex, 1); | ||
} else { | ||
const keyChanged = variable.key !== originalVariable.key; | ||
if (!keyChanged) return null; | ||
|
||
for (const mapping of registrationBackendOptions.variablesMapping) { | ||
if (mapping.variableKey === originalVariable.key) { | ||
mapping.variableKey = variable.key; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
export {onObjectsAPIUserDefinedVariableEdit}; |