smashingboxes/cardboard

View on GitHub
app/javascript/main/restfulApi/reducerCreators/Immutable/createListReducer.js

Summary

Maintainability
B
5 hrs
Test Coverage
import Immutable from 'immutable';

function createListReducer(resource) {
  // eg PROJECTS
  const actionPrefix = resource.toUpperCase();

  const INITIAL_STATE = new Immutable.Map({
    isActive: false,
    isFetched: false,
    data: new Immutable.List()
  });

  return function reducer(state = INITIAL_STATE, action) {
    switch (action.type) {
    case `${actionPrefix}_LIST_START`:
      return state.withMutations((map) => {
        map.delete('error');
        map.set('isActive', true);
      });

    case `${actionPrefix}_LIST_SUCCESS`:
      return state.withMutations((map) => {
        map.set('isActive', false);
        map.set('isFetched', true);
        map.set('data', Immutable.fromJS(action.payload.response));
      });

    case `${actionPrefix}_LIST_FAILURE`:
      return state.withMutations((map) => {
        map.set('error', action.payload.error);
        map.set('isActive', false);
      });

    default:
      return state;
    }
  };
}

export default createListReducer;