From 6c00c3f91cf4f7fd359918ed79ad191e813596a9 Mon Sep 17 00:00:00 2001 From: Yaroslav Kuznietsov Date: Wed, 25 Sep 2024 00:28:00 +0300 Subject: [PATCH] [useAsyncTree]: deps change handling fix. # Conflicts: # uui-core/src/data/processing/views/tree/hooks/strategies/asyncTree/useAsyncTree.ts --- .../data/processing/views/tree/ItemsStorage.ts | 4 ++-- .../hooks/strategies/asyncTree/useAsyncTree.ts | 15 ++++++++++++--- .../hooks/strategies/asyncTree/useLoadData.ts | 11 +++++++---- .../processing/views/tree/treeState/TreeState.ts | 2 +- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/uui-core/src/data/processing/views/tree/ItemsStorage.ts b/uui-core/src/data/processing/views/tree/ItemsStorage.ts index c5539ed49a..e9632b77f1 100644 --- a/uui-core/src/data/processing/views/tree/ItemsStorage.ts +++ b/uui-core/src/data/processing/views/tree/ItemsStorage.ts @@ -49,7 +49,7 @@ export class ItemsStorage { return this._itemsMap; }; - public getItemsMap() { + public getItemsMap = () => { return this._itemsMap; - } + }; } diff --git a/uui-core/src/data/processing/views/tree/hooks/strategies/asyncTree/useAsyncTree.ts b/uui-core/src/data/processing/views/tree/hooks/strategies/asyncTree/useAsyncTree.ts index e53b460232..5d0a5cf39c 100644 --- a/uui-core/src/data/processing/views/tree/hooks/strategies/asyncTree/useAsyncTree.ts +++ b/uui-core/src/data/processing/views/tree/hooks/strategies/asyncTree/useAsyncTree.ts @@ -6,6 +6,7 @@ import { usePrevious } from '../../../../../../../hooks/usePrevious'; import { useItemsStorage, useDataSourceStateWithDefaults, useFilterTree, useSortTree, useSearchTree, useSelectedOnlyTree, usePatchTree, + useActualItemsMap, } from '../../common'; import { UseTreeResult } from '../../types'; @@ -54,9 +55,12 @@ export function useAsyncTree( const [incommingTree, setIncommingTree] = useState(baseTree); + useEffect(() => { + setIncommingTree(TreeState.blank({ getId, getParentId, complexIds }, itemsMap, setItems)); + }, [...deps]); + const prevIsForceReload = usePrevious(isForceReload); const dataSourceState = useDataSourceStateWithDefaults({ dataSourceState: props.dataSourceState }); - const { tree: treeWithData, itemsStatusCollector, isLoaded: isTreeLoaded, isLoading, isFetching } = useLoadData({ getId, complexIds, @@ -118,8 +122,13 @@ export function useAsyncTree( isLoading: isTreeLoading, }, [searchTree, isTreeLoading]); - const { tree, applyPatch } = usePatchTree({ + const treeWithNewItemsMap = useActualItemsMap({ tree: treeWithSelectedOnly, + itemsMap, + }); + + const { tree, applyPatch } = usePatchTree({ + tree: treeWithNewItemsMap, patch: showSelectedOnly ? null : patch, isDeleted, getNewItemPosition, @@ -137,7 +146,7 @@ export function useAsyncTree( return { tree: showSelectedOnly ? tree.selectedOnly : tree.visible, - treeWithoutPatch: treeWithSelectedOnly.visible, + treeWithoutPatch: treeWithNewItemsMap.visible, selectionTree: tree.full, reload, totalCount, diff --git a/uui-core/src/data/processing/views/tree/hooks/strategies/asyncTree/useLoadData.ts b/uui-core/src/data/processing/views/tree/hooks/strategies/asyncTree/useLoadData.ts index 656f74331d..1a90d690ad 100644 --- a/uui-core/src/data/processing/views/tree/hooks/strategies/asyncTree/useLoadData.ts +++ b/uui-core/src/data/processing/views/tree/hooks/strategies/asyncTree/useLoadData.ts @@ -4,7 +4,6 @@ import { TreeState } from '../../../treeState'; import { usePrevious } from '../../../../../../../hooks/usePrevious'; import { isQueryChanged } from '../lazyTree/helpers'; import { useItemsStatusCollector } from '../../common'; -import { useDepsChanged } from '../../common/useDepsChanged'; import { getSelectedAndChecked } from '../../../treeStructure'; import { NOT_FOUND_RECORD } from '../../../constants'; import { ItemsStatuses } from '../types'; @@ -95,8 +94,6 @@ export function useLoadData( } }, [api]); - const depsChanged = useDepsChanged(deps); - const shouldForceReload = prevForceReload !== forceReload && forceReload; const shouldLoad = ( @@ -111,6 +108,12 @@ export function useLoadData( } }, [shouldForceReload]); + useEffect(() => { + if (isLoaded) { + setIsLoaded(false); + } + }, [...deps]); + useEffect(() => { if (shouldLoad) { setIsFetching(true); @@ -140,7 +143,7 @@ export function useLoadData( setIsLoading(false); }); } - }, [shouldLoad, depsChanged, shouldForceReload]); + }, [...deps, shouldLoad, shouldForceReload]); return { tree: loadedTree, isLoading, isFetching, isLoaded, itemsStatusCollector }; } diff --git a/uui-core/src/data/processing/views/tree/treeState/TreeState.ts b/uui-core/src/data/processing/views/tree/treeState/TreeState.ts index 54227736ed..6ac71300a4 100644 --- a/uui-core/src/data/processing/views/tree/treeState/TreeState.ts +++ b/uui-core/src/data/processing/views/tree/treeState/TreeState.ts @@ -390,7 +390,7 @@ export class TreeState { ) { const itemsAccessor = ItemsAccessor.toItemsAccessor(itemsMap); const treeStructure = TreeStructure.createFromItems({ params, items: itemsMap, itemsAccessor }); - return new TreeState( + return this.create( treeStructure, treeStructure, TreeStructure.create(params, ItemsAccessor.toItemsAccessor(itemsMap)),