fix(netlify-cms-core): avoid partially loaded collection list (#1964)

This commit is contained in:
Bartholomew
2018-12-19 21:28:45 +01:00
committed by Shawn Erquhart
parent 42d6478117
commit cedcbf89a5
5 changed files with 38 additions and 20 deletions

View File

@ -42,4 +42,23 @@ describe('entries', () => {
),
);
});
it('should handle loaded entry', () => {
const entry = { slug: 'a', path: '' };
expect(reducer(initialState, actions.entryLoaded(Map({ name: 'posts' }), entry))).toEqual(
OrderedMap(
fromJS({
posts: { name: 'posts' },
entities: {
'posts.a': { slug: 'a', path: '' },
},
pages: {
posts: {
ids: ['a'],
},
},
}),
),
);
});
});

View File

@ -15,6 +15,7 @@ let collection;
let loadedEntries;
let append;
let page;
let slug;
const entries = (state = Map({ entities: Map(), pages: Map() }), action) => {
switch (action.type) {
@ -25,10 +26,15 @@ const entries = (state = Map({ entities: Map(), pages: Map() }), action) => {
);
case ENTRY_SUCCESS:
return state.setIn(
['entities', `${action.payload.collection}.${action.payload.entry.slug}`],
fromJS(action.payload.entry),
);
collection = action.payload.collection;
slug = action.payload.entry.slug;
return state.withMutations(map => {
map.setIn(['entities', `${collection}.${slug}`], fromJS(action.payload.entry));
const ids = map.getIn(['pages', collection, 'ids'], List());
if (!ids.includes(slug)) {
map.setIn(['pages', collection, 'ids'], ids.unshift(slug));
}
});
case ENTRIES_REQUEST:
return state.setIn(['pages', action.payload.collection, 'isFetching'], true);