Skip to content

Commit

Permalink
fix: use onChangeFormData to change form data in the state
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarde committed Dec 12, 2023
1 parent 329b806 commit ed8cf11
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions src/components/FormView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ const messages = defineMessages({
},
});

const getFieldsToSendWithValue = (subblock) => {
const FieldSchema = config.blocks.blocksConfig.form.fieldSchema;

var fields_to_send = [];
var fieldSchemaProperties = FieldSchema(subblock)?.properties;
for (var key in fieldSchemaProperties) {
if (fieldSchemaProperties[key].send_to_backend) {
fields_to_send.push(key);
}
}

var fields_to_send_with_value = Object.assign(
{},
...fields_to_send.map((field) => {
return {
[field]: subblock[field],
};
}),
);

return fields_to_send_with_value;
};

const FormView = ({
formState,
formErrors,
Expand All @@ -54,8 +77,6 @@ const FormView = ({
id,
}) => {
const intl = useIntl();
const FieldSchema = config.blocks.blocksConfig.form.fieldSchema;

const userId = useSelector((state) =>
state.userSession.token ? jwtDecode(state.userSession.token).sub : '',
);
Expand All @@ -75,8 +96,15 @@ const FormView = ({
subblock.user_email_as_default
) {
const name = getFieldName(subblock.label, subblock.id);
if (!formData.hasOwnProperty(name))
formData[name] = { value: curUserEmail };
if (!formData.hasOwnProperty(name)) {
const fields_to_send_with_value = getFieldsToSendWithValue(subblock);
onChangeFormData(
subblock.id,
name,
curUserEmail,
fields_to_send_with_value,
);
}
}
});
}
Expand Down Expand Up @@ -144,24 +172,8 @@ const FormView = ({
))}
{data.subblocks?.map((subblock, index) => {
let name = getFieldName(subblock.label, subblock.id);

var fields_to_send = [];
var fieldSchemaProperties = FieldSchema(subblock)?.properties;
for (var key in fieldSchemaProperties) {
if (fieldSchemaProperties[key].send_to_backend) {
fields_to_send.push(key);
}
}

var fields_to_send_with_value = Object.assign(
{},
...fields_to_send.map((field) => {
return {
[field]: subblock[field],
};
}),
);

const fields_to_send_with_value =
getFieldsToSendWithValue(subblock);
return (
<Grid.Row key={'row' + index}>
<Grid.Column>
Expand Down

0 comments on commit ed8cf11

Please sign in to comment.