{"size": 960, "ext": "js", "max_stars_repo_path": "src/components/store/categories.js", "max_stars_repo_name": "marlenerinker-401-advanced-javascript/virtual-store", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/components/store/categories.js", "max_issues_repo_name": "marlenerinker-401-advanced-javascript/virtual-store", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/components/store/categories.js", "max_forks_repo_name": "marlenerinker-401-advanced-javascript/virtual-store", "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": 23.4146341463, "max_line_length": 108, "alphanum_fraction": 0.6697916667}
/* eslint-disable no-case-declarations */
import axios from 'axios';
// Our global state object just for categories
const initialState = {
  categories: [],
  activeCategory: {},
};

// Reducers, a function that takes an action and produces a new version of state, from a payload and a type.
export default (state = initialState, action) => {
  const { type, payload } = action;

  switch (type) {
  case 'CHANGE':
  
    return {...state, activeCategory: payload};

  case 'FETCH_CATEGORIES':
    return {...state, categories: payload};

  default:
    return state;
  }
};


// actions are the functions that components can run themselves
export const changeCategory = (category) => {
  return {
    type: 'CHANGE',
    payload: category,
  };
};

export const fetchCategories = () => async (dispatch) => {
  const response = await axios.get('http://localhost:3000/categories');
  dispatch({
    type: 'FETCH_CATEGORIES',
    payload: response.data,
  });
};