{"size": 1803, "ext": "js", "max_stars_repo_path": "src/reducers/fermenter_reducer.js", "max_stars_repo_name": "jonrhall/swill-ui", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/reducers/fermenter_reducer.js", "max_issues_repo_name": "jonrhall/swill-ui", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/reducers/fermenter_reducer.js", "max_forks_repo_name": "jonrhall/swill-ui", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.5573770492, "max_line_length": 92, "alphanum_fraction": 0.6577925679}
const initialFermenterState = {
  loadingList: false,
  loadingTypes: false,
  fermenters: [],
  types: []
};

// Update an fermenter in the fermenters list
const updateFermenterList = (newFermenter, state) =>
  Object.assign({}, state, {
    fermenters: state.fermenters.map((fermenter) => {
      if (newFermenter.id === fermenter.id) {
        return newFermenter;
      }
      return fermenter;
    })
  });

const fermenterReducer = (state = initialFermenterState, action) => {
  switch (action.type) {
    case 'GET_FERMENTERS_PENDING':
      return Object.assign({}, state, {
        loadingList: true
      });
    case 'GET_FERMENTERS_FULFILLED':
      return Object.assign({}, state, {
        loadingList: false,
        fermenters: action.payload
      });
    case 'GET_FERMENTER_TYPES_PENDING':
      return Object.assign({}, state, {
        loadingTypes: true
      });
    case 'GET_FERMENTER_TYPES_FULFILLED':
      return Object.assign({}, state, {
        loadingTypes: false,
        types: action.payload
      });
    case 'CREATE_FERMENTER_FULFILLED':
      return Object.assign({}, state, {
        fermenters: [...state.fermenters, action.payload]
      });
    case 'REMOVE_FERMENTER_FULFILLED':
      return Object.assign({}, state, {
        fermenters: state.fermenters.filter(fermenter => fermenter.id !== action.payload.id)
      });
    case 'EDIT_FERMENTER_NAME_FULFILLED':
    case 'EDIT_FERMENTER_HEATER_FULFILLED':
    case 'EDIT_FERMENTER_COOLER_FULFILLED':
    case 'EDIT_FERMENTER_SENSOR_FULFILLED':
    case 'EDIT_FERMENTER_LOGIC_FULFILLED':
      return updateFermenterList(action.payload, state);
    case 'FERMENTER_UPDATE':
      return updateFermenterList(action.fermenter, state);
    default:
      return state;
  }
};

export default fermenterReducer;
