Created
July 24, 2015 08:28
-
-
Save tadjik1/414dcc5894ad1fe6fe99 to your computer and use it in GitHub Desktop.
example of reducer
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
import { | |
RECEIVED_NEW_RELEASES, | |
RECEIVED_FREE_MOVIES | |
} from '../constants/ActionTypes'; | |
import selectn from 'selectn'; | |
import { merge, union } from 'lodash'; | |
const initialState = { | |
allMovies: {}, | |
releases: [], | |
free: [] | |
}; | |
/** | |
* | |
* @param {Object} state Current state | |
* @param {Object} action Action object with type and payload | |
* @returns {Object} New state | |
*/ | |
export default function moviesReducer(state = initialState, action) { | |
const movies = selectn('payload.entities.movies', action); | |
const ids = selectn('payload.result.movies', action); | |
if (!movies) return state; | |
// we want grab all movies (it's not important with which action it comes) | |
const newState = merge({}, state, { | |
allMovies: movies | |
}); | |
switch (action.type) { | |
case RECEIVED_NEW_RELEASES: | |
return merge({}, newState, { | |
releases: union(newState.releases, ids).sort() | |
}); | |
case RECEIVED_FREE_MOVIES: | |
return merge({}, newState, { | |
free: union(newState.free, ids).sort() | |
}); | |
default: | |
return newState; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment