diff --git a/src/Contexts/CrmContext.js b/src/Contexts/CrmContext.js index 64d97c0..0c9ecb6 100644 --- a/src/Contexts/CrmContext.js +++ b/src/Contexts/CrmContext.js @@ -8,6 +8,7 @@ const preview = { openDrawerCustomerId: null, currentDrawerCustomer: null, currentDrawerCustomerRef: null, + currentDrawerCustomerNotesRef: null, changedDrawerCustomerFieldsRef: null, }; @@ -27,6 +28,8 @@ export function CrmProvider({ children }) { const [currentDrawerCustomer, setCurrentDrawerCustomer] = useState(null); // will be used to store the customer updated object that is currently being viewed in the drawer const currentDrawerCustomerRef = useRef(null); + // this will be used to store the updates noted and will be compared to currentDrawerCustomerRef to see if there are any changes + const currentDrawerCustomerNotesRef = useRef(null); const changedDrawerCustomerFieldsRef = useRef([]); return ( @@ -42,6 +45,7 @@ export function CrmProvider({ children }) { openDrawerCustomerId, currentDrawerCustomer, setCurrentDrawerCustomer, + currentDrawerCustomerNotesRef, currentDrawerCustomerRef, changedDrawerCustomerFieldsRef, }} diff --git a/src/Handlers/WebSocketMessageHandler.js b/src/Handlers/WebSocketMessageHandler.js index 684b613..1e96823 100644 --- a/src/Handlers/WebSocketMessageHandler.js +++ b/src/Handlers/WebSocketMessageHandler.js @@ -1061,8 +1061,6 @@ export function handleWebSocketMessage( crmContext.setCustomers((arr) => { const newArr = [...arr]; - console.log("before", arr); - const arrIndex = arr.findIndex((customer) => customer.Id === body.Id); if (arrIndex !== -1) { @@ -1070,14 +1068,10 @@ export function handleWebSocketMessage( for (const property in body) { if (body[property] !== undefined && property !== "Id") { newArr[arrIndex][property] = body[property]; - - console.log("updated", property, body, body[property]); } } } - console.log("after", newArr); - return newArr; }); diff --git a/src/Pages/CrmTest/CrmTest.js b/src/Pages/CrmTest/CrmTest.js index 6193cd1..049b620 100644 --- a/src/Pages/CrmTest/CrmTest.js +++ b/src/Pages/CrmTest/CrmTest.js @@ -356,10 +356,10 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) { children: ( ), @@ -424,6 +424,11 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) { }; useEffect(() => { + // set the active tab to the first one if the drawer is closed + if (!isOpen) { + setActiveTab(tabItems[0].key); + } + if (crmContext.openDrawerCustomerId.current === null) return; formDealInfo @@ -441,6 +446,7 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) { } let changedFields = []; + let newCustomer = {}; Object.keys(values).forEach((key) => { if (values[key] !== undefined) { @@ -448,17 +454,26 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) { } }); + // check notes + + if (crmContext.currentDrawerCustomerNotesRef.current !== null) { + changedFields.push("Notes"); + } + // check if something has changed (length 2 = only the pipeline and deal phase) if (changedFields.length === 2) return; - let newCustomer = {}; - Object.keys(values).forEach((key) => { if (values[key] !== undefined) { newCustomer[key] = values[key]; } }); + if (changedFields.includes("Notes")) { + newCustomer.Notes = + crmContext.currentDrawerCustomerNotesRef.current; + } + myFetch(`/crm/customer/create`, "POST", newCustomer) .then(() => { crmContext.openDrawerCustomerId.current = false; @@ -472,6 +487,7 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) { customerRequest(); } else { let changedFields = []; + const updatedCustomer = {}; Object.keys(values).forEach((key) => { if ( @@ -481,15 +497,27 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) { } }); + // check notes + + if ( + crmContext.currentDrawerCustomerNotesRef.current !== null && + crmContext.currentDrawerCustomerNotesRef.current !== + crmContext.currentDrawerCustomerRef.current.Notes + ) { + changedFields.push("Notes"); + } + // check if something has changed if (changedFields.length > 0) { - // only updated changed fields - const updatedCustomer = {}; - changedFields.forEach((key) => { updatedCustomer[key] = values[key]; }); + if (changedFields.includes("Notes")) { + updatedCustomer.Notes = + crmContext.currentDrawerCustomerNotesRef.current; + } + myFetch( `/crm/customer/update/${crmContext.openDrawerCustomerId.current}`, "POST", @@ -500,7 +528,7 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) { } } }) - .catch(() => {}); + .catch((err) => console.log("error", err)); const handleCustomerRequest = () => customerRequest(); @@ -519,21 +547,13 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) { const handleCloseDrawer = () => { crmContext.openDrawerCustomerId.current = null; crmContext.currentDrawerCustomerRef.current = null; - crmContext.changedDrawerCustomerFieldsRef.current = []; + crmContext.currentDrawerCustomerNotesRef.current = null; formDealInfo.resetFields(); - - setActiveTab(tabItems[0].key); }; - const title = - crmContext.currentDrawerCustomer === null - ? "loading..." - : `${crmContext.currentDrawerCustomer?.FirstName} ${crmContext.currentDrawerCustomer?.LastName}`; - return ( { - crmContext.setCurrentDrawerCustomer({ - ...crmContext.currentDrawerCustomer, - Notes: value, - }); + crmContext.currentDrawerCustomerNotesRef.current = value; }} plugins={[ listsPlugin(),