diff --git a/client/src/common/errors.js b/client/src/common/errors.js index e69de29bb..34db1734c 100644 --- a/client/src/common/errors.js +++ b/client/src/common/errors.js @@ -0,0 +1,14 @@ +export const ERROR = { + // Sales Estimates + ESTIMATE_NUMBER_IS_NOT_UNQIUE: 'ESTIMATE.NUMBER.IS.NOT.UNQIUE', + + // Sales Invoices + SALE_INVOICE_NUMBER_IS_EXISTS: 'SALE.INVOICE.NUMBER.IS.EXISTS', + SALE_INVOICE_NO_NOT_UNIQUE: 'SALE_INVOICE_NO_NOT_UNIQUE', + + // Sales Receipts + SALE_RECEIPT_NUMBER_NOT_UNIQUE: 'SALE_RECEIPT_NUMBER_NOT_UNIQUE', + + // Bills + BILL_NUMBER_EXISTS: 'BILL.NUMBER.EXISTS', +}; diff --git a/client/src/containers/Purchases/Bill/BillForm.js b/client/src/containers/Purchases/Bill/BillForm.js index 4c2954245..15b2c5904 100644 --- a/client/src/containers/Purchases/Bill/BillForm.js +++ b/client/src/containers/Purchases/Bill/BillForm.js @@ -24,7 +24,7 @@ import withBillDetail from './withBillDetail'; import { AppToaster } from 'components'; import useMedia from 'hooks/useMedia'; - +import { ERROR } from 'common/errors'; import { compose, repeatValue } from 'utils'; const MIN_LINES_NUMBER = 5; @@ -137,7 +137,6 @@ function BillForm({ [], ); - const defaultInitialValues = useMemo( () => ({ vendor_id: '', @@ -192,6 +191,16 @@ function BillForm({ : []; }, [bill]); + const handleErrors = (errors, { setErrors }) => { + if (errors.some((e) => e.type === ERROR.BILL_NUMBER_EXISTS)) { + setErrors({ + bill_number: formatMessage({ + id: 'bill_number_exists', + }), + }); + } + }; + const formik = useFormik({ // enableReinitialize: true, validationSchema, @@ -222,7 +231,8 @@ function BillForm({ saveBillSubmit({ action: 'update', ...payload }); resetForm(); }) - .catch((error) => { + .catch((errors) => { + handleErrors(errors, { setErrors }); setSubmitting(false); }); } else { @@ -240,6 +250,7 @@ function BillForm({ resetForm(); }) .catch((errors) => { + handleErrors(errors, { setErrors }); setSubmitting(false); }); } diff --git a/client/src/containers/Sales/Estimate/EstimateForm.js b/client/src/containers/Sales/Estimate/EstimateForm.js index c1df70b5c..97ead1a26 100644 --- a/client/src/containers/Sales/Estimate/EstimateForm.js +++ b/client/src/containers/Sales/Estimate/EstimateForm.js @@ -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 ( -