static-cms/src/reducers/editorialWorkflow.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-09-06 13:04:17 -03:00
import { Map, List, fromJS } from 'immutable';
import {
2016-09-08 16:18:38 -03:00
INIT, UNPUBLISHED_ENTRIES_REQUEST, UNPUBLISHED_ENTRIES_SUCCESS
2016-09-06 13:04:17 -03:00
} from '../actions/editorialWorkflow';
2016-09-08 16:18:38 -03:00
const unpublishedEntries = (state = null, action) => {
2016-09-06 13:04:17 -03:00
switch (action.type) {
2016-09-08 16:18:38 -03:00
case INIT:
// Editorial workflow must be explicitly initiated.
return Map({ entities: Map(), pages: Map() });
2016-09-06 13:04:17 -03:00
case UNPUBLISHED_ENTRIES_REQUEST:
return state.setIn(['pages', 'isFetching'], true);
case UNPUBLISHED_ENTRIES_SUCCESS:
const { entries, pages } = action.payload;
return state.withMutations((map) => {
entries.forEach((entry) => (
2016-09-06 17:18:27 -03:00
map.setIn(['entities', `${entry.metaData.status}.${entry.slug}`], fromJS(entry).set('isFetching', false))
2016-09-06 13:04:17 -03:00
));
map.set('pages', Map({
...pages,
ids: List(entries.map((entry) => entry.slug))
}));
});
default:
return state;
}
};
export const selectUnpublishedEntry = (state, status, slug) => {
return state && state.getIn(['entities', `${status}.${slug}`]);
};
2016-09-06 13:04:17 -03:00
export const selectUnpublishedEntries = (state, status) => {
2016-09-08 16:18:38 -03:00
if (!state) return;
2016-09-06 13:04:17 -03:00
const slugs = state.getIn(['pages', 'ids']);
2016-09-08 19:04:54 -03:00
return slugs && slugs.reduce((acc, slug) => {
const entry = selectUnpublishedEntry(state, status, slug);
if (entry) {
return acc.push(entry);
} else {
return acc;
}
}, List());
2016-09-06 13:04:17 -03:00
};
2016-09-08 19:04:54 -03:00
2016-09-06 13:04:17 -03:00
export default unpublishedEntries;