Skip to content

Commit 3e830d8

Browse files
vaszigsergei-maertens
authored andcommitted
[#5074] Transform the data for the JSON dump in the variables tab
1 parent 8a47c04 commit 3e830d8

File tree

7 files changed

+82
-11
lines changed

7 files changed

+82
-11
lines changed

src/openforms/js/components/admin/form_design/registrations/json_dump/JSONDumpOptionsForm.js

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const JSONDumpOptionsForm = ({name, label, schema, formData, onChange}) => {
5858
variables: [],
5959
fixedMetadataVariables: fixedMetadataVariables || [],
6060
additionalMetadataVariables: [],
61+
transformToList: {},
6162
...formData,
6263
}}
6364
onSubmit={values => onChange({formData: values})}

src/openforms/js/components/admin/form_design/registrations/json_dump/JSONDumpVariableConfigurationEditor.js

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
import {useFormikContext} from 'formik';
22
import PropTypes from 'prop-types';
3-
import React from 'react';
3+
import React, {useContext} from 'react';
44
import {FormattedMessage} from 'react-intl';
55

6+
import {FormContext} from 'components/admin/form_design/Context';
67
import {VARIABLE_SOURCES} from 'components/admin/form_design/variables/constants';
78
import {getVariableSource} from 'components/admin/form_design/variables/utils';
89
import Field from 'components/admin/forms/Field';
910
import FormRow from 'components/admin/forms/FormRow';
1011
import {Checkbox} from 'components/admin/forms/Inputs';
1112

13+
import {CUSTOM_COMPONENTS_SCHEMA} from '../utils';
14+
1215
const JSONDumpVariableConfigurationEditor = ({variable}) => {
1316
const {
14-
values: {variables = [], additionalMetadataVariables = [], fixedMetadataVariables = []},
17+
values: {
18+
variables = [],
19+
additionalMetadataVariables = [],
20+
fixedMetadataVariables = [],
21+
transformToList = {},
22+
},
1523
setFieldValue,
1624
} = useFormikContext();
25+
const {components} = useContext(FormContext);
1726
const isIncluded = variables.includes(variable.key);
1827
const isRequiredInMetadata = fixedMetadataVariables.includes(variable.key);
1928
const isInAdditionalMetadata = additionalMetadataVariables.includes(variable.key);
29+
const componentType = components[variable?.key]?.type;
2030

2131
return (
2232
<>
@@ -78,6 +88,30 @@ const JSONDumpVariableConfigurationEditor = ({variable}) => {
7888
/>
7989
</Field>
8090
</FormRow>
91+
92+
{componentType in CUSTOM_COMPONENTS_SCHEMA && (
93+
<FormRow>
94+
<Field name={`transformToList.${variable.key}`}>
95+
<Checkbox
96+
name="transformToListCheckbox"
97+
label={
98+
<FormattedMessage
99+
defaultMessage="Transform to list"
100+
description="'Transform to list' checkbox label"
101+
/>
102+
}
103+
helpText={
104+
<FormattedMessage
105+
description="'Transform to list' checkbox help text"
106+
defaultMessage="Whether to transform the data of the component to a list or not (depends on the component type)"
107+
/>
108+
}
109+
checked={transformToList?.[variable.key] || false}
110+
onChange={e => setFieldValue(`transformToList.${variable.key}`, e.target.checked)}
111+
/>
112+
</Field>
113+
</FormRow>
114+
)}
81115
</>
82116
);
83117
};

src/openforms/js/components/admin/form_design/registrations/objectsapi/GenericObjectsApiVariableConfigurationEditor.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import {Checkbox} from 'components/admin/forms/Inputs';
1212
import {TargetPathSelect} from 'components/admin/forms/objects_api';
1313
import ErrorMessage from 'components/errors/ErrorMessage';
1414

15+
import {CUSTOM_COMPONENTS_SCHEMA} from '../utils';
1516
import {asJsonSchema} from './utils';
16-
import {CUSTOM_COMPONENTS_SCHEMA, fetchTargetPaths} from './utils';
17+
import {fetchTargetPaths} from './utils';
1718

1819
/**
1920
* Hack-ish way to manage the variablesMapping state for one particular entry.

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ const FORMAT_TYPE_MAP = {
1616
date: 'date',
1717
};
1818

19-
const CUSTOM_COMPONENTS_SCHEMA = {selectboxes: [{type: 'array'}]};
20-
2119
/**
2220
* Return a JSON Schema definition matching the provided variable.
2321
* @param {Object} variable - The current variable
@@ -83,4 +81,4 @@ const fetchTargetPaths = async (
8381
return response.data;
8482
};
8583

86-
export {asJsonSchema, fetchTargetPaths, CUSTOM_COMPONENTS_SCHEMA};
84+
export {asJsonSchema, fetchTargetPaths};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const CUSTOM_COMPONENTS_SCHEMA = {selectboxes: [{type: 'array'}]};
2+
3+
export {CUSTOM_COMPONENTS_SCHEMA};

src/openforms/registrations/contrib/json_dump/config.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TypedDict
1+
from typing import NotRequired, TypedDict
22

33
from django.core.exceptions import ValidationError
44
from django.utils.translation import gettext_lazy as _
@@ -7,8 +7,9 @@
77
from zgw_consumers.constants import APITypes
88
from zgw_consumers.models import Service
99

10-
from openforms.api.fields import PrimaryKeyRelatedAsChoicesField
10+
from openforms.api.fields import JSONFieldWithSchema, PrimaryKeyRelatedAsChoicesField
1111
from openforms.formio.api.fields import FormioVariableKeyField
12+
from openforms.typing import JSONObject
1213
from openforms.utils.mixins import JsonSchemaSerializerMixin
1314

1415

@@ -70,6 +71,15 @@ class JSONDumpOptionsSerializer(JsonSchemaSerializerMixin, serializers.Serialize
7071
required=False,
7172
default=list,
7273
)
74+
transform_to_list = JSONFieldWithSchema(
75+
label=_("transform to list"),
76+
default=dict,
77+
required=False,
78+
help_text=_(
79+
"The components which need special handling concerning the shape of the data "
80+
"and need to be transformed to a list."
81+
),
82+
)
7383

7484

7585
class JSONDumpOptions(TypedDict):
@@ -85,3 +95,4 @@ class JSONDumpOptions(TypedDict):
8595
variables: list[str]
8696
fixed_metadata_variables: list[str]
8797
additional_metadata_variables: list[str]
98+
transform_to_list: NotRequired[JSONObject]

src/openforms/registrations/contrib/json_dump/plugin.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from zgw_consumers.client import build_client
1313

14+
from openforms.api.utils import camel_to_underscore
1415
from openforms.formio.constants import DataSrcOptions
1516
from openforms.formio.service import rewrite_formio_components
1617
from openforms.formio.typing import (
@@ -65,7 +66,8 @@ def register_submission(
6566
if key in options["variables"]
6667
}
6768
values_schema = generate_json_schema(submission.form, options["variables"])
68-
post_process(values, values_schema, submission)
69+
transform_to_list = options.get("transform_to_list")
70+
post_process(values, values_schema, submission, transform_to_list)
6971

7072
# Metadata
7173
# Note: as the metadata contains only static variables no post-processing is
@@ -114,7 +116,10 @@ def get_variables(self) -> list[FormVariable]: # pragma: no cover
114116

115117

116118
def post_process(
117-
values: JSONObject, schema: JSONObject, submission: Submission
119+
values: JSONObject,
120+
schema: JSONObject,
121+
submission: Submission,
122+
transform_to_list: JSONObject | None = None,
118123
) -> None:
119124
"""Post-process the values and schema.
120125
@@ -170,7 +175,12 @@ def post_process(
170175
assert component is not None
171176

172177
process_component(
173-
component, values, schema, attachments_dict, configuration_wrapper
178+
component,
179+
values,
180+
schema,
181+
attachments_dict,
182+
configuration_wrapper,
183+
transform_to_list=transform_to_list,
174184
)
175185

176186

@@ -181,6 +191,7 @@ def process_component(
181191
attachments: dict[str, list[SubmissionFileAttachment]],
182192
configuration_wrapper,
183193
key_prefix: str = "",
194+
transform_to_list: JSONObject | None = None,
184195
) -> None:
185196
"""Process a component.
186197
@@ -264,6 +275,18 @@ def process_component(
264275
if not values[key]:
265276
schema["properties"][key]["required"] = [] # type: ignore
266277

278+
if (
279+
transform_to_list
280+
and (key_to_transform := camel_to_underscore(key)) in transform_to_list
281+
and transform_to_list[key_to_transform]
282+
and values[key]
283+
):
284+
schema["properties"][key]["type"] = "array" # type: ignore
285+
286+
values[key] = [
287+
option for option, is_selected in values[key].items() if is_selected # type: ignore
288+
]
289+
267290
case {"type": "editgrid"}:
268291
# Note: the schema actually only needs to be processed once for each child
269292
# component, but will be processed for each submitted repeating group entry

0 commit comments

Comments
 (0)