From 792f39a8f9974786b3d7413ea4b720bfda728ee6 Mon Sep 17 00:00:00 2001 From: elforjani3 Date: Sat, 2 Jan 2021 14:52:49 +0200 Subject: [PATCH] fix : handle errors message with item categories. --- .../ItemCategoryFormDialogContent.js | 14 ++++++++++++-- client/src/lang/en/index.js | 9 ++++++--- .../itemCategories/itemsCategory.actions.js | 17 +++++++++++++---- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/client/src/containers/Dialogs/ItemCategoryDialog/ItemCategoryFormDialogContent.js b/client/src/containers/Dialogs/ItemCategoryDialog/ItemCategoryFormDialogContent.js index f5f5134c9..0fd4f7e03 100644 --- a/client/src/containers/Dialogs/ItemCategoryDialog/ItemCategoryFormDialogContent.js +++ b/client/src/containers/Dialogs/ItemCategoryDialog/ItemCategoryFormDialogContent.js @@ -80,8 +80,16 @@ function ItemCategoryFormDialogContent({ [], ); + const transformErrors = (errors, { setErrors }) => { + if (errors.find((error) => error.type === 'CATEGORY_NAME_EXISTS')) { + setErrors({ + name: formatMessage({ id: 'category_name_exists' }), + }); + } + }; + // Handles the form submit. - const handleFormSubmit = (values, { setSubmitting }) => { + const handleFormSubmit = (values, { setSubmitting, setErrors }) => { setSubmitting(true); const form = { ...values }; const afterSubmit = () => { @@ -100,7 +108,9 @@ function ItemCategoryFormDialogContent({ }); afterSubmit(response); }; - const onError = ({ response }) => { + + const onError = (errors) => { + transformErrors(errors, { setErrors }); setSubmitting(false); }; if (isNewMode) { diff --git a/client/src/lang/en/index.js b/client/src/lang/en/index.js index a7ecf2ddc..67c23222b 100644 --- a/client/src/lang/en/index.js +++ b/client/src/lang/en/index.js @@ -929,11 +929,14 @@ export default { 'Are you sure you want to activate this item? You will be able to inactivate it later', inactivate_item: 'Inactivate Item', activate_item: 'Activate Item', - all_payments:'All Payments', + all_payments: 'All Payments', hide_customizer: 'Hide Customizer', opening_quantity_: 'Opening quantity', opening_average_cost: 'Opening average cost', opening_cost_: 'Opening cost ', opening_date_: 'Opening date ', - no_results:'No results.' -}; + no_results: 'No results.', + the_invoice_cannot_be_deleted: + 'The invoice cannot be deleted cause has associated payment transactions', + category_name_exists:'Category name exists' + }; diff --git a/client/src/store/itemCategories/itemsCategory.actions.js b/client/src/store/itemCategories/itemsCategory.actions.js index 0c471e6b8..8487faaac 100644 --- a/client/src/store/itemCategories/itemsCategory.actions.js +++ b/client/src/store/itemCategories/itemsCategory.actions.js @@ -2,11 +2,20 @@ import ApiService from 'services/ApiService'; import t from 'store/types'; export const submitItemCategory = ({ form }) => { - return (dispatch) => { - return ApiService.post('item_categories', { ...form }); - }; -}; + return (dispatch) => + new Promise((resolve, reject) => { + ApiService.post('item_categories', form) + .then((response) => { + resolve(response); + }) + .catch((error) => { + const { response } = error; + const { data } = response; + reject(data?.errors); + }); + }); +}; export const fetchItemCategories = ({ query }) => { return (dispatch, getState) => new Promise((resolve, reject) => {