Skip to content

Instantly share code, notes, and snippets.

@tanduong
Last active May 30, 2017 08:18
Show Gist options
  • Save tanduong/045475c6eefceebdbc4ae04a1824273c to your computer and use it in GitHub Desktop.
Save tanduong/045475c6eefceebdbc4ae04a1824273c to your computer and use it in GitHub Desktop.
reduceColection redux
import { fromJS, List, Map } from 'immutable';
const reduceColection = (state, response) => {
if(response) {
const {
data,
page,
per_page,
total_count,
} = response;
const objects = state.get('objects');
const ids = state.get('ids');
const newObjects = {};
const newIds = data.map((obj) => {
newObjects[obj.id] = obj;
return obj.id;
});
const finalIds = List(newIds).concat(ids).slice(0, 5);
const finalObjects = new Map(finalIds.reduce((acc, id) => {
const key = id.toString();
acc[key] = newObjects[key] || objects.get(key);
return acc;
}, {}));
return state
.set('objects', finalObjects)
.set('ids', finalIds)
.set('page', page)
.set('per_page', per_page)
.set('total_count', total_count);
}
}
export default reduceColection;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment