From 792c96c344f57cec0471dee319445b869feaec12 Mon Sep 17 00:00:00 2001 From: elforjani3 Date: Sat, 2 Jan 2021 13:43:40 +0200 Subject: [PATCH] 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.