From d1b6a2631d81c5701ac66fd12e54e3d476c4c857 Mon Sep 17 00:00:00 2001 From: Daniel Lautzenheiser Date: Tue, 11 Oct 2022 07:48:39 -0400 Subject: [PATCH] Properly reset loading between collections --- src/components/Collection/Collection.tsx | 55 ++++++++++++++---------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/components/Collection/Collection.tsx b/src/components/Collection/Collection.tsx index ebb3da1d..e2bad7b8 100644 --- a/src/components/Collection/Collection.tsx +++ b/src/components/Collection/Collection.tsx @@ -85,6 +85,10 @@ const CollectionView = ({ viewStyle, }: ReturnType) => { const [readyToLoad, setReadyToLoad] = useState(false); + const [preCollection, setPreCollection] = useState(collection); + useEffect(() => { + setPreCollection(collection); + }, [collection]); const newEntryUrl = useMemo(() => { let url = collection.get('create') ? getNewEntryUrl(collectionName) : ''; @@ -108,7 +112,7 @@ const CollectionView = ({ collection={collection} viewStyle={viewStyle} filterTerm={filterTerm} - readyToLoad={readyToLoad} + readyToLoad={readyToLoad && collection === preCollection} /> ); }, [collection, filterTerm, viewStyle, readyToLoad]); @@ -123,29 +127,34 @@ const CollectionView = ({ }, [searchTerm, collections, collection, isSingleSearchResult]); useEffect(() => { - if (sort?.first()?.get('key')) { - setReadyToLoad(true); - return; - } - - const defaultSort = collection.getIn(['sortable_fields', 'default']) as - | StaticallyTypedRecord - | undefined; - - if (!defaultSort || !defaultSort.get('field')) { - setReadyToLoad(true); - return; - } - + setReadyToLoad(false); let alive = true; - const sortEntries = async () => { - await onSortClick( - defaultSort.get('field'), - defaultSort.get('direction') ?? SortDirection.Ascending, - ); - if (alive) { - setReadyToLoad(true); - } + + const sortEntries = () => { + setTimeout(async () => { + if (sort?.first()?.get('key')) { + setReadyToLoad(true); + return; + } + + const defaultSort = collection.getIn(['sortable_fields', 'default']) as + | StaticallyTypedRecord + | undefined; + + if (!defaultSort || !defaultSort.get('field')) { + setReadyToLoad(true); + return; + } + + await onSortClick( + defaultSort.get('field'), + defaultSort.get('direction') ?? SortDirection.Ascending, + ); + + if (alive) { + setReadyToLoad(true); + } + }); }; sortEntries();