Skip to content

Commit

Permalink
fix(core): pass loadEntry to widgets as utility function and not redu…
Browse files Browse the repository at this point in the history
…x action (#3439)
  • Loading branch information
d4rekanguok committed Mar 20, 2020
1 parent 9616cdb commit 6d87655
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
10 changes: 7 additions & 3 deletions packages/netlify-cms-core/src/actions/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,11 @@ export function deleteLocalBackup(collection: Collection, slug: string) {

export function loadEntry(collection: Collection, slug: string) {
return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
const state = getState();
const backend = currentBackend(state.config);
await waitForMediaLibraryToLoad(dispatch, getState());
dispatch(entryLoading(collection, slug));

try {
const loadedEntry = await backend.getEntry(getState(), collection, slug);
const loadedEntry = await tryLoadEntry(getState(), collection, slug);
dispatch(entryLoaded(collection, loadedEntry));
dispatch(createDraftFromEntry(loadedEntry));
} catch (error) {
Expand All @@ -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({
['append_next']: { action: 'next', append: true },
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { partial, uniqueId } from 'lodash';
import { connect } from 'react-redux';
import { FieldLabel, colors, transitions, lengths, borders } from 'netlify-cms-ui-default';
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 { selectIsLoadingAsset } from 'Reducers/medias';
import { query, clearSearch } from 'Actions/search';
Expand Down Expand Up @@ -269,13 +269,24 @@ const mapStateToProps = state => {
const collection = collections.get(entryDraft.getIn(['entry', 'collection']));
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 {
mediaPaths: state.mediaLibrary.get('controlMedia'),
isFetching: state.search.get('isFetching'),
queryHits: state.search.get('queryHits'),
collection,
entry,
isLoadingAsset,
loadEntry,
};
};

Expand All @@ -295,10 +306,6 @@ const mapDispatchToProps = dispatch => {
);
return {
...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),
};
};
Expand Down

0 comments on commit 6d87655

Please sign in to comment.