From 792c96c344f57cec0471dee319445b869feaec12 Mon Sep 17 00:00:00 2001 From: elforjani3 Date: Sat, 2 Jan 2021 13:43:40 +0200 Subject: [PATCH 1/4] fix : handle errors message in sale invoice --- .../containers/Sales/Invoice/InvoiceList.js | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/client/src/containers/Sales/Invoice/InvoiceList.js b/client/src/containers/Sales/Invoice/InvoiceList.js index 053c71145..7e031be48 100644 --- a/client/src/containers/Sales/Invoice/InvoiceList.js +++ b/client/src/containers/Sales/Invoice/InvoiceList.js @@ -78,17 +78,36 @@ function InvoiceList({ setDeleteInvoice(false); }, [setDeleteInvoice]); - // handleConfirm delete invoice - const handleConfirmInvoiceDelete = useCallback(() => { - requestDeleteInvoice(deleteInvoice.id).then(() => { + const handleDeleteErrors = (errors) => { + if ( + errors.find( + (error) => error.type === 'INVOICE_HAS_ASSOCIATED_PAYMENT_ENTRIES', + ) + ) { AppToaster.show({ message: formatMessage({ - id: 'the_invocie_has_been_successfully_deleted', + id: 'the_invoice_cannot_be_deleted', }), - intent: Intent.SUCCESS, + intent: Intent.DANGER, + }); + } + }; + + // handleConfirm delete invoice + const handleConfirmInvoiceDelete = useCallback(() => { + requestDeleteInvoice(deleteInvoice.id) + .then(() => { + AppToaster.show({ + message: formatMessage({ + id: 'the_invocie_has_been_successfully_deleted', + }), + intent: Intent.SUCCESS, + }); + }) + .catch((errors) => { + handleDeleteErrors(errors); + setDeleteInvoice(false); }); - setDeleteInvoice(false); - }); }, [deleteInvoice, requestDeleteInvoice, formatMessage]); // Handle cancel/confirm invoice deliver. From 792f39a8f9974786b3d7413ea4b720bfda728ee6 Mon Sep 17 00:00:00 2001 From: elforjani3 Date: Sat, 2 Jan 2021 14:52:49 +0200 Subject: [PATCH 2/4] 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) => { From a1c228a8a4f007ab29f6dbd5b9d6488ecc822979 Mon Sep 17 00:00:00 2001 From: elforjani3 Date: Sat, 2 Jan 2021 15:07:51 +0200 Subject: [PATCH 3/4] fix: transform message error with customers. --- .../src/containers/Customers/CustomersList.js | 19 +++++++++++++++++-- client/src/lang/en/index.js | 5 +++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/client/src/containers/Customers/CustomersList.js b/client/src/containers/Customers/CustomersList.js index 9c090ff86..34da5edde 100644 --- a/client/src/containers/Customers/CustomersList.js +++ b/client/src/containers/Customers/CustomersList.js @@ -59,7 +59,7 @@ function CustomersList({ ['resource-views', 'customers'], (key, resourceName) => requestFetchResourceViews(resourceName), ); - + const fetchCustomers = useQuery( ['customers-table', customersTableQuery], (key, query) => requestFetchCustomers({ ...query }), @@ -146,6 +146,20 @@ function CustomersList({ setBulkDelete(false); }, []); + const transformApiErrors = (errors) => { + if ( + errors.find( + (error) => error.type === 'SOME.CUSTOMERS.HAVE.SALES_INVOICES', + ) + ) { + AppToaster.show({ + message: formatMessage({ + id: 'some_customers_have_sales_invoices', + }), + intent: Intent.DANGER, + }); + } + }; // Handle confirm customers bulk delete. const handleConfirmBulkDelete = useCallback(() => { requestDeleteBulkCustomers(bulkDelete) @@ -158,7 +172,8 @@ function CustomersList({ intent: Intent.SUCCESS, }); }) - .catch((error) => { + .catch((errors) => { + transformApiErrors(errors); setBulkDelete(false); }); }, [requestDeleteBulkCustomers, bulkDelete, formatMessage]); diff --git a/client/src/lang/en/index.js b/client/src/lang/en/index.js index 67c23222b..9e241c1dc 100644 --- a/client/src/lang/en/index.js +++ b/client/src/lang/en/index.js @@ -938,5 +938,6 @@ export default { 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' - }; + category_name_exists: 'Category name exists', + some_customers_have_sales_invoices: 'Some customers have sales invoices', +}; From 9014e1229f7135d0d6c8c57bc98b5fb1428e4cee Mon Sep 17 00:00:00 2001 From: elforjani3 Date: Sat, 2 Jan 2021 15:19:56 +0200 Subject: [PATCH 4/4] fix: item error message. --- client/src/containers/Items/ItemForm.js | 7 +++---- client/src/store/items/items.actions.js | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/client/src/containers/Items/ItemForm.js b/client/src/containers/Items/ItemForm.js index 2acc022f0..b30b6c3ee 100644 --- a/client/src/containers/Items/ItemForm.js +++ b/client/src/containers/Items/ItemForm.js @@ -163,11 +163,10 @@ function ItemForm({ history.push('/items'); } }; - const onError = ({ response }) => { + const onError = (errors) => { setSubmitting(false); - - if (response.data.errors) { - const _errors = transformApiErrors(response.data.errors); + if (errors) { + const _errors = transformApiErrors(errors); setErrors({ ..._errors }); } }; diff --git a/client/src/store/items/items.actions.js b/client/src/store/items/items.actions.js index 1ffb414ef..f29f2f673 100644 --- a/client/src/store/items/items.actions.js +++ b/client/src/store/items/items.actions.js @@ -2,9 +2,20 @@ import ApiService from 'services/ApiService'; import t from 'store/types'; export const submitItem = ({ form }) => { - return (dispatch) => ApiService.post(`items`, form); -}; + return (dispatch) => + new Promise((resolve, reject) => { + ApiService.post('items', form) + .then((response) => { + resolve(response); + }) + .catch((error) => { + const { response } = error; + const { data } = response; + reject(data?.errors); + }); + }); +}; export const editItem = ({ id, form }) => { return (dispatch) => ApiService.post(`items/${id}`, form); };