From 27670d51ed4127a6fc7caf2e60d3bdb1a550ef5e Mon Sep 17 00:00:00 2001 From: elforjani3 Date: Sun, 21 Mar 2021 18:20:48 +0200 Subject: [PATCH] fix(vendor): catch error message. --- .../Alerts/Vendors/VendorDeleteAlert.js | 19 +++++++++++-------- client/src/containers/Vendors/utils.js | 15 +++++++++++++++ client/src/lang/en/index.js | 1 + 3 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 client/src/containers/Vendors/utils.js diff --git a/client/src/containers/Alerts/Vendors/VendorDeleteAlert.js b/client/src/containers/Alerts/Vendors/VendorDeleteAlert.js index dfca9a55c..cf73a3616 100644 --- a/client/src/containers/Alerts/Vendors/VendorDeleteAlert.js +++ b/client/src/containers/Alerts/Vendors/VendorDeleteAlert.js @@ -6,7 +6,7 @@ import { } from 'react-intl'; import { Intent, Alert } from '@blueprintjs/core'; import { AppToaster } from 'components'; -import { transformErrors } from 'containers/Customers/utils'; +import { transformErrors } from 'containers/Vendors/utils'; import { useDeleteVendor } from 'hooks/query'; import withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect'; @@ -28,10 +28,7 @@ function VendorDeleteAlert({ closeAlert, }) { const { formatMessage } = useIntl(); - const { - mutateAsync: deleteVendorMutate, - isLoading - } = useDeleteVendor(); + const { mutateAsync: deleteVendorMutate, isLoading } = useDeleteVendor(); // Handle cancel delete the vendor. const handleCancelDeleteAlert = () => { @@ -49,9 +46,15 @@ function VendorDeleteAlert({ intent: Intent.SUCCESS, }); }) - .catch((errors) => { - transformErrors(errors); - }) + .catch( + ({ + response: { + data: { errors }, + }, + }) => { + transformErrors(errors); + }, + ) .finally(() => { closeAlert(name); }); diff --git a/client/src/containers/Vendors/utils.js b/client/src/containers/Vendors/utils.js new file mode 100644 index 000000000..4d4d322e8 --- /dev/null +++ b/client/src/containers/Vendors/utils.js @@ -0,0 +1,15 @@ +import React from 'react'; +import { Intent } from '@blueprintjs/core'; +import { AppToaster } from 'components'; +import { formatMessage } from 'services/intl'; + +export const transformErrors = (errors) => { + if (errors.find((error) => error.type === 'VENDOR.HAS.ASSOCIATED.BILLS')) { + AppToaster.show({ + message: formatMessage({ + id: 'cannot_delete_vendor_that_has_associated_purchase_bills', + }), + intent: Intent.DANGER, + }); + } +}; diff --git a/client/src/lang/en/index.js b/client/src/lang/en/index.js index 4b15e129a..e83cbe24b 100644 --- a/client/src/lang/en/index.js +++ b/client/src/lang/en/index.js @@ -1020,4 +1020,5 @@ export default { cannot_change_item_type_to_inventory_with_item_has_associated_transactions: 'Cannot change item type to inventory with item has associated transactions.', work_phone: 'Work Phone', + cannot_delete_vendor_that_has_associated_purchase_bills:'Cannot delete vendor that has associated purchase bills.' };