{"size": 5240, "ext": "js", "max_stars_repo_path": "client/src/actions/index.js", "max_stars_repo_name": "babadee001/ovie-recipes", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "client/src/actions/index.js", "max_issues_repo_name": "babadee001/ovie-recipes", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "client/src/actions/index.js", "max_forks_repo_name": "babadee001/ovie-recipes", "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": 20.6299212598, "max_line_length": 73, "alphanum_fraction": 0.5700381679}
import axios from 'axios';
import {
  actions as toastrActions
} from 'react-redux-toastr';
import {
  bindActionCreators
} from 'redux';

export const BASE_URL = '/api/v1';

/**
 * @export errorHandler
 * @param {action} dispatch
 * @param {string} error
 * @param {constant} type
 * @returns {*} void
 */
export const errorHandler = (dispatch, error, type) => {
  const errorMessage = error.response ? error.response.data : error;
  dispatch({
    type,
    payload: errorMessage,
  });
  let errorData = {};
  errorData = Object.assign({}, errorMessage);
  errorData.message = '';
  errorData.error = '';

  setTimeout(() => {
    dispatch({
      type,
      payload: errorData
    });
  }, 5000);
};

// Post Request
/**
 * @export postData
 * @param {constant} action
 * @param {constant} errorType
 * @param {any} isAuthReq
 * @param {string} url
 * @param {action} dispatch
 * @param {object} data
 * @param {string} message
 * @param {constant} toastrConstant
 * @param {string} directTo
 * @returns {*} void
 */
export const postData = (
  action,
  errorType,
  isAuthReq,
  url,
  dispatch,
  data,
  message,
  toastrConstant,
  directTo
) => {
  const requestUrl = BASE_URL + url;
  let headers = {};

  if (isAuthReq) {
    headers = {
      headers: {
        'x-access-token': window.localStorage.getItem('token')
      }
    };
  }
  return axios.post(requestUrl, data, headers)
    .then((response) => {
      dispatch({
        type: action,
        payload: response.data,
      });
      if (directTo.length > 0) {
        window.location.hash = directTo;
      }
      if (toastrConstant.length > 2) {
        const toastr = bindActionCreators(toastrActions, dispatch);
        toastr.add({
          id: toastrConstant,
          type: 'success',
          title: 'Success',
          message,
          timeout: 5000,
        });
        setTimeout(() => {
          toastr.remove(toastrConstant);
        }, 3500);
      }
    })
    .catch((error) => {
      errorHandler(dispatch, error, errorType);
    });
};

// Get Request
/**
 * @export getData
 * @param {any} action
 * @param {any} errorType
 * @param {any} isAuthReq
 * @param {any} url
 * @param {any} dispatch
 *  @returns {*} void
 */
export const getData = (action, errorType, isAuthReq, url, dispatch) => {
  const requestUrl = BASE_URL + url;
  let headers = {};

  if (isAuthReq) {
    headers = {
      headers: {
        'x-access-token': window.localStorage.getItem('token')
      }
    };
  }

  return axios.get(requestUrl, headers)
    .then((response) => {
      dispatch({
        type: action,
        payload: response.data,
      });
    })
    .catch((error) => {
      errorHandler(dispatch, error, errorType);
    });
};

// Put Request
/**
 * @export putData
 * @param {constant} action
 * @param {constant} errorType
 * @param {any} isAuthReq
 * @param {string} url
 * @param {action} dispatch
 * @param {object} data
 * @param {string} message
 * @param {constant} toastrConstant
 * @param {string} directTo
 * @returns {*} void
 */
export const putData = (
  action,
  errorType,
  isAuthReq,
  url,
  dispatch,
  data,
  message,
  toastrConstant,
  directTo
) => {
  const requestUrl = BASE_URL + url;
  let headers = {};

  if (isAuthReq) {
    headers = {
      headers: {
        'x-access-token': window.localStorage.getItem('token')
      }
    };
  }

  return axios.put(requestUrl, data, headers)
    .then((response) => {
      const toastr = bindActionCreators(toastrActions, dispatch);
      dispatch({
        type: action,
        payload: response.data,
      });
      if (directTo.length > 3) {
        window.location.hash = directTo;
      }
      if (toastrConstant.length > 2) {
        toastr.add({
          id: toastrConstant,
          type: 'success',
          title: 'Success',
          message,
          timeout: 5000,
        });
        setTimeout(() => {
          toastr.remove(toastrConstant);
        }, 3500);
      }
    })
    .catch((error) => {
      errorHandler(dispatch, error, errorType);
    });
};

// Delete Request
/**
 * @export deleteData
 * @param {constant} action
 * @param {constant} errorType
 * @param {any} isAuthReq
 * @param {string} url
 * @param {action} dispatch
 * @param {string} message
 * @param {constant} toastrConstant
 * @returns {*} void
 */
export const deleteData = (
  action,
  errorType,
  isAuthReq,
  url,
  dispatch,
  message,
  toastrConstant
) => {
  const requestUrl = BASE_URL + url;
  let headers = {};

  if (isAuthReq) {
    headers = {
      headers: {
        'x-access-token': window.localStorage.getItem('token')
      }
    };
  }

  return axios.delete(requestUrl, headers)
    .then((response) => {
      dispatch({
        type: action,
        payload: response.data,
      });
      if (toastrConstant.length > 2) {
        const toastr = bindActionCreators(toastrActions, dispatch);
        toastr.add({
          id: toastrConstant,
          type: 'success',
          title: 'Success',
          message,
          timeout: 5000,
        });
        setTimeout(() => {
          toastr.remove(toastrConstant);
        }, 3500);
      }
    })
    .catch((error) => {
      errorHandler(dispatch, error, errorType);
    });
};
