Skip to content

Instantly share code, notes, and snippets.

@idream3
Last active August 29, 2015 14:21
Show Gist options
  • Save idream3/dceec5e8bf4021329b20 to your computer and use it in GitHub Desktop.
Save idream3/dceec5e8bf4021329b20 to your computer and use it in GitHub Desktop.
/*
Extracting Maps completely out of the state tree and defining below.
Also, flattening the entire tree.
Con: No way to tell what is a Map or not
*/
var state = {
// application data
data: {
users: {},
projects: {},
license: {},
locations: {},
locationSchedules: {},
locationServices: {},
crawlers: {}
concepts: {},
conceptRules: {},
conceptRuleParameters: {},
reports: {
// much more data
}
},
view: {
currentUserId: null,
selectedLocationId: null,
locationSortType: 'ASC',
locationSortBy: 'status',
selectedConceptId: null,
conceptSortType: 'DESC',
conceptSortBy: 'name'
}
}
Cerebral.map('view.selectedUser',
['data.users', 'view.selectedUserId'],
(Cerebral, state) => {
return state.users[state.selectedUserId]
})
Cerebral.map('view.selectedLocation',
['view.selectedLocationId', 'data.locations'],
(Cerebral, state) => {
let location = state.locations[state.selectedLocationId].toJS(); // so we can mutate
let schedules = Cerebral.get('data.locationSchedules');
// grab echedules and attach to location object
location.schedules = location.scheduleIds.filter(id => {
return schedules[id]
})
return location
})
Cerebral.map('view.sortedLocations',
['view.locationSortType', 'view.locationSortBy', 'data.locations'],
(Cerebral, state) => {
return Object.keys(state.locations).map(id => {
return state.locations[id]
}).sort(/* sort function */)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment