Skip to content

Commit

Permalink
feat: add reset form
Browse files Browse the repository at this point in the history
  • Loading branch information
nzambello committed Mar 18, 2021
1 parent e7f4864 commit d07a79f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/components/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const Form = ({ data, id, path }) => {

if (isAttachment) {
attachments[name] = formData[name].value;
delete formData[name];
}
}
});
Expand All @@ -137,6 +138,17 @@ const Form = ({ data, id, path }) => {
}
};

const resetFormState = () => {
setFormData({
...static_fields.reduce(
(acc, field) => ({ ...acc, [getFieldName(field.label)]: field }),
{},
),
});
console.log('reset state');
setFormState({ type: FORM_STATES.normal });
};

useEffect(() => {
if (submitResults?.loaded) {
setFormState({
Expand All @@ -161,6 +173,7 @@ const Form = ({ data, id, path }) => {
onChangeFormData={onChangeFormData}
data={data}
onSubmit={submit}
resetFormState={resetFormState}
/>
);
};
Expand Down
11 changes: 11 additions & 0 deletions src/components/FormView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const messages = defineMessages({
id: 'form_empty_values_validation',
defaultMessage: 'Fill in the required fields',
},
reset: {
id: 'form_reset',
defaultMessage: 'Clear',
},
});

const FormView = ({
Expand All @@ -38,6 +42,7 @@ const FormView = ({
onChangeFormData,
data,
onSubmit,
resetFormState,
}) => {
const intl = useIntl();

Expand Down Expand Up @@ -69,13 +74,19 @@ const FormView = ({
{intl.formatMessage(messages.error)}
</Message.Header>
<p>{formState.error}</p>
<Button secondary type="clear" onClick={resetFormState}>
{intl.formatMessage(messages.reset)}
</Button>
</Message>
) : formState.result ? (
<Message positive role="alert">
<Message.Header as="h4">
{intl.formatMessage(messages.success)}
</Message.Header>
<p>{formState.result}</p>
<Button secondary type="clear" onClick={resetFormState}>
{intl.formatMessage(messages.reset)}
</Button>
</Message>
) : (
<Form loading={formState.loading} onSubmit={onSubmit} method="post">
Expand Down

0 comments on commit d07a79f

Please sign in to comment.