{"size": 1634, "ext": "js", "max_stars_repo_path": "diary/fe/src/actions/drink.js", "max_stars_repo_name": "phoffmeister/diary", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "diary/fe/src/actions/drink.js", "max_issues_repo_name": "phoffmeister/diary", "max_issues_repo_issues_event_min_datetime": "2021-04-08T19:38:28.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-12T00:47:30.000Z", "max_forks_repo_path": "diary/fe/src/actions/drink.js", "max_forks_repo_name": "phoffmeister/diary", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 7, "max_forks_count": null, "avg_line_length": 28.1724137931, "max_line_length": 75, "alphanum_fraction": 0.5869033048}
import axios from "axios";
import { tokenConfig } from "./auth";
import { DELETE_DRINK_SUCCESS, GET_DRINK_OPTS, GET_DAY } from "./types";
import { createMessage, MERROR, MSUCCESS, MINFO } from "./messages";

export const getDrinkOpts = () => (dispatch, getState) => {
  axios
    .get("/api/opts/drink/", tokenConfig(getState))
    .then((res) => {
      dispatch({
        type: GET_DRINK_OPTS,
        payload: res.data,
      });
    })
    .catch((err) =>
      dispatch(createMessage("cannot get drink options", MERROR))
    );
};

export const createDrink = (drinkEntry, successCallback) => (
  dispatch,
  getState
) => {
  axios
    .post("/api/drink/", drinkEntry, tokenConfig(getState))
    .then((res) => {
      axios
        .get(`/api/day/${res.data.day}/`, tokenConfig(getState))
        .then((res) => {
          dispatch({
            type: GET_DAY,
            payload: res.data,
          });
          dispatch(createMessage("DrinkEntry created", MSUCCESS));
          if (successCallback) successCallback();
        })
        .catch((err) => dispatch(createMessage("cannot get day", MERROR)));
    })
    .catch((err) =>
      dispatch(createMessage("cannot create drink entry", MERROR))
    );
};

export const deleteDrink = (drinkID) => (dispatch, getState) => {
  axios
    .delete(`/api/drink/${drinkID}/`, tokenConfig(getState))
    .then((res) => {
      dispatch({
        type: DELETE_DRINK_SUCCESS,
        payload: drinkID,
      });
      dispatch(createMessage("DrinkEntry deleted!", MSUCCESS));
    })
    .catch((err) =>
      dispatch(createMessage("cannot delete DrinkEntry", MERROR))
    );
};
