Last active
May 30, 2017 08:18
-
-
Save tanduong/045475c6eefceebdbc4ae04a1824273c to your computer and use it in GitHub Desktop.
reduceColection redux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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