2016-06-05 01:52:18 -07:00
|
|
|
import { Map, List, fromJS } from 'immutable';
|
|
|
|
import {
|
2016-10-12 16:04:58 +02:00
|
|
|
ENTRY_REQUEST,
|
|
|
|
ENTRY_SUCCESS,
|
2017-01-10 22:23:22 -02:00
|
|
|
ENTRY_FAILURE,
|
2016-10-12 16:04:58 +02:00
|
|
|
ENTRIES_REQUEST,
|
|
|
|
ENTRIES_SUCCESS,
|
2016-06-05 01:52:18 -07:00
|
|
|
} from '../actions/entries';
|
|
|
|
|
2016-12-07 15:44:07 -02:00
|
|
|
import { SEARCH_ENTRIES_SUCCESS } from '../actions/search';
|
|
|
|
|
2016-10-12 16:04:58 +02:00
|
|
|
let collection;
|
|
|
|
let loadedEntries;
|
|
|
|
let page;
|
2016-10-10 15:34:21 -03:00
|
|
|
|
2016-06-16 22:49:48 -03:00
|
|
|
const entries = (state = Map({ entities: Map(), pages: Map() }), action) => {
|
2016-06-05 01:52:18 -07:00
|
|
|
switch (action.type) {
|
|
|
|
case ENTRY_REQUEST:
|
2016-10-12 16:04:58 +02:00
|
|
|
return state.setIn(['entities', `${ action.payload.collection }.${ action.payload.slug }`, 'isFetching'], true);
|
2016-09-06 13:04:17 -03:00
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
case ENTRY_SUCCESS:
|
|
|
|
return state.setIn(
|
2016-10-12 16:04:58 +02:00
|
|
|
['entities', `${ action.payload.collection }.${ action.payload.entry.slug }`],
|
2016-06-05 01:52:18 -07:00
|
|
|
fromJS(action.payload.entry)
|
|
|
|
);
|
2016-09-06 13:04:17 -03:00
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
case ENTRIES_REQUEST:
|
|
|
|
return state.setIn(['pages', action.payload.collection, 'isFetching'], true);
|
2016-09-06 13:04:17 -03:00
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
case ENTRIES_SUCCESS:
|
2016-10-10 15:34:21 -03:00
|
|
|
collection = action.payload.collection;
|
|
|
|
loadedEntries = action.payload.entries;
|
|
|
|
page = action.payload.page;
|
2016-06-05 01:52:18 -07:00
|
|
|
return state.withMutations((map) => {
|
2016-10-12 16:04:58 +02:00
|
|
|
loadedEntries.forEach(entry => (
|
|
|
|
map.setIn(['entities', `${ collection }.${ entry.slug }`], fromJS(entry).set('isFetching', false))
|
2016-06-05 01:52:18 -07:00
|
|
|
));
|
2016-10-10 15:34:21 -03:00
|
|
|
|
2016-10-12 16:04:58 +02:00
|
|
|
const ids = List(loadedEntries.map(entry => entry.slug));
|
2016-06-05 01:52:18 -07:00
|
|
|
map.setIn(['pages', collection], Map({
|
2016-10-12 16:04:58 +02:00
|
|
|
page,
|
2016-10-20 11:30:29 -02:00
|
|
|
ids: (!page || page === 0) ? ids : map.getIn(['pages', collection, 'ids'], List()).concat(ids),
|
2016-10-10 15:34:21 -03:00
|
|
|
}));
|
|
|
|
});
|
2017-01-10 22:23:22 -02:00
|
|
|
|
|
|
|
case ENTRY_FAILURE:
|
|
|
|
return state.withMutations((map) => {
|
|
|
|
map.setIn(['entities', `${ action.payload.collection }.${ action.payload.slug }`, 'isFetching'], false);
|
|
|
|
map.setIn(['entities', `${ action.payload.collection }.${ action.payload.slug }`, 'error'], action.payload.error.message);
|
|
|
|
});
|
2016-10-10 15:34:21 -03:00
|
|
|
|
|
|
|
case SEARCH_ENTRIES_SUCCESS:
|
|
|
|
loadedEntries = action.payload.entries;
|
|
|
|
return state.withMutations((map) => {
|
2016-10-12 16:04:58 +02:00
|
|
|
loadedEntries.forEach(entry => (
|
|
|
|
map.setIn(['entities', `${ entry.collection }.${ entry.slug }`], fromJS(entry).set('isFetching', false))
|
2016-10-10 15:34:21 -03:00
|
|
|
));
|
2016-06-05 01:52:18 -07:00
|
|
|
});
|
2016-09-06 13:04:17 -03:00
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2016-06-10 00:16:01 -03:00
|
|
|
};
|
2016-06-05 01:52:18 -07:00
|
|
|
|
2016-06-10 00:16:01 -03:00
|
|
|
export const selectEntry = (state, collection, slug) => (
|
2016-10-12 16:04:58 +02:00
|
|
|
state.getIn(['entities', `${ collection }.${ slug }`])
|
2016-06-10 00:16:01 -03:00
|
|
|
);
|
2016-06-05 01:52:18 -07:00
|
|
|
|
2016-06-10 00:16:01 -03:00
|
|
|
export const selectEntries = (state, collection) => {
|
|
|
|
const slugs = state.getIn(['pages', collection, 'ids']);
|
2016-10-12 16:04:58 +02:00
|
|
|
return slugs && slugs.map(slug => selectEntry(state, collection, slug));
|
2016-06-10 00:16:01 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
export default entries;
|