main
alex 2024-03-06 21:27:08 +01:00
parent 02ee6c6a72
commit 09a959153d
3 changed files with 44 additions and 29 deletions

View File

@ -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,
}}

View File

@ -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;
});

View File

@ -356,10 +356,10 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) {
children: (
<TabContentNotes
notes={
crmContext.currentDrawerCustomer === null ||
crmContext.currentDrawerCustomer.Notes === null
crmContext.currentDrawerCustomerRef.current === null ||
crmContext.currentDrawerCustomerRef.current.Notes === null
? ""
: crmContext.currentDrawerCustomer.Notes
: crmContext.currentDrawerCustomerRef.current.Notes
}
/>
),
@ -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 (
<Drawer
title={title}
placement="right"
open={isOpen}
onClose={onClose}
@ -828,14 +848,11 @@ function TabContentNotes({ notes }) {
return (
<MySupsenseFallback>
<MDXEditor
key={crmContext.currentDrawerCustomer?.Id}
key={crmContext.currentDrawerCustomerRef.current?.Id}
className="mdx-editor"
markdown={notes}
onChange={(value) => {
crmContext.setCurrentDrawerCustomer({
...crmContext.currentDrawerCustomer,
Notes: value,
});
crmContext.currentDrawerCustomerNotesRef.current = value;
}}
plugins={[
listsPlugin(),