From 29e670071bb28e262735c34590ccff0969bb4a5f Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Fri, 8 Mar 2024 16:19:52 +0100 Subject: [PATCH] [#3688] Add option to map var to geometry attribute --- .../objectsapi/ObjectsApiOptionsFormFields.js | 1 + .../objectsapi/ObjectsApiSummaryHandler.js | 15 ++++++- .../ObjectsApiVariableConfigurationEditor.js | 44 ++++++++++++++++--- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/openforms/js/components/admin/form_design/registrations/objectsapi/ObjectsApiOptionsFormFields.js b/src/openforms/js/components/admin/form_design/registrations/objectsapi/ObjectsApiOptionsFormFields.js index e80cb04c91..96b3d2af7e 100644 --- a/src/openforms/js/components/admin/form_design/registrations/objectsapi/ObjectsApiOptionsFormFields.js +++ b/src/openforms/js/components/admin/form_design/registrations/objectsapi/ObjectsApiOptionsFormFields.js @@ -43,6 +43,7 @@ const ObjectsApiOptionsFormFields = ({index, name, schema, formData, onChange}) draft.version = realVersion; if (realVersion === 2) { draft.variablesMapping = []; + draft.geometryVariableKey = ''; } else { delete draft.variablesMapping; } diff --git a/src/openforms/js/components/admin/form_design/registrations/objectsapi/ObjectsApiSummaryHandler.js b/src/openforms/js/components/admin/form_design/registrations/objectsapi/ObjectsApiSummaryHandler.js index 4b9ed90e9a..a132077f71 100644 --- a/src/openforms/js/components/admin/form_design/registrations/objectsapi/ObjectsApiSummaryHandler.js +++ b/src/openforms/js/components/admin/form_design/registrations/objectsapi/ObjectsApiSummaryHandler.js @@ -5,7 +5,8 @@ import {FormattedMessage} from 'react-intl'; * Returns the Objects API Registration summary for a specific variable. This only applies to V2 Options * * @typedef {{ - * variablesMapping: {variableKey: string, targetPath: string[]}[] + * variablesMapping: {variableKey: string, targetPath: string[]}[], + * geometryVariableKey: string, * }} ObjectsAPIV2Options * * @param {Object} p @@ -14,6 +15,18 @@ import {FormattedMessage} from 'react-intl'; * @returns {JSX.Element} - The summary, represented as the parts of the target path separated by '>' */ const ObjectsApiSummaryHandler = ({variable, backendOptions}) => { + const geometryVariableKey = backendOptions.geometryVariableKey; + + if (geometryVariableKey === variable.key) { + return ( + record.geometry}} + /> + ); + } + const variableMapping = backendOptions.variablesMapping.find( mapping => mapping.variableKey === variable.key ); diff --git a/src/openforms/js/components/admin/form_design/registrations/objectsapi/ObjectsApiVariableConfigurationEditor.js b/src/openforms/js/components/admin/form_design/registrations/objectsapi/ObjectsApiVariableConfigurationEditor.js index 7042ee1b1b..898d2b56ca 100644 --- a/src/openforms/js/components/admin/form_design/registrations/objectsapi/ObjectsApiVariableConfigurationEditor.js +++ b/src/openforms/js/components/admin/form_design/registrations/objectsapi/ObjectsApiVariableConfigurationEditor.js @@ -10,7 +10,7 @@ import {REGISTRATION_OBJECTS_TARGET_PATHS} from 'components/admin/form_design/co import Field from 'components/admin/forms/Field'; import Fieldset from 'components/admin/forms/Fieldset'; import FormRow from 'components/admin/forms/FormRow'; -import {TextInput} from 'components/admin/forms/Inputs'; +import {Checkbox, TextInput} from 'components/admin/forms/Inputs'; import Select, {LOADING_OPTION} from 'components/admin/forms/Select'; import ErrorMessage from 'components/errors/ErrorMessage'; import {post} from 'utils/fetch'; @@ -25,6 +25,7 @@ import {asJsonSchema} from './utils'; * objecttype: string; * objecttypeVersion: number; * variablesMapping: {variableKey: string, targetPath: string[]}[]; + * geometryVariableKey: string; * }} ObjectsAPIRegistrationBackendOptions * * @param {Object} p @@ -35,12 +36,16 @@ const ObjectsApiVariableConfigurationEditor = ({variable}) => { const {csrftoken} = useContext(APIContext); const [jsonSchemaVisible, toggleJsonSchemaVisible] = useToggle(false); - const {values: backendOptions, getFieldProps} = useFormikContext(); + const {values: backendOptions, getFieldProps, setFieldValue} = useFormikContext(); /** @type {ObjectsAPIRegistrationBackendOptions} */ - const {objecttype, objecttypeVersion, variablesMapping, version} = backendOptions; + const {objecttype, objecttypeVersion, geometryVariableKey, variablesMapping, version} = + backendOptions; + if (version !== 2) throw new Error('Not supported, must be config version 2.'); + const isGeometry = geometryVariableKey === variable.key; + // get the index of our variable in the mapping, if it exists let index = variablesMapping.findIndex( mappedVariable => mappedVariable.variableKey === variable.key @@ -110,6 +115,33 @@ const ObjectsApiVariableConfigurationEditor = ({variable}) => { /> + + + } + helpText={ + record.geometry}} + /> + } + name="geometryVariableKey" + disabled={!!mappedVariable.targetPath} + > + { + const newValue = event.target.checked ? variable.key : undefined; + setFieldValue('geometryVariableKey', newValue); + }} + /> + + { description="'JSON Schema target' label" /> } + disabled={isGeometry} > @@ -153,11 +187,10 @@ ObjectsApiVariableConfigurationEditor.propTypes = { }).isRequired, }; -const TargetPathSelect = ({name, index, choices, mappedVariable}) => { +const TargetPathSelect = ({name, index, choices, mappedVariable, disabled}) => { // To avoid having an incomplete variable mapping added in the `variablesMapping` array, // It is added only when an actual target path is selected. This way, having the empty // option selected means the variable is unmapped (hence the `arrayHelpers.remove` call below). - const { values: {variablesMapping}, getFieldProps, @@ -176,6 +209,7 @@ const TargetPathSelect = ({name, index, choices, mappedVariable}) => { allowBlank choices={choices} {...props} + disabled={disabled} value={JSON.stringify(props.value)} onChange={event => { if (event.target.value === '') {