mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
handle Errors message api
This commit is contained in:
@@ -27,6 +27,7 @@ import withSettings from 'containers/Settings/withSettings';
|
||||
import { AppToaster, Row, Col } from 'components';
|
||||
import Dragzone from 'components/Dragzone';
|
||||
import useMedia from 'hooks/useMedia';
|
||||
import { ERROR } from 'common/errors';
|
||||
|
||||
import { compose, repeatValue } from 'utils';
|
||||
|
||||
@@ -204,6 +205,16 @@ const EstimateForm = ({
|
||||
: [];
|
||||
}, [estimate]);
|
||||
|
||||
const handleErrors = (errors, { setErrors }) => {
|
||||
if (errors.some((e) => e.type === ERROR.ESTIMATE_NUMBER_IS_NOT_UNQIUE)) {
|
||||
setErrors({
|
||||
estimate_number: formatMessage({
|
||||
id: 'estimate_number_is_not_unqiue',
|
||||
}),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const formik = useFormik({
|
||||
validationSchema,
|
||||
initialValues: {
|
||||
@@ -220,20 +231,25 @@ const EstimateForm = ({
|
||||
const requestForm = { ...form };
|
||||
|
||||
if (estimate && estimate.id) {
|
||||
requestEditEstimate(estimate.id, requestForm).then((response) => {
|
||||
AppToaster.show({
|
||||
message: formatMessage(
|
||||
{
|
||||
id: 'the_estimate_has_been_successfully_edited',
|
||||
},
|
||||
{ number: values.estimate_number },
|
||||
),
|
||||
intent: Intent.SUCCESS,
|
||||
requestEditEstimate(estimate.id, requestForm)
|
||||
.then((response) => {
|
||||
AppToaster.show({
|
||||
message: formatMessage(
|
||||
{
|
||||
id: 'the_estimate_has_been_successfully_edited',
|
||||
},
|
||||
{ number: values.estimate_number },
|
||||
),
|
||||
intent: Intent.SUCCESS,
|
||||
});
|
||||
setSubmitting(false);
|
||||
saveEstimateSubmit({ action: 'update', ...payload });
|
||||
resetForm();
|
||||
})
|
||||
.catch((errors) => {
|
||||
handleErrors(errors, { setErrors });
|
||||
setSubmitting(false);
|
||||
});
|
||||
setSubmitting(false);
|
||||
saveEstimateSubmit({ action: 'update', ...payload });
|
||||
resetForm();
|
||||
});
|
||||
} else {
|
||||
requestSubmitEstimate(requestForm)
|
||||
.then((response) => {
|
||||
@@ -249,12 +265,13 @@ const EstimateForm = ({
|
||||
saveEstimateSubmit({ action: 'new', ...payload });
|
||||
})
|
||||
.catch((errors) => {
|
||||
handleErrors(errors, { setErrors });
|
||||
setSubmitting(false);
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
formik.setFieldValue('estimate_number', estimateNumber);
|
||||
}, [estimateNumber]);
|
||||
@@ -302,10 +319,7 @@ const EstimateForm = ({
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={classNames(
|
||||
CLASSES.PAGE_FORM,
|
||||
CLASSES.PAGE_FORM_ESTIMATE,
|
||||
)}>
|
||||
<div className={classNames(CLASSES.PAGE_FORM, CLASSES.PAGE_FORM_ESTIMATE)}>
|
||||
<form onSubmit={formik.handleSubmit}>
|
||||
<EstimateFormHeader formik={formik} />
|
||||
<EntriesItemsTable
|
||||
@@ -338,7 +352,7 @@ const EstimateForm = ({
|
||||
/>
|
||||
</FormGroup>
|
||||
</Col>
|
||||
|
||||
|
||||
<Col md={4}>
|
||||
<Dragzone
|
||||
initialFiles={initialAttachmentFiles}
|
||||
@@ -366,7 +380,7 @@ export default compose(
|
||||
withDashboardActions,
|
||||
withMediaActions,
|
||||
withSettings(({ estimatesSettings }) => ({
|
||||
estimateNextNumber: estimatesSettings?.next_number,
|
||||
estimateNumberPrefix: estimatesSettings?.number_prefix,
|
||||
estimateNextNumber: estimatesSettings?.nextNumber,
|
||||
estimateNumberPrefix: estimatesSettings?.numberPrefix,
|
||||
})),
|
||||
)(EstimateForm);
|
||||
|
||||
Reference in New Issue
Block a user