Do not load entries before default sort is set

This commit is contained in:
Daniel Lautzenheiser 2022-09-29 13:43:59 -04:00
parent d4b55e1411
commit 2c14223932
3 changed files with 34 additions and 10 deletions

View File

@ -515,10 +515,12 @@ export function deleteLocalBackup(collection: Collection, slug: string) {
* Exported Thunk Action Creators * Exported Thunk Action Creators
*/ */
export function loadEntry(collection: Collection, slug: string) { export function loadEntry(collection: Collection, slug: string, silent = false) {
return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => { return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
await waitForMediaLibraryToLoad(dispatch, getState()); await waitForMediaLibraryToLoad(dispatch, getState());
dispatch(entryLoading(collection, slug)); if (!silent) {
dispatch(entryLoading(collection, slug));
}
try { try {
const loadedEntry = await tryLoadEntry(getState(), collection, slug); const loadedEntry = await tryLoadEntry(getState(), collection, slug);
@ -931,6 +933,8 @@ export function persistEntry(collection: Collection) {
if (entry.get('slug') !== newSlug) { if (entry.get('slug') !== newSlug) {
await dispatch(loadEntry(collection, newSlug)); await dispatch(loadEntry(collection, newSlug));
navigateToEntry(collection.get('name'), newSlug); navigateToEntry(collection.get('name'), newSlug);
} else {
await dispatch(loadEntry(collection, newSlug, true));
} }
}) })
.catch((error: Error) => { .catch((error: Error) => {

View File

@ -1,5 +1,5 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import React, { useCallback, useEffect, useMemo } from 'react'; import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { translate } from 'react-polyglot'; import { translate } from 'react-polyglot';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
@ -84,6 +84,8 @@ const CollectionView = ({
onChangeViewStyle, onChangeViewStyle,
viewStyle, viewStyle,
}: ReturnType<typeof mergeProps>) => { }: ReturnType<typeof mergeProps>) => {
const [readyToLoad, setReadyToLoad] = useState(false);
const newEntryUrl = useMemo(() => { const newEntryUrl = useMemo(() => {
let url = collection.get('create') ? getNewEntryUrl(collectionName) : ''; let url = collection.get('create') ? getNewEntryUrl(collectionName) : '';
if (url && filterTerm) { if (url && filterTerm) {
@ -102,9 +104,9 @@ const CollectionView = ({
const renderEntriesCollection = useCallback(() => { const renderEntriesCollection = useCallback(() => {
return ( return (
<EntriesCollection collection={collection} viewStyle={viewStyle} filterTerm={filterTerm} /> <EntriesCollection collection={collection} viewStyle={viewStyle} filterTerm={filterTerm} readyToLoad={readyToLoad} />
); );
}, [collection, filterTerm, viewStyle]); }, [collection, filterTerm, viewStyle, readyToLoad]);
const renderEntriesSearch = useCallback(() => { const renderEntriesSearch = useCallback(() => {
return ( return (
@ -117,6 +119,7 @@ const CollectionView = ({
useEffect(() => { useEffect(() => {
if (sort?.first()?.get('key')) { if (sort?.first()?.get('key')) {
setReadyToLoad(true);
return; return;
} }
@ -125,10 +128,26 @@ const CollectionView = ({
| undefined; | undefined;
if (!defaultSort || !defaultSort.get('field')) { if (!defaultSort || !defaultSort.get('field')) {
setReadyToLoad(true);
return; return;
} }
onSortClick(defaultSort.get('field'), defaultSort.get('direction') ?? SortDirection.Ascending); let alive = true;
const sortEntries = async () => {
await onSortClick(
defaultSort.get('field'),
defaultSort.get('direction') ?? SortDirection.Ascending,
);
if (alive) {
setReadyToLoad(true);
}
};
sortEntries();
return () => {
alive = false;
};
}, [collection]); }, [collection]);
return ( return (

View File

@ -68,18 +68,19 @@ class EntriesCollection extends React.Component {
loadEntries: PropTypes.func.isRequired, loadEntries: PropTypes.func.isRequired,
traverseCollectionCursor: PropTypes.func.isRequired, traverseCollectionCursor: PropTypes.func.isRequired,
entriesLoaded: PropTypes.bool, entriesLoaded: PropTypes.bool,
readyToLoad: PropTypes.bool,
}; };
componentDidMount() { componentDidMount() {
const { collection, entriesLoaded, loadEntries } = this.props; const { collection, entriesLoaded, loadEntries, readyToLoad } = this.props;
if (collection && !entriesLoaded) { if (collection && !entriesLoaded && readyToLoad) {
loadEntries(collection); loadEntries(collection);
} }
} }
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
const { collection, entriesLoaded, loadEntries } = this.props; const { collection, entriesLoaded, loadEntries, readyToLoad } = this.props;
if (collection !== prevProps.collection && !entriesLoaded) { if (!entriesLoaded && readyToLoad && !prevProps.readyToLoad) {
loadEntries(collection); loadEntries(collection);
} }
} }