fix(core): pass loadEntry to widgets as utility function and not redux action (#3439)

This commit is contained in:
Derek Nguyen 2020-03-20 19:54:53 +09:00 committed by GitHub
parent 9616cdb8bb
commit 6d8765521c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View File

@ -335,13 +335,11 @@ export function deleteLocalBackup(collection: Collection, slug: string) {
export function loadEntry(collection: Collection, slug: string) { export function loadEntry(collection: Collection, slug: string) {
return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => { return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
const state = getState();
const backend = currentBackend(state.config);
await waitForMediaLibraryToLoad(dispatch, getState()); await waitForMediaLibraryToLoad(dispatch, getState());
dispatch(entryLoading(collection, slug)); dispatch(entryLoading(collection, slug));
try { try {
const loadedEntry = await backend.getEntry(getState(), collection, slug); const loadedEntry = await tryLoadEntry(getState(), collection, slug);
dispatch(entryLoaded(collection, loadedEntry)); dispatch(entryLoaded(collection, loadedEntry));
dispatch(createDraftFromEntry(loadedEntry)); dispatch(createDraftFromEntry(loadedEntry));
} catch (error) { } catch (error) {
@ -361,6 +359,12 @@ export function loadEntry(collection: Collection, slug: string) {
}; };
} }
export async function tryLoadEntry(state: State, collection: Collection, slug: string) {
const backend = currentBackend(state.config);
const loadedEntry = await backend.getEntry(state, collection, slug);
return loadedEntry;
}
const appendActions = fromJS({ const appendActions = fromJS({
['append_next']: { action: 'next', append: true }, ['append_next']: { action: 'next', append: true },
}); });

View File

@ -9,7 +9,7 @@ import { partial, uniqueId } from 'lodash';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { FieldLabel, colors, transitions, lengths, borders } from 'netlify-cms-ui-default'; import { FieldLabel, colors, transitions, lengths, borders } from 'netlify-cms-ui-default';
import { resolveWidget, getEditorComponents } from 'Lib/registry'; import { resolveWidget, getEditorComponents } from 'Lib/registry';
import { clearFieldErrors, loadEntry } from 'Actions/entries'; import { clearFieldErrors, tryLoadEntry } from 'Actions/entries';
import { addAsset, boundGetAsset } from 'Actions/media'; import { addAsset, boundGetAsset } from 'Actions/media';
import { selectIsLoadingAsset } from 'Reducers/medias'; import { selectIsLoadingAsset } from 'Reducers/medias';
import { query, clearSearch } from 'Actions/search'; import { query, clearSearch } from 'Actions/search';
@ -269,6 +269,16 @@ const mapStateToProps = state => {
const collection = collections.get(entryDraft.getIn(['entry', 'collection'])); const collection = collections.get(entryDraft.getIn(['entry', 'collection']));
const isLoadingAsset = selectIsLoadingAsset(state.medias); const isLoadingAsset = selectIsLoadingAsset(state.medias);
const loadEntry = async (collectionName, slug) => {
const targetCollection = collections.get(collectionName);
if (targetCollection) {
const loadedEntry = await tryLoadEntry(state, targetCollection, slug);
return loadedEntry;
} else {
throw new Error(`Can't find collection '${collectionName}'`);
}
};
return { return {
mediaPaths: state.mediaLibrary.get('controlMedia'), mediaPaths: state.mediaLibrary.get('controlMedia'),
isFetching: state.search.get('isFetching'), isFetching: state.search.get('isFetching'),
@ -276,6 +286,7 @@ const mapStateToProps = state => {
collection, collection,
entry, entry,
isLoadingAsset, isLoadingAsset,
loadEntry,
}; };
}; };
@ -295,10 +306,6 @@ const mapDispatchToProps = dispatch => {
); );
return { return {
...creators, ...creators,
loadEntry: (collectionName, slug) => (dispatch, getState) => {
const collection = getState().collections.get(collectionName);
return loadEntry(collection, slug)(dispatch, getState);
},
boundGetAsset: (collection, entry) => boundGetAsset(dispatch, collection, entry), boundGetAsset: (collection, entry) => boundGetAsset(dispatch, collection, entry),
}; };
}; };