Properly reset loading between collections

This commit is contained in:
Daniel Lautzenheiser
2022-10-11 07:48:39 -04:00
parent 5280909a0c
commit d1b6a2631d

View File

@ -85,6 +85,10 @@ const CollectionView = ({
viewStyle, viewStyle,
}: ReturnType<typeof mergeProps>) => { }: ReturnType<typeof mergeProps>) => {
const [readyToLoad, setReadyToLoad] = useState(false); const [readyToLoad, setReadyToLoad] = useState(false);
const [preCollection, setPreCollection] = useState(collection);
useEffect(() => {
setPreCollection(collection);
}, [collection]);
const newEntryUrl = useMemo(() => { const newEntryUrl = useMemo(() => {
let url = collection.get('create') ? getNewEntryUrl(collectionName) : ''; let url = collection.get('create') ? getNewEntryUrl(collectionName) : '';
@ -108,7 +112,7 @@ const CollectionView = ({
collection={collection} collection={collection}
viewStyle={viewStyle} viewStyle={viewStyle}
filterTerm={filterTerm} filterTerm={filterTerm}
readyToLoad={readyToLoad} readyToLoad={readyToLoad && collection === preCollection}
/> />
); );
}, [collection, filterTerm, viewStyle, readyToLoad]); }, [collection, filterTerm, viewStyle, readyToLoad]);
@ -123,6 +127,11 @@ const CollectionView = ({
}, [searchTerm, collections, collection, isSingleSearchResult]); }, [searchTerm, collections, collection, isSingleSearchResult]);
useEffect(() => { useEffect(() => {
setReadyToLoad(false);
let alive = true;
const sortEntries = () => {
setTimeout(async () => {
if (sort?.first()?.get('key')) { if (sort?.first()?.get('key')) {
setReadyToLoad(true); setReadyToLoad(true);
return; return;
@ -137,15 +146,15 @@ const CollectionView = ({
return; return;
} }
let alive = true;
const sortEntries = async () => {
await onSortClick( await onSortClick(
defaultSort.get('field'), defaultSort.get('field'),
defaultSort.get('direction') ?? SortDirection.Ascending, defaultSort.get('direction') ?? SortDirection.Ascending,
); );
if (alive) { if (alive) {
setReadyToLoad(true); setReadyToLoad(true);
} }
});
}; };
sortEntries(); sortEntries();