mirror of
https://github.com/ansible/awx.git
synced 2026-03-18 17:37:30 -02:30
add JT form error feedback from API errors
This commit is contained in:
@@ -1,11 +1,15 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { useHistory } from 'react-router-dom';
|
import { useHistory } from 'react-router-dom';
|
||||||
|
import { t } from '@lingui/macro';
|
||||||
|
import { withI18n } from '@lingui/react';
|
||||||
import { Card } from '@patternfly/react-core';
|
import { Card } from '@patternfly/react-core';
|
||||||
import { CardBody } from '@components/Card';
|
import { CardBody } from '@components/Card';
|
||||||
|
import ErrorDetail from '@components/ErrorDetail';
|
||||||
|
import AlertModal from '@components/AlertModal';
|
||||||
import JobTemplateForm from '../shared/JobTemplateForm';
|
import JobTemplateForm from '../shared/JobTemplateForm';
|
||||||
import { JobTemplatesAPI } from '@api';
|
import { JobTemplatesAPI } from '@api';
|
||||||
|
|
||||||
function JobTemplateAdd() {
|
function JobTemplateAdd({ i18n }) {
|
||||||
const [formSubmitError, setFormSubmitError] = useState(null);
|
const [formSubmitError, setFormSubmitError] = useState(null);
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
|
||||||
@@ -31,6 +35,10 @@ function JobTemplateAdd() {
|
|||||||
]);
|
]);
|
||||||
history.push(`/templates/${type}/${id}/details`);
|
history.push(`/templates/${type}/${id}/details`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// check for field-specific errors from API
|
||||||
|
if (error.response?.data && typeof error.response.data === 'object') {
|
||||||
|
throw error.response.data;
|
||||||
|
}
|
||||||
setFormSubmitError(error);
|
setFormSubmitError(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,9 +76,19 @@ function JobTemplateAdd() {
|
|||||||
handleSubmit={handleSubmit}
|
handleSubmit={handleSubmit}
|
||||||
/>
|
/>
|
||||||
</CardBody>
|
</CardBody>
|
||||||
{formSubmitError ? <div>formSubmitError</div> : ''}
|
{formSubmitError && (
|
||||||
|
<AlertModal
|
||||||
|
variant="danger"
|
||||||
|
title={i18n._(t`Error!`)}
|
||||||
|
isOpen={formSubmitError}
|
||||||
|
onClose={() => setFormSubmitError(null)}
|
||||||
|
>
|
||||||
|
{i18n._(t`An error occurred when saving`)}
|
||||||
|
<ErrorDetail error={formSubmitError} />
|
||||||
|
</AlertModal>
|
||||||
|
)}
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default JobTemplateAdd;
|
export default withI18n()(JobTemplateAdd);
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
/* eslint react/no-unused-state: 0 */
|
/* eslint react/no-unused-state: 0 */
|
||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import { withRouter, Redirect } from 'react-router-dom';
|
import { withRouter, Redirect } from 'react-router-dom';
|
||||||
|
import { t } from '@lingui/macro';
|
||||||
|
import { withI18n } from '@lingui/react';
|
||||||
import { CardBody } from '@components/Card';
|
import { CardBody } from '@components/Card';
|
||||||
import ContentError from '@components/ContentError';
|
import ContentError from '@components/ContentError';
|
||||||
import ContentLoading from '@components/ContentLoading';
|
import ContentLoading from '@components/ContentLoading';
|
||||||
|
import ErrorDetail from '@components/ErrorDetail';
|
||||||
|
import AlertModal from '@components/AlertModal';
|
||||||
import { JobTemplatesAPI, ProjectsAPI } from '@api';
|
import { JobTemplatesAPI, ProjectsAPI } from '@api';
|
||||||
import { JobTemplate } from '@types';
|
import { JobTemplate } from '@types';
|
||||||
import { getAddedAndRemoved } from '@util/lists';
|
import { getAddedAndRemoved } from '@util/lists';
|
||||||
@@ -113,8 +117,12 @@ class JobTemplateEdit extends Component {
|
|||||||
this.submitCredentials(credentials),
|
this.submitCredentials(credentials),
|
||||||
]);
|
]);
|
||||||
history.push(this.detailsUrl);
|
history.push(this.detailsUrl);
|
||||||
} catch (formSubmitError) {
|
} catch (error) {
|
||||||
this.setState({ formSubmitError });
|
// check for field-specific errors from API
|
||||||
|
if (error.response?.data && typeof error.response.data === 'object') {
|
||||||
|
throw error.response.data;
|
||||||
|
}
|
||||||
|
this.setState({ formSubmitError: error });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,7 +181,7 @@ class JobTemplateEdit extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { template } = this.props;
|
const { template, i18n } = this.props;
|
||||||
const {
|
const {
|
||||||
contentError,
|
contentError,
|
||||||
formSubmitError,
|
formSubmitError,
|
||||||
@@ -210,10 +218,20 @@ class JobTemplateEdit extends Component {
|
|||||||
handleSubmit={this.handleSubmit}
|
handleSubmit={this.handleSubmit}
|
||||||
relatedProjectPlaybooks={relatedProjectPlaybooks}
|
relatedProjectPlaybooks={relatedProjectPlaybooks}
|
||||||
/>
|
/>
|
||||||
{formSubmitError ? <div> error </div> : null}
|
{formSubmitError && (
|
||||||
|
<AlertModal
|
||||||
|
variant="danger"
|
||||||
|
title={i18n._(t`Error!`)}
|
||||||
|
isOpen={formSubmitError}
|
||||||
|
onClose={() => this.setState({ formSubmitError: null })}
|
||||||
|
>
|
||||||
|
{i18n._(t`An error occurred when saving`)}
|
||||||
|
<ErrorDetail error={formSubmitError} />
|
||||||
|
</AlertModal>
|
||||||
|
)}
|
||||||
</CardBody>
|
</CardBody>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withRouter(JobTemplateEdit);
|
export default withI18n()(withRouter(JobTemplateEdit));
|
||||||
|
|||||||
@@ -163,6 +163,7 @@ class JobTemplateForm extends Component {
|
|||||||
setFieldValue,
|
setFieldValue,
|
||||||
i18n,
|
i18n,
|
||||||
template,
|
template,
|
||||||
|
formik,
|
||||||
} = this.props;
|
} = this.props;
|
||||||
|
|
||||||
const jobTypeOptions = [
|
const jobTypeOptions = [
|
||||||
@@ -631,7 +632,13 @@ const FormikApp = withFormik({
|
|||||||
credentials: summary_fields.credentials || [],
|
credentials: summary_fields.credentials || [],
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
handleSubmit: (values, { props }) => props.handleSubmit(values),
|
handleSubmit: async (values, { props, setErrors }) => {
|
||||||
|
try {
|
||||||
|
await props.handleSubmit(values);
|
||||||
|
} catch (errors) {
|
||||||
|
setErrors(errors);
|
||||||
|
}
|
||||||
|
},
|
||||||
})(JobTemplateForm);
|
})(JobTemplateForm);
|
||||||
|
|
||||||
export { JobTemplateForm as _JobTemplateForm };
|
export { JobTemplateForm as _JobTemplateForm };
|
||||||
|
|||||||
Reference in New Issue
Block a user