From 8bbfe20ec494b7e265fb8b41c56dd3785cc00f10 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sun, 31 Aug 2025 21:31:54 +0100 Subject: [PATCH] Fixed object update issues. --- .../Dashboard/common/ObjectTable.jsx | 80 +++++++++++-------- 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/src/components/Dashboard/common/ObjectTable.jsx b/src/components/Dashboard/common/ObjectTable.jsx index fca65c4..972d186 100644 --- a/src/components/Dashboard/common/ObjectTable.jsx +++ b/src/components/Dashboard/common/ObjectTable.jsx @@ -39,7 +39,7 @@ import CheckIcon from '../../Icons/CheckIcon' import { useNavigate } from 'react-router-dom' import QuestionCircleIcon from '../../Icons/QuestionCircleIcon' import { AuthContext } from '../context/AuthContext' -import merge from 'lodash/merge' +import unionBy from 'lodash/unionBy' const logger = loglevel.getLogger('DasboardTable') logger.setLevel(config.logLevel) @@ -88,9 +88,10 @@ const ObjectTable = forwardRef( const [lazyLoading, setLazyLoading] = useState(false) const subscribedIdsRef = useRef([]) - const [typeSubscribed, setTypeSubscribed] = useState(false) + // const [typeSubscribed, setTypeSubscribed] = useState(false) const unsubscribesRef = useRef([]) const updateEventHandlerRef = useRef() + const subscribeToObjectTypeUpdatesRef = useRef(null) const rowActions = model.actions?.filter((action) => action.row == true) || [] @@ -291,12 +292,17 @@ const ObjectTable = forwardRef( const updateEventHandler = useCallback((id, updatedData) => { console.log('GOT UPDATE FOR', id) setPages((prevPages) => - prevPages.map((page) => ({ - ...page, - items: page.items.map((item) => { - return item._id === id ? merge({}, item, updatedData) : item - }) - })) + prevPages.map((page) => { + const updatedItems = unionBy( + [{ ...updatedData, _id: id }], + page.items, + '_id' + ) + return { + ...page, + items: updatedItems + } + }) ) }, []) @@ -363,33 +369,43 @@ const ObjectTable = forwardRef( // Cleanup effect for component unmount useEffect(() => { return () => { - // Clean up all subscriptions when component unmounts - unsubscribesRef.current.forEach((unsubscribe) => { - console.log('CALLING UNSUB on unmount') - if (unsubscribe) unsubscribe() - }) - unsubscribesRef.current = [] - subscribedIdsRef.current = [] - } - }, []) + console.log('API: Call unsub', connected) + if (connected == true && unsubscribesRef.current) { + console.log('API: Got object unsub') + // Clean up all subscriptions when component unmounts + unsubscribesRef.current.forEach((unsubscribe) => { + console.log('CALLING UNSUB on unmount') + if (unsubscribe) unsubscribe() + }) + unsubscribesRef.current = [] + subscribedIdsRef.current = [] - useEffect(() => { - if (connected == true && typeSubscribed == false) { - const unsubscribe = subscribeToObjectTypeUpdates(type, newEventHandler) - setTypeSubscribed(true) - return () => { - if (unsubscribe && typeSubscribed == true) { - unsubscribe() - } + // Clean up type subscription + } + if (connected == true && subscribeToObjectTypeUpdatesRef.current) { + console.log('UNSUBBING type subscription on unmount') + console.log('API: Got type unsub') + subscribeToObjectTypeUpdatesRef.current() + subscribeToObjectTypeUpdatesRef.current = null } } - }, [ - type, - subscribeToObjectTypeUpdates, - connected, - newEventHandler, - typeSubscribed - ]) + }, [connected]) + + useEffect(() => { + if ( + connected == true && + subscribeToObjectTypeUpdatesRef.current == null + ) { + console.log( + 'API: Subbing to updates', + subscribeToObjectTypeUpdatesRef.current + ) + subscribeToObjectTypeUpdatesRef.current = subscribeToObjectTypeUpdates( + type, + newEventHandler + ) + } + }, [type, subscribeToObjectTypeUpdates, connected, newEventHandler]) const updateData = useCallback( (_id, updatedData) => {