From 50d8129b9c7b38b1d27028ccb84f2a8d873b92d0 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Mon, 28 Oct 2024 15:36:07 +0100 Subject: [PATCH] :alien: [#4693] Integrate Objects API prefill modal with backend --- .../forms/api/serializers/form_variable.py | 12 ++--- .../forms/tests/variables/test_viewset.py | 32 +++++++++++- .../variables/VariablesEditor.stories.js | 4 +- .../form_design/variables/VariablesTable.js | 3 +- .../variables/prefill/ObjectsAPIFields.js | 49 ++++++++++--------- .../prefill/PrefillConfigurationForm.js | 16 +++--- .../variables/prefill/PrefillSummary.js | 2 +- .../components/admin/forms/VariableMapping.js | 26 ++++++++-- 8 files changed, 98 insertions(+), 46 deletions(-) diff --git a/src/openforms/forms/api/serializers/form_variable.py b/src/openforms/forms/api/serializers/form_variable.py index 102db35cdf..a6a827d559 100644 --- a/src/openforms/forms/api/serializers/form_variable.py +++ b/src/openforms/forms/api/serializers/form_variable.py @@ -205,15 +205,13 @@ def validate(self, attrs): } ) - if (prefill_plugin and not prefill_attribute) or ( - not prefill_plugin and prefill_attribute + if (prefill_plugin and not (prefill_attribute or prefill_options)) or ( + not prefill_plugin and (prefill_attribute or prefill_options) ): raise ValidationError( - { - "prefill_attribute": _( - "Prefill plugin and attribute must both be specified." - ), - } + _( + "Prefill plugin must be specified with either prefill attribute or prefill options." + ) ) # check the specific validation options of the prefill plugin diff --git a/src/openforms/forms/tests/variables/test_viewset.py b/src/openforms/forms/tests/variables/test_viewset.py index b6333070f5..9b0ba3e79f 100644 --- a/src/openforms/forms/tests/variables/test_viewset.py +++ b/src/openforms/forms/tests/variables/test_viewset.py @@ -977,7 +977,9 @@ def test_bulk_create_and_update_with_prefill_constraints(self): self.assertEqual(response.json()["invalidParams"][0]["code"], "invalid") self.assertEqual( response.json()["invalidParams"][0]["reason"], - _("Prefill plugin and attribute must both be specified."), + _( + "Prefill plugin must be specified with either prefill attribute or prefill options." + ), ) with self.subTest( @@ -1018,3 +1020,31 @@ def test_bulk_create_and_update_with_prefill_constraints(self): "Prefill plugin, attribute and options can not be specified at the same time." ), ) + + with self.subTest( + "user_defined with prefill plugin and prefill options is allowed" + ): + data = [ + { + "form": form_url, + "form_definition": form_definition_url, + "key": "userdefined", + "name": "Test", + "service_fetch_configuration": None, + "data_type": FormVariableDataTypes.string, + "source": FormVariableSources.user_defined, + "prefill_plugin": "objects_api", + "prefill_attribute": "", + "prefill_options": {"foo": "bar"}, + } + ] + + response = self.client.put( + reverse( + "api:form-variables", + kwargs={"uuid_or_slug": form.uuid}, + ), + data=data, + ) + + self.assertEqual(status.HTTP_200_OK, response.status_code) diff --git a/src/openforms/js/components/admin/form_design/variables/VariablesEditor.stories.js b/src/openforms/js/components/admin/form_design/variables/VariablesEditor.stories.js index 3f24311731..6ce83969c2 100644 --- a/src/openforms/js/components/admin/form_design/variables/VariablesEditor.stories.js +++ b/src/openforms/js/components/admin/form_design/variables/VariablesEditor.stories.js @@ -80,9 +80,9 @@ const VARIABLES = [ initialValue: [], prefillOptions: { objectsApiGroup: 1, - objecttype: '2c77babf-a967-4057-9969-0200320d23f2', + objecttypeUuid: '2c77babf-a967-4057-9969-0200320d23f2', objecttypeVersion: 1, - variablesMapping: [{formVariable: 'formioComponent', prefillProperty: ['firstName']}], + variablesMapping: [{variableKey: 'formioComponent', targetPath: ['firstName']}], }, }, ]; diff --git a/src/openforms/js/components/admin/form_design/variables/VariablesTable.js b/src/openforms/js/components/admin/form_design/variables/VariablesTable.js index 683893efc6..595c1e44cd 100644 --- a/src/openforms/js/components/admin/form_design/variables/VariablesTable.js +++ b/src/openforms/js/components/admin/form_design/variables/VariablesTable.js @@ -162,11 +162,12 @@ const EditableVariableRow = ({index, variable, onDelete, onChange, onFieldChange identifierRole={variable.prefillIdentifierRole} errors={variable.errors} options={variable.prefillOptions} - onChange={({plugin, attribute, identifierRole}) => + onChange={({plugin, attribute, identifierRole, prefillOptions}) => onChange(variable.key, '', { prefillPlugin: plugin, prefillAttribute: attribute, prefillIdentifierRole: identifierRole, + prefillOptions: prefillOptions, }) } /> diff --git a/src/openforms/js/components/admin/form_design/variables/prefill/ObjectsAPIFields.js b/src/openforms/js/components/admin/form_design/variables/prefill/ObjectsAPIFields.js index d8af3e337f..e2aedbaf9c 100644 --- a/src/openforms/js/components/admin/form_design/variables/prefill/ObjectsAPIFields.js +++ b/src/openforms/js/components/admin/form_design/variables/prefill/ObjectsAPIFields.js @@ -33,9 +33,9 @@ const PLUGIN_ID = 'objects_api'; */ const onApiGroupChange = prevValues => ({ ...prevValues, - options: { - ...prevValues.options, - objecttype: '', + prefillOptions: { + ...prevValues.prefillOptions, + objecttypeUuid: '', objecttypeVersion: undefined, variablesMapping: [], }, @@ -43,8 +43,8 @@ const onApiGroupChange = prevValues => ({ // Load the possible prefill properties // XXX: this would benefit from client-side caching -const getProperties = async (objectsApiGroup, objecttype, objecttypeVersion) => { - const endpoint = `/api/v2/prefill/plugins/objects-api/objecttypes/${objecttype}/versions/${objecttypeVersion}/properties`; +const getProperties = async (objectsApiGroup, objecttypeUuid, objecttypeVersion) => { + const endpoint = `/api/v2/prefill/plugins/objects-api/objecttypes/${objecttypeUuid}/versions/${objecttypeVersion}/properties`; // XXX: clean up error handling here at some point... const response = await get(endpoint, {objects_api_group: objectsApiGroup}); if (!response.ok) throw response.data; @@ -56,7 +56,7 @@ const ObjectsAPIFields = ({errors}) => { const { values: { plugin, - options: {objecttype, objecttypeVersion, objectsApiGroup}, + prefillOptions: {objecttypeUuid, objecttypeVersion, objectsApiGroup, variablesMapping}, }, setFieldValue, } = useFormikContext(); @@ -74,13 +74,13 @@ const ObjectsAPIFields = ({errors}) => { value = [], error, } = useAsync(async () => { - if (!plugin || !objecttype || !objecttypeVersion || !objectsApiGroup) return []; + if (!plugin || !objecttypeUuid || !objecttypeVersion || !objectsApiGroup) return []; try { - return await getProperties(objectsApiGroup, objecttype, objecttypeVersion); + return await getProperties(objectsApiGroup, objecttypeUuid, objecttypeVersion); } catch (e) { throw e; } - }, [plugin, objecttype, objecttypeVersion, objectsApiGroup]); + }, [plugin, objecttypeUuid, objecttypeVersion, objectsApiGroup]); // throw errors to the nearest error boundary if (error) throw error; @@ -92,7 +92,7 @@ const ObjectsAPIFields = ({errors}) => { { - if (values.options.variablesMapping.length === 0) return true; + if (variablesMapping.length === 0) return true; const confirmSwitch = window.confirm( intl.formatMessage({ description: @@ -102,14 +102,16 @@ const ObjectsAPIFields = ({errors}) => { }) ); if (!confirmSwitch) return false; - setFieldValue('options.variablesMapping', []); + setFieldValue('prefillOptions.variablesMapping', []); return true; }} - name="options.objectsApiGroup" + name="prefillOptions.objectsApiGroup" onApiGroupChange={onApiGroupChange} /> { } > { - if (values.options.variablesMapping.length === 0) return true; + if (variablesMapping.length === 0) return true; const confirmSwitch = window.confirm( intl.formatMessage({ description: @@ -132,13 +134,15 @@ const ObjectsAPIFields = ({errors}) => { }) ); if (!confirmSwitch) return false; - setFieldValue('options.variablesMapping', []); + setFieldValue('prefillOptions.variablesMapping', []); return true; }} /> { } > @@ -164,10 +168,11 @@ const ObjectsAPIFields = ({errors}) => { >