Skip to content

Commit 9cf5dde

Browse files
committed
[#5074] Transform selectboxes data to list for JSON dump
1 parent 6d72916 commit 9cf5dde

File tree

5 files changed

+82
-6
lines changed

5 files changed

+82
-6
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

+43-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,37 @@ 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.includes(variable.key) || false}
110+
onChange={event => {
111+
const shouldBeTransformed = event.target.checked;
112+
const newTransformToList = shouldBeTransformed
113+
? [...transformToList, variable.key]
114+
: transformToList.filter(key => key !== variable.key);
115+
setFieldValue('transformToList', newTransformToList);
116+
setFieldValue(`transformToList.${variable.key}`, event.target.checked);
117+
}}
118+
/>
119+
</Field>
120+
</FormRow>
121+
)}
81122
</>
82123
);
83124
};
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

+12-1
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 _
@@ -70,6 +70,16 @@ class JSONDumpOptionsSerializer(JsonSchemaSerializerMixin, serializers.Serialize
7070
required=False,
7171
default=list,
7272
)
73+
transform_to_list = serializers.ListField(
74+
child=FormioVariableKeyField(),
75+
label=_("Transform to list"),
76+
required=False,
77+
default=list,
78+
help_text=_(
79+
"The components which need special handling concerning the shape of the "
80+
"data and needs 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[list]

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

+23-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def register_submission(
6565
if key in options["variables"]
6666
}
6767
values_schema = generate_json_schema(submission.form, options["variables"])
68-
post_process(values, values_schema, submission)
68+
transform_to_list = options.get("transform_to_list")
69+
post_process(values, values_schema, submission, transform_to_list)
6970

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

115116

116117
def post_process(
117-
values: JSONObject, schema: JSONObject, submission: Submission
118+
values: JSONObject,
119+
schema: JSONObject,
120+
submission: Submission,
121+
transform_to_list: list | None = None,
118122
) -> None:
119123
"""Post-process the values and schema.
120124
@@ -127,6 +131,8 @@ def post_process(
127131
:param values: Mapping from key to value of the data to be sent.
128132
:param schema: JSON schema describing ``values``.
129133
:param submission: The corresponding submission instance.
134+
:param transform_to_list: The list of the variables(keys) which need special handling
135+
concerning the shape of the data (need transformation to a list).
130136
"""
131137
state = submission.load_submission_value_variables_state()
132138

@@ -170,7 +176,12 @@ def post_process(
170176
assert component is not None
171177

172178
process_component(
173-
component, values, schema, attachments_dict, configuration_wrapper
179+
component,
180+
values,
181+
schema,
182+
attachments_dict,
183+
configuration_wrapper,
184+
transform_to_list=transform_to_list,
174185
)
175186

176187

@@ -181,6 +192,7 @@ def process_component(
181192
attachments: dict[str, list[SubmissionFileAttachment]],
182193
configuration_wrapper,
183194
key_prefix: str = "",
195+
transform_to_list: list | None = None,
184196
) -> None:
185197
"""Process a component.
186198
@@ -202,6 +214,8 @@ def process_component(
202214
:param key_prefix: If the component is part of an edit grid component, this key
203215
prefix includes the parent key and the index of the component as it appears in the
204216
submitted data list of that edit grid component.
217+
:param transform_to_list: The list of the variables(keys) which need special handling
218+
concerning the shape of the data (need transformation to a list).
205219
"""
206220
key = component["key"]
207221

@@ -264,6 +278,12 @@ def process_component(
264278
if not values[key]:
265279
schema["properties"][key]["required"] = [] # type: ignore
266280

281+
if transform_to_list and key in transform_to_list and values[key]:
282+
schema["properties"][key]["type"] = "array" # type: ignore
283+
values[key] = [
284+
option for option, is_selected in values[key].items() if is_selected # type: ignore
285+
]
286+
267287
case {"type": "editgrid"}:
268288
# Note: the schema actually only needs to be processed once for each child
269289
# component, but will be processed for each submitted repeating group entry

0 commit comments

Comments
 (0)