-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core Data: Refactor, add comments and avoiding returning new instance…
…s in each selector call
- Loading branch information
1 parent
5a20290
commit d741b82
Showing
3 changed files
with
34 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,30 @@ | ||
const reducer = ( state, action ) => { | ||
/** | ||
* External dependencies | ||
*/ | ||
import { uniqBy } from 'lodash'; | ||
import { combineReducers } from 'redux'; | ||
|
||
/** | ||
* Reducer returning the categories list. | ||
* | ||
* @param {Object} state Current state. | ||
* @param {Object} action Dispatched action. | ||
* | ||
* @return {string} Updated state. | ||
*/ | ||
function categories( state = [], action ) { | ||
switch ( action.type ) { | ||
case 'FETCH_CATEGORIES_SUCCESS': | ||
return action.categories.reduce( ( memo, category ) => ( { | ||
...memo, | ||
[ category.id ]: category, | ||
} ), {} ); | ||
case 'RECEIVE_CATEGORIES': | ||
return uniqBy( | ||
[ | ||
...action.categories, | ||
...state, | ||
], | ||
( category ) => category.id | ||
); | ||
} | ||
|
||
return state; | ||
}; | ||
} | ||
|
||
export default reducer; | ||
export default combineReducers( { categories } ); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import { values } from 'lodash'; | ||
|
||
/** | ||
* Returns all the available categories. | ||
* | ||
* @param {Object} state Global application state. | ||
* | ||
* @return {Array} Categories List | ||
*/ | ||
export function getCategories( state ) { | ||
return values( state ); | ||
return state.categories; | ||
} |