handle duplicate company name

main
alex 2024-03-05 21:35:45 +01:00
parent a3a63b8d84
commit 19adacfcf6
4 changed files with 94 additions and 17 deletions

View File

@ -31,6 +31,12 @@
"show": "Anzeigen", "show": "Anzeigen",
"hide": "Verbergen", "hide": "Verbergen",
"reload": "Neu laden" "reload": "Neu laden"
},
"request": {
"unknownError": {
"title": "Ein unbekannter Fehler ist aufgetreten",
"description": "Bitte versuchen Sie es erneut."
}
} }
}, },
"sideMenu": { "sideMenu": {

View File

@ -31,6 +31,12 @@
"show": "Show", "show": "Show",
"hide": "Hide", "hide": "Hide",
"reload": "Reload" "reload": "Reload"
},
"request": {
"unknownError": {
"title": "An unknown error has occurred",
"description": "The request failed. Please try again."
}
} }
}, },
"sideMenu": { "sideMenu": {
@ -284,6 +290,12 @@
"notes": "Notes" "notes": "Notes"
}, },
"tabContent": { "tabContent": {
"request": {
"duplicateCompanyError": {
"message": "Company already exists",
"description": "Choose another company name"
}
},
"dealInfo": { "dealInfo": {
"collapseDealStatus": { "collapseDealStatus": {
"label": "Deal Status", "label": "Deal Status",

View File

@ -12,6 +12,7 @@ import {
Table, Table,
Tabs, Tabs,
Typography, Typography,
notification,
} from "antd"; } from "antd";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
@ -50,6 +51,8 @@ export default function Crm() {
const crmContext = useCrmContext(); const crmContext = useCrmContext();
const { paramType, paramDealPhase } = useParams(); const { paramType, paramDealPhase } = useParams();
const [isDrawerOpen, setIsDrawerOpen] = useState(false); const [isDrawerOpen, setIsDrawerOpen] = useState(false);
const [notificationApi, notificationContextHolder] =
notification.useNotification();
const title = const title =
paramType === Constants.CRM_TYPE.CUSTOMERS paramType === Constants.CRM_TYPE.CUSTOMERS
@ -146,7 +149,7 @@ export default function Crm() {
`/crm/pipeline/${paramType}/${paramDealPhase}?page=${crmContext.paginationPage}`, `/crm/pipeline/${paramType}/${paramDealPhase}?page=${crmContext.paginationPage}`,
"GET" "GET"
).then((data) => { ).then((data) => {
if (data.Customers !== undefined) { if (data.Customers !== undefined && data.Customers !== null) {
crmContext.setCustomers(data.Customers); crmContext.setCustomers(data.Customers);
} }
@ -177,6 +180,8 @@ export default function Crm() {
return ( return (
<> <>
{notificationContextHolder}
<div <div
style={{ style={{
display: "flex", display: "flex",
@ -243,13 +248,15 @@ export default function Crm() {
<CustomerDrawer <CustomerDrawer
isOpen={isDrawerOpen} isOpen={isDrawerOpen}
setIsOpen={setIsDrawerOpen}
onClose={() => setIsDrawerOpen(false)} onClose={() => setIsDrawerOpen(false)}
notificationApi={notificationApi}
/> />
</> </>
); );
} }
function CustomerDrawer({ isOpen, onClose }) { function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) {
const { t } = useTranslation(); const { t } = useTranslation();
const crmContext = useCrmContext(); const crmContext = useCrmContext();
@ -282,6 +289,18 @@ function CustomerDrawer({ isOpen, onClose }) {
const [activeTab, setActiveTab] = useState(tabItems[0].key); 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(() => { useEffect(() => {
if (crmContext.openDrawerCustomerId.current === null) return; if (crmContext.openDrawerCustomerId.current === null) return;
@ -291,22 +310,33 @@ function CustomerDrawer({ isOpen, onClose }) {
// check if something has changed // check if something has changed
if (crmContext.currentDrawerCustomer === null) return; 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; 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) { if (isOpen) {
customerRequest(); customerRequest();
} else { } else {
@ -331,10 +361,32 @@ function CustomerDrawer({ isOpen, onClose }) {
`/crm/customer/update/${crmContext.openDrawerCustomerId.current}`, `/crm/customer/update/${crmContext.openDrawerCustomerId.current}`,
"POST", "POST",
updatedCustomer 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(); const handleCustomerRequest = () => customerRequest();

View File

@ -1469,3 +1469,10 @@ export function myFetch(
throw error; throw error;
}); });
} }
export function showUnkownErrorNotification(notificationApi, t) {
notificationApi["error"]({
message: t("common.request.unknownError.title"),
description: t("common.request.unknownError.description"),
});
}