Skip to content

Instantly share code, notes, and snippets.

@tadjik1
Created July 24, 2015 08:28
Show Gist options
  • Save tadjik1/414dcc5894ad1fe6fe99 to your computer and use it in GitHub Desktop.
Save tadjik1/414dcc5894ad1fe6fe99 to your computer and use it in GitHub Desktop.
example of reducer
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