{"size": 1077, "ext": "js", "max_stars_repo_path": "client/redux/singleCandy.js", "max_stars_repo_name": "2011-team-coco/quicklys-shop", "max_stars_repo_stars_event_min_datetime": "2021-05-26T05:03:04.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-26T05:03:04.000Z", "max_issues_repo_path": "client/redux/singleCandy.js", "max_issues_repo_name": "2011-team-coco/quicklys-shop", "max_issues_repo_issues_event_min_datetime": "2021-01-11T17:49:12.000Z", "max_issues_repo_issues_event_max_datetime": "2021-01-21T02:01:37.000Z", "max_forks_repo_path": "client/redux/singleCandy.js", "max_forks_repo_name": "2011-team-coco/quicklys-shop", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 22, "max_forks_count": null, "avg_line_length": 19.2321428571, "max_line_length": 74, "alphanum_fraction": 0.6276694522}
import axios from 'axios'

const SET_SINGLE_CANDY = 'SET_SINGLE_CANDY'
const EDIT_CANDY = 'EDIT_CANDY'

const setSingleCandy = candy => {
  return {
    type: SET_SINGLE_CANDY,
    candy
  }
}
const editCandy = candy => {
  return {
    type: EDIT_CANDY,
    candy
  }
}

export const fetchSingleCandy = id => {
  return async dispatch => {
    try {
      const {data} = await axios.get(`/api/candies/${id}`)
      dispatch(setSingleCandy(data))
    } catch (error) {
      console.log(error)
    }
  }
}

export const modifyCandy = (previousCandyId, modifiedCandy) => {
  return async dispatch => {
    try {
      const {data} = await axios.put(
        `/api/candies/${previousCandyId}`,
        modifiedCandy
      )
      dispatch(editCandy(data))
    } catch (error) {
      console.log(error)
    }
  }
}

const initialState = {}

export default function singleCandyReducer(state = initialState, action) {
  switch (action.type) {
    case SET_SINGLE_CANDY:
      return action.candy
    case EDIT_CANDY:
      return action.candy
    default:
      return state
  }
}
