From 19adacfcf69d9f9470a500120b7022a92c227918 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 5 Mar 2024 21:35:45 +0100 Subject: [PATCH] handle duplicate company name --- public/locales/de/translation.json | 6 +++ public/locales/en/translation.json | 12 +++++ src/Pages/Crm/index.js | 86 ++++++++++++++++++++++++------ src/utils.js | 7 +++ 4 files changed, 94 insertions(+), 17 deletions(-) diff --git a/public/locales/de/translation.json b/public/locales/de/translation.json index 8fc9f0a..c1181b3 100644 --- a/public/locales/de/translation.json +++ b/public/locales/de/translation.json @@ -31,6 +31,12 @@ "show": "Anzeigen", "hide": "Verbergen", "reload": "Neu laden" + }, + "request": { + "unknownError": { + "title": "Ein unbekannter Fehler ist aufgetreten", + "description": "Bitte versuchen Sie es erneut." + } } }, "sideMenu": { diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 6a7c598..26a9cef 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -31,6 +31,12 @@ "show": "Show", "hide": "Hide", "reload": "Reload" + }, + "request": { + "unknownError": { + "title": "An unknown error has occurred", + "description": "The request failed. Please try again." + } } }, "sideMenu": { @@ -284,6 +290,12 @@ "notes": "Notes" }, "tabContent": { + "request": { + "duplicateCompanyError": { + "message": "Company already exists", + "description": "Choose another company name" + } + }, "dealInfo": { "collapseDealStatus": { "label": "Deal Status", diff --git a/src/Pages/Crm/index.js b/src/Pages/Crm/index.js index 7d0cb87..288336e 100644 --- a/src/Pages/Crm/index.js +++ b/src/Pages/Crm/index.js @@ -12,6 +12,7 @@ import { Table, Tabs, Typography, + notification, } from "antd"; import { useTranslation } from "react-i18next"; import { useNavigate, useParams } from "react-router-dom"; @@ -50,6 +51,8 @@ export default function Crm() { const crmContext = useCrmContext(); const { paramType, paramDealPhase } = useParams(); const [isDrawerOpen, setIsDrawerOpen] = useState(false); + const [notificationApi, notificationContextHolder] = + notification.useNotification(); const title = paramType === Constants.CRM_TYPE.CUSTOMERS @@ -146,7 +149,7 @@ export default function Crm() { `/crm/pipeline/${paramType}/${paramDealPhase}?page=${crmContext.paginationPage}`, "GET" ).then((data) => { - if (data.Customers !== undefined) { + if (data.Customers !== undefined && data.Customers !== null) { crmContext.setCustomers(data.Customers); } @@ -177,6 +180,8 @@ export default function Crm() { return ( <> + {notificationContextHolder} +
setIsDrawerOpen(false)} + notificationApi={notificationApi} /> ); } -function CustomerDrawer({ isOpen, onClose }) { +function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) { const { t } = useTranslation(); const crmContext = useCrmContext(); @@ -282,6 +289,18 @@ function CustomerDrawer({ isOpen, onClose }) { const [activeTab, setActiveTab] = useState(tabItems[0].key); + const customerRequest = () => { + if (!isOpen) return; + + myFetch( + `/crm/customer/view/${crmContext.openDrawerCustomerId.current}`, + "GET" + ).then((data) => { + crmContext.setCurrentDrawerCustomer(data); + crmContext.currentDrawerCustomerRef.current = data; + }); + }; + useEffect(() => { if (crmContext.openDrawerCustomerId.current === null) return; @@ -291,22 +310,33 @@ function CustomerDrawer({ isOpen, onClose }) { // check if something has changed if (crmContext.currentDrawerCustomer === null) return; - myFetch(`/crm/customer/create`, "POST", crmContext.currentDrawerCustomer); + myFetch(`/crm/customer/create`, "POST", crmContext.currentDrawerCustomer) + .then(() => { + crmContext.openDrawerCustomerId.current = false; + handleCloseDrawer(); + }) + .catch((status) => { + if (status === 409) { + notificationApi["error"]({ + message: t( + "crm.tabContent.request.duplicateCompanyError.message" + ), + description: t( + "crm.tabContent.request.duplicateCompanyError.description" + ), + }); + } else { + notificationApi["error"]({ + message: "test", + description: "here", + }); + } + + setIsOpen(true); + }); return; } - const customerRequest = () => { - if (!isOpen) return; - - myFetch( - `/crm/customer/view/${crmContext.openDrawerCustomerId.current}`, - "GET" - ).then((data) => { - crmContext.setCurrentDrawerCustomer(data); - crmContext.currentDrawerCustomerRef.current = data; - }); - }; - if (isOpen) { customerRequest(); } else { @@ -331,10 +361,32 @@ function CustomerDrawer({ isOpen, onClose }) { `/crm/customer/update/${crmContext.openDrawerCustomerId.current}`, "POST", updatedCustomer - ); + ) + .then(() => { + handleCloseDrawer(); + }) + .catch((status) => { + if (status === 409) { + notificationApi["error"]({ + message: t( + "crm.tabContent.request.duplicateCompanyError.message" + ), + description: t( + "crm.tabContent.request.duplicateCompanyError.description" + ), + }); + } else { + notificationApi["error"]({ + message: "test", + description: "here", + }); + } + + setIsOpen(true); + }); } - handleCloseDrawer(); + //handleCloseDrawer(); } const handleCustomerRequest = () => customerRequest(); diff --git a/src/utils.js b/src/utils.js index 6522174..5af88a7 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1469,3 +1469,10 @@ export function myFetch( throw error; }); } + +export function showUnkownErrorNotification(notificationApi, t) { + notificationApi["error"]({ + message: t("common.request.unknownError.title"), + description: t("common.request.unknownError.description"), + }); +}