import { combineReducers } from 'redux';

const createFilteredListReducerFor = (filter) => {
  const ids = (state = [], action) => {
    if(action.filter !== filter) { return state; }

    switch (action.type) {
     case 'FETCH_TODOS_SUCCESS':
       return action.response.map(todo => todo.id);
     default:
       return state;
    }
  };

  const isFetching = (state = false, action) => {
    if(action.filter !== filter) { return state; }

    switch (action.type) {
      case 'FETCH_TODOS_SUCCESS':
      case 'FETCH_TODOS_FAILURE':
        return false;
      case 'FETCH_TODOS_REQUEST':
        return true;
      default:
        return state;
    }
  };

  return combineReducers({ ids, isFetching });
};

export default createFilteredListReducerFor;

export const getIds = (state) => state.ids;
export const getIsFetching = (state) => state.isFetching;

import { combineReducers } from 'redux';

const createFilteredListReducerFor = (filter) => {
  const ids = (state = [], action) => {
    if(action.filter !== filter) { return state; }

    switch (action.type) {
     case 'FETCH_TODOS_SUCCESS':
       return action.response.map(todo => todo.id);
     default:
       return state;
    }
  };

  const isFetching = (state = false, action) => {
    if(action.filter !== filter) { return state; }

    switch (action.type) {
      case 'FETCH_TODOS_SUCCESS':
      case 'FETCH_TODOS_FAILURE':
        return false;
      case 'FETCH_TODOS_REQUEST':
        return true;
      default:
        return state;
    }
  };

  const errorMessage = (state = null, action) => {
    if(action.filter !== filter) { return state; }

    switch (action.type) {
      case 'FETCH_TODOS_FAILURE':
        return action.message;
      case 'FETCH_TODOS_SUCCESS':
      case 'FETCH_TODOS_REQUEST':
        return null;
      default:
        return state;
    }
  };

  return combineReducers({ ids, isFetching, errorMessage });
};

export default createFilteredListReducerFor;

export const getIds = (state) => state.ids;
export const getIsFetching = (state) => state.isFetching;
export const getErrorMessage = (state) => state.errorMessage;

add errorMessage reducer to createFilteredListReducerFor