Skip to content

Commit

Permalink
chore: updated volto-form-block customizations
Browse files Browse the repository at this point in the history
  • Loading branch information
SaraBianchi committed Jun 24, 2024
1 parent ad65f0c commit 00e44c7
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 58 deletions.
78 changes: 78 additions & 0 deletions src/customizations/volto-form-block/components/FormResult.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
CUSTOMIZATIONS:
- used design-react-kit components to render form result
*/
import React from 'react';
import { useIntl, defineMessages } from 'react-intl';
import { Button, Alert } from 'design-react-kit/dist/design-react-kit';
import { getFieldName } from 'volto-form-block/components/utils';

const messages = defineMessages({
success: {
id: 'form_submit_success',
defaultMessage: 'Sent!',
},
reset: {
id: 'form_reset',
defaultMessage: 'Clear',
},
});

const alertTransition = {
appear: true,
baseClass: 'fade',
baseClassActive: 'show',
enter: true,
exit: true,
in: true,
mountOnEnter: false,
tag: 'div',
timeout: 150,
unmountOnExit: true,
};

/* Function that replaces variables from the user customized message */
const replaceMessage = (text, sent_data) => {
let i = 0;
while (i < sent_data.length) {
let idField = getFieldName(sent_data[i].label, sent_data[i].field_id);
text = text.replaceAll('${' + idField + '}', sent_data[i].value ?? '');
i++;
}
text = text.replaceAll(/\$\{[^}]*\}/gm, ''); //replace empty fields with nothing
text = text.replaceAll('\n', '<br/>');
return text;
};

const FormResult = ({ formState, data, resetFormState }) => {
const intl = useIntl();
return (
<Alert color="success" fade isOpen tag="div" transition={alertTransition}>
<h4>{intl.formatMessage(messages.success)}</h4>
<br />
{/* Custom message */}
{data.send_message && (
<>
<p
dangerouslySetInnerHTML={{
__html: replaceMessage(data.send_message, formState.result.data),
}}
/>
<br />
</>
)}
<Button
color="primary"
outline
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
resetFormState();
}}
>
{intl.formatMessage(messages.reset)}
</Button>
</Alert>
);
};
export default FormResult;
136 changes: 78 additions & 58 deletions src/customizations/volto-form-block/components/FormView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ import {
CardBody,
Row,
Col,
Button,
Alert,
Progress,
} from 'design-react-kit/dist/design-react-kit';
// eslint-disable-next-line import/no-unresolved
import { getFieldName } from 'volto-form-block/components/utils';
// eslint-disable-next-line import/no-unresolved
import Field from 'volto-form-block/components/Field';
import {
OTPWidget,
OTP_FIELDNAME_EXTENDER,
Button,
} from 'volto-form-block/components/Widget';
import { FormResult } from 'volto-form-block/components';
// eslint-disable-next-line import/no-unresolved
import config from '@plone/volto/registry';

Expand Down Expand Up @@ -64,6 +69,8 @@ const FormView = ({
captcha,
id,
getErrorMessage,
path,
block_id,
}) => {
const intl = useIntl();
const alertTransition = {
Expand All @@ -86,23 +93,6 @@ const FormView = ({
return formErrors?.filter((e) => e.field === field).length === 0;
};

/* Function that replaces variables from the user customized message */
const replaceMessage = (text) => {
let i = 0;
while (i < data.subblocks.length) {
let idField = getFieldName(
data.subblocks[i].label,
data.subblocks[i].field_id,
);
text = text.replaceAll(
'${' + idField + '}',
formData[idField]?.value || '',
);
i++;
}
return text;
};

var FieldSchema = config.blocks.blocksConfig.form.fieldSchema;
var fieldSchemaProperties = FieldSchema()?.properties;
var fields_to_send = [];
Expand All @@ -117,6 +107,26 @@ const FormView = ({
onSubmit(e);
};

const getFieldsToSendWithValue = (subblock) => {
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;
};

return (
<div className="block form">
<div className="public-ui">
Expand All @@ -128,38 +138,11 @@ const FormView = ({
<Card className="card-bg rounded py-3" noWrapper={false} tag="div">
<CardBody tag="div">
{formState.result ? (
<Alert
color="success"
fade
isOpen
tag="div"
transition={alertTransition}
>
<h4>{intl.formatMessage(messages.success)}</h4>
<br />
{/* Custom message */}
{data.send_message && (
<>
<p
dangerouslySetInnerHTML={{
__html: replaceMessage(data.send_message),
}}
/>
<br />
</>
)}
<Button
color="primary"
outline
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
resetFormState();
}}
>
{intl.formatMessage(messages.reset)}
</Button>
</Alert>
<FormResult
formState={formState}
data={data}
resetFormState={resetFormState}
/>
) : (
<form
onSubmit={submit}
Expand Down Expand Up @@ -201,14 +184,8 @@ const FormView = ({
)}
{data.subblocks.map((subblock, index) => {
let name = getFieldName(subblock.label, subblock.id);
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 (
<Row key={'row' + index}>
Expand Down Expand Up @@ -238,6 +215,49 @@ const FormView = ({
);
})}

{/*OTP*/}
{data.subblocks
.filter((subblock) => subblock.use_as_bcc)
.map((subblock, index) => {
const fieldName = getFieldName(
subblock.label,
subblock.id,
);
const name = fieldName + OTP_FIELDNAME_EXTENDER;
const fieldValue = formData[fieldName]?.value;
const value = formData[fieldName]?.otp;
const fields_to_send_with_value =
getFieldsToSendWithValue(subblock);

return (
<Row key={'row_otp' + index}>
<Col className="py-2">
<OTPWidget
{...subblock}
fieldValue={fieldValue}
onChange={(field, value) => {
onChangeFormData(
subblock.id,
fieldName,
fieldValue,
{
...fields_to_send_with_value,
otp: value,
},
);
}}
value={value}
valid={isValidField(name)}
errorMessage={getErrorMessage(name)}
formHasErrors={formErrors?.length > 0}
path={path}
block_id={block_id}
/>
</Col>
</Row>
);
})}

{enableCaptcha && <>{captcha.render()}</>}

{formErrors.length > 0 && (
Expand Down
50 changes: 50 additions & 0 deletions src/customizations/volto-form-block/components/Widget/Button.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* customizations:
* used design-react-kit button
*
*
* Button component.
* This is a wrapper for Buttons, to eventually customize Button component if you don't like to use semantic-ui, for example.
* @module components/Widget/OTPWidget
*/

import { Button as DesignButton } from 'design-react-kit/dist/design-react-kit';

const Button = (props) => {
let _props = { ...props };
if (props.primary) {
_props.color = 'primary';
delete _props.primary;
}
if (props.secondary) {
_props.color = 'secondary';
delete _props.secondary;
}
if (props.basic) {
_props.outline = true;
delete _props.basic;
}
if (props.size) {
switch (props.size) {
case 'mini':
case 'tiny':
case 'small':
_props.size = 'xs';
break;
case 'medium':
case 'large':
_props.size = 'sm';
break;
case 'big':
case 'huge':
case 'massive':
_props.size = 'lg';
break;
default:
break;
}
}
return <DesignButton {..._props} />;
};

export default Button;

0 comments on commit 00e44c7

Please sign in to comment.