2016-09-06 13:04:17 -03:00
|
|
|
import { Map, List, fromJS } from 'immutable';
|
2016-09-13 04:29:30 -03:00
|
|
|
import { EDITORIAL_WORKFLOW } from '../constants/publishModes';
|
2016-09-06 13:04:17 -03:00
|
|
|
import {
|
2016-09-13 16:00:24 -03:00
|
|
|
UNPUBLISHED_ENTRY_REQUEST,
|
|
|
|
UNPUBLISHED_ENTRY_SUCCESS,
|
|
|
|
UNPUBLISHED_ENTRIES_REQUEST,
|
|
|
|
UNPUBLISHED_ENTRIES_SUCCESS,
|
2016-10-18 12:32:39 -02:00
|
|
|
UNPUBLISHED_ENTRY_STATUS_CHANGE_REQUEST,
|
|
|
|
UNPUBLISHED_ENTRY_PUBLISH_REQUEST,
|
2016-09-06 13:04:17 -03:00
|
|
|
} from '../actions/editorialWorkflow';
|
2016-09-13 04:29:30 -03:00
|
|
|
import { CONFIG_SUCCESS } from '../actions/config';
|
2016-09-06 13:04:17 -03:00
|
|
|
|
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-13 04:29:30 -03:00
|
|
|
case CONFIG_SUCCESS:
|
|
|
|
const publish_mode = action.payload && action.payload.publish_mode;
|
|
|
|
if (publish_mode === EDITORIAL_WORKFLOW) {
|
|
|
|
// Editorial workflow state is explicetelly initiated after the config.
|
|
|
|
return Map({ entities: Map(), pages: Map() });
|
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
2016-09-13 04:09:52 -03:00
|
|
|
case UNPUBLISHED_ENTRY_REQUEST:
|
2016-10-18 12:32:39 -02:00
|
|
|
return state.setIn(['entities', `${ action.payload.status }.${ action.payload.slug }`, 'isFetching'], true);
|
2016-09-13 04:09:52 -03:00
|
|
|
|
|
|
|
case UNPUBLISHED_ENTRY_SUCCESS:
|
|
|
|
return state.setIn(
|
2016-10-18 12:32:39 -02:00
|
|
|
['entities', `${ action.payload.status }.${ action.payload.entry.slug }`],
|
2016-09-13 04:09:52 -03:00
|
|
|
fromJS(action.payload.entry)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
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) => {
|
2016-10-18 12:32:39 -02:00
|
|
|
entries.forEach(entry => (
|
|
|
|
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,
|
2016-10-18 12:32:39 -02:00
|
|
|
ids: List(entries.map(entry => entry.slug)),
|
2016-09-06 13:04:17 -03:00
|
|
|
}));
|
|
|
|
});
|
2016-09-13 16:00:24 -03:00
|
|
|
|
2016-10-18 12:32:39 -02:00
|
|
|
case UNPUBLISHED_ENTRY_STATUS_CHANGE_REQUEST:
|
|
|
|
// Update Optimistically
|
2016-09-13 16:00:24 -03:00
|
|
|
return state.withMutations((map) => {
|
2016-10-18 12:32:39 -02:00
|
|
|
let entry = map.getIn(['entities', `${ action.payload.oldStatus }.${ action.payload.slug }`]);
|
2016-09-14 18:25:45 -03:00
|
|
|
entry = entry.setIn(['metaData', 'status'], action.payload.newStatus);
|
2016-09-13 16:00:24 -03:00
|
|
|
|
|
|
|
let entities = map.get('entities').filter((val, key) => (
|
2016-10-18 12:32:39 -02:00
|
|
|
key !== `${ action.payload.oldStatus }.${ action.payload.slug }`
|
2016-09-13 16:00:24 -03:00
|
|
|
));
|
2016-10-18 12:32:39 -02:00
|
|
|
entities = entities.set(`${ action.payload.newStatus }.${ action.payload.slug }`, entry);
|
2016-09-13 16:00:24 -03:00
|
|
|
|
|
|
|
map.set('entities', entities);
|
|
|
|
});
|
|
|
|
|
2016-10-18 12:32:39 -02:00
|
|
|
case UNPUBLISHED_ENTRY_PUBLISH_REQUEST:
|
|
|
|
// Update Optimistically
|
|
|
|
return state.deleteIn(['entities', `${ action.payload.status }.${ action.payload.slug }`]);
|
2016-09-14 18:25:45 -03:00
|
|
|
|
2016-09-06 13:04:17 -03:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-13 03:59:48 -03:00
|
|
|
export const selectUnpublishedEntry = (state, status, slug) => {
|
2016-10-18 12:32:39 -02:00
|
|
|
return state && state.getIn(['entities', `${ status }.${ slug }`]);
|
2016-09-13 03:59:48 -03:00
|
|
|
};
|
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;
|