{"size": 1333, "ext": "js", "max_stars_repo_path": "app/javascript/oxalates/actions/fetchFoods.js", "max_stars_repo_name": "adamstegman/oxalates", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/javascript/oxalates/actions/fetchFoods.js", "max_issues_repo_name": "adamstegman/oxalates", "max_issues_repo_issues_event_min_datetime": "2020-02-01T17:05:55.000Z", "max_issues_repo_issues_event_max_datetime": "2020-02-01T17:05:55.000Z", "max_forks_repo_path": "app/javascript/oxalates/actions/fetchFoods.js", "max_forks_repo_name": "adamstegman/oxalates", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 25.6346153846, "max_line_length": 75, "alphanum_fraction": 0.6504126032}
export const FETCH_FOODS_REQUEST = 'FETCH_FOODS_REQUEST';
export const fetchFoodsRequest = listId => {
  return {
    type: FETCH_FOODS_REQUEST,
    listId,
  };
};

export const FETCH_FOODS_SUCCESS = 'FETCH_FOODS_SUCCESS';
export const fetchFoodsSuccess = (listId, response) => {
  const foods = response.foods.map(food => {
    const newFood = Object.assign({}, food);
    newFood.oxalateMg = food.oxalate_mg;
    delete newFood.oxalate_mg;
    return newFood;
  });
  return {
    type: FETCH_FOODS_SUCCESS,
    listId,
    foods,
  };
};

export const FETCH_FOODS_FAILURE = 'FETCH_FOODS_FAILURE';
export const fetchFoodsFailure = (listId, err) => {
  return {
    type: FETCH_FOODS_FAILURE,
    listId,
    err,
  };
};

const FOODS_URL = '/foods';
const LIST_ID_PARAM = 'list_id';
export const fetchFoods = (listId) => {
  return dispatch => {
    dispatch(fetchFoodsRequest(listId));
    return fetch(`${FOODS_URL}?${LIST_ID_PARAM}=${listId}`, {
      headers: { 'accept': 'application/json' },
    }).then(response => {
      if (response.ok) {
        return response.json();
      }
      throw new Error(`Expected OK response, got code=${response.status}`);
    }).then(response => {
      dispatch(fetchFoodsSuccess(listId, response));
    }).catch(err => {
      dispatch(fetchFoodsFailure(listId, err));
    });
  };
};
