Skip to content

Commit

Permalink
Create/edit forms: fix cancel button
Browse files Browse the repository at this point in the history
  • Loading branch information
Pineirin committed Sep 26, 2022
1 parent 7af2b8f commit 44b6f46
Showing 1 changed file with 43 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ import { ErrorMessage } from "../ui_messages/messages";
import isEmpty from "lodash/isEmpty";
import { GenerateForm } from "./GenerateForm";
import { deserializeFieldErrors } from "../components/utils";
import { i18next } from "@translations/invenio_administration/i18next";
import mapValues from "lodash/mapValues";

export class AdminForm extends Component {
constructor(props) {
super(props);
const { resource } = props;
const { resource, formFields } = props;

this.state = {
error: undefined,
formData: resource ? resource : {},
formData: resource
? resource
: mapValues(formFields, function (value) {
return "";
}),
};
}

Expand Down Expand Up @@ -67,55 +73,51 @@ export class AdminForm extends Component {
}
};

onCancel = () => {
const { resource } = this.props;
this.setState({ formData: resource });
};

resetErrorState = () => {
this.setState({ error: undefined });
};

render() {
const { resourceSchema, create, formFields } = this.props;
const { formData, error } = this.state;

return (
<Formik initialValues={formData} onSubmit={this.onSubmit}>
{(props) => (
<SemanticForm
as={Form}
onSubmit={(e) => {
e.preventDefault();
props.handleSubmit();
}}
>
<GenerateForm
formFields={formFields}
jsonSchema={resourceSchema}
create={create}
/>
{!isEmpty(error) && (
<ErrorMessage {...error} removeNotification={this.resetErrorState} />
)}
<Button
type="button"
onClick={this.onCancel}
loading={props.isSubmitting}
disabled={props.isSubmitting}
>
Cancel
</Button>
<Button
type="submit"
primary
loading={props.isSubmitting}
disabled={props.isSubmitting}
{(props) => {
return (
<SemanticForm
as={Form}
onSubmit={(e) => {
e.preventDefault();
props.handleSubmit();
}}
>
Save
</Button>
</SemanticForm>
)}
<GenerateForm
formFields={formFields}
jsonSchema={resourceSchema}
create={create}
/>
{!isEmpty(error) && (
<ErrorMessage {...error} removeNotification={this.resetErrorState} />
)}
<Button
type="button"
onClick={() => props.resetForm()}
loading={props.isSubmitting}
disabled={props.isSubmitting}
>
{i18next.t("Cancel")}
</Button>
<Button
type="submit"
primary
loading={props.isSubmitting}
disabled={props.isSubmitting}
>
{i18next.t("Save")}
</Button>
</SemanticForm>
);
}}
</Formik>
);
}
Expand Down

0 comments on commit 44b6f46

Please sign in to comment.