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, openDrawerCustomerId: null,
currentDrawerCustomer: null, currentDrawerCustomer: null,
currentDrawerCustomerRef: null, currentDrawerCustomerRef: null,
currentDrawerCustomerNotesRef: null,
changedDrawerCustomerFieldsRef: null, changedDrawerCustomerFieldsRef: null,
}; };
@ -27,6 +28,8 @@ export function CrmProvider({ children }) {
const [currentDrawerCustomer, setCurrentDrawerCustomer] = useState(null); const [currentDrawerCustomer, setCurrentDrawerCustomer] = useState(null);
// will be used to store the customer updated object that is currently being viewed in the drawer // will be used to store the customer updated object that is currently being viewed in the drawer
const currentDrawerCustomerRef = useRef(null); 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([]); const changedDrawerCustomerFieldsRef = useRef([]);
return ( return (
@ -42,6 +45,7 @@ export function CrmProvider({ children }) {
openDrawerCustomerId, openDrawerCustomerId,
currentDrawerCustomer, currentDrawerCustomer,
setCurrentDrawerCustomer, setCurrentDrawerCustomer,
currentDrawerCustomerNotesRef,
currentDrawerCustomerRef, currentDrawerCustomerRef,
changedDrawerCustomerFieldsRef, changedDrawerCustomerFieldsRef,
}} }}

View File

@ -1061,8 +1061,6 @@ export function handleWebSocketMessage(
crmContext.setCustomers((arr) => { crmContext.setCustomers((arr) => {
const newArr = [...arr]; const newArr = [...arr];
console.log("before", arr);
const arrIndex = arr.findIndex((customer) => customer.Id === body.Id); const arrIndex = arr.findIndex((customer) => customer.Id === body.Id);
if (arrIndex !== -1) { if (arrIndex !== -1) {
@ -1070,14 +1068,10 @@ export function handleWebSocketMessage(
for (const property in body) { for (const property in body) {
if (body[property] !== undefined && property !== "Id") { if (body[property] !== undefined && property !== "Id") {
newArr[arrIndex][property] = body[property]; newArr[arrIndex][property] = body[property];
console.log("updated", property, body, body[property]);
} }
} }
} }
console.log("after", newArr);
return newArr; return newArr;
}); });

View File

@ -356,10 +356,10 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) {
children: ( children: (
<TabContentNotes <TabContentNotes
notes={ notes={
crmContext.currentDrawerCustomer === null || crmContext.currentDrawerCustomerRef.current === null ||
crmContext.currentDrawerCustomer.Notes === null crmContext.currentDrawerCustomerRef.current.Notes === null
? "" ? ""
: crmContext.currentDrawerCustomer.Notes : crmContext.currentDrawerCustomerRef.current.Notes
} }
/> />
), ),
@ -424,6 +424,11 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) {
}; };
useEffect(() => { 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; if (crmContext.openDrawerCustomerId.current === null) return;
formDealInfo formDealInfo
@ -441,6 +446,7 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) {
} }
let changedFields = []; let changedFields = [];
let newCustomer = {};
Object.keys(values).forEach((key) => { Object.keys(values).forEach((key) => {
if (values[key] !== undefined) { 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) // check if something has changed (length 2 = only the pipeline and deal phase)
if (changedFields.length === 2) return; if (changedFields.length === 2) return;
let newCustomer = {};
Object.keys(values).forEach((key) => { Object.keys(values).forEach((key) => {
if (values[key] !== undefined) { if (values[key] !== undefined) {
newCustomer[key] = values[key]; newCustomer[key] = values[key];
} }
}); });
if (changedFields.includes("Notes")) {
newCustomer.Notes =
crmContext.currentDrawerCustomerNotesRef.current;
}
myFetch(`/crm/customer/create`, "POST", newCustomer) myFetch(`/crm/customer/create`, "POST", newCustomer)
.then(() => { .then(() => {
crmContext.openDrawerCustomerId.current = false; crmContext.openDrawerCustomerId.current = false;
@ -472,6 +487,7 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) {
customerRequest(); customerRequest();
} else { } else {
let changedFields = []; let changedFields = [];
const updatedCustomer = {};
Object.keys(values).forEach((key) => { Object.keys(values).forEach((key) => {
if ( 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 // check if something has changed
if (changedFields.length > 0) { if (changedFields.length > 0) {
// only updated changed fields
const updatedCustomer = {};
changedFields.forEach((key) => { changedFields.forEach((key) => {
updatedCustomer[key] = values[key]; updatedCustomer[key] = values[key];
}); });
if (changedFields.includes("Notes")) {
updatedCustomer.Notes =
crmContext.currentDrawerCustomerNotesRef.current;
}
myFetch( myFetch(
`/crm/customer/update/${crmContext.openDrawerCustomerId.current}`, `/crm/customer/update/${crmContext.openDrawerCustomerId.current}`,
"POST", "POST",
@ -500,7 +528,7 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) {
} }
} }
}) })
.catch(() => {}); .catch((err) => console.log("error", err));
const handleCustomerRequest = () => customerRequest(); const handleCustomerRequest = () => customerRequest();
@ -519,21 +547,13 @@ function CustomerDrawer({ isOpen, setIsOpen, onClose, notificationApi }) {
const handleCloseDrawer = () => { const handleCloseDrawer = () => {
crmContext.openDrawerCustomerId.current = null; crmContext.openDrawerCustomerId.current = null;
crmContext.currentDrawerCustomerRef.current = null; crmContext.currentDrawerCustomerRef.current = null;
crmContext.changedDrawerCustomerFieldsRef.current = []; crmContext.currentDrawerCustomerNotesRef.current = null;
formDealInfo.resetFields(); formDealInfo.resetFields();
setActiveTab(tabItems[0].key);
}; };
const title =
crmContext.currentDrawerCustomer === null
? "loading..."
: `${crmContext.currentDrawerCustomer?.FirstName} ${crmContext.currentDrawerCustomer?.LastName}`;
return ( return (
<Drawer <Drawer
title={title}
placement="right" placement="right"
open={isOpen} open={isOpen}
onClose={onClose} onClose={onClose}
@ -828,14 +848,11 @@ function TabContentNotes({ notes }) {
return ( return (
<MySupsenseFallback> <MySupsenseFallback>
<MDXEditor <MDXEditor
key={crmContext.currentDrawerCustomer?.Id} key={crmContext.currentDrawerCustomerRef.current?.Id}
className="mdx-editor" className="mdx-editor"
markdown={notes} markdown={notes}
onChange={(value) => { onChange={(value) => {
crmContext.setCurrentDrawerCustomer({ crmContext.currentDrawerCustomerNotesRef.current = value;
...crmContext.currentDrawerCustomer,
Notes: value,
});
}} }}
plugins={[ plugins={[
listsPlugin(), listsPlugin(),