text: import axios from 'axios';
  import cookie from 'react-cookie';
  import { logoutUser } from './auth';
  import { STATIC_ERROR, FETCH_USER } from '../constants';
  
  import { API_URL, AUTH_USER, AUTH_ERROR, UNAUTH_USER, FORGOT_PASSWORD_REQUEST, RESET_PASSWORD_REQUEST, PROTECTED_TEST } from '../constants';
  
  //= ===============================
  // Utility actions
  //= ===============================
  
  export function fetchUserByToken(token) {
    return function (dispatch) {
      axios.get(`${API_URL}/profile`, {
        headers: { Authorization: "Bearer " + cookie.load('token') },
      })
      .then((response) => {
        dispatch({
          type: FETCH_USER,
          payload: response.data.data,
        });
      })
      .catch(response => dispatch(errorHandler(response.data.error)));
    };
  }
  
  
  export function errorHandler(dispatch, error, type) {
    console.log('Error type: ', type);
    console.log(error);
  
    let errorMessage = error.response ? error.response.data : error;
  
     // NOT AUTHENTICATED ERROR
    if (error.status === 401 || error.response.status === 401) {
      errorMessage = 'You are not authorized to do this.';
      return dispatch(logoutUser(errorMessage));
    }
  
    dispatch({
      type,
      payload: errorMessage,
    });
  }
  
  // Post Request
  export function postData(action, errorType, isAuthReq, url, dispatch, data) {
    const requestUrl = API_URL + url;
    let headers = {};
  
    if (isAuthReq) {
      headers = { headers: { Authorization: cookie.load('token') } };
    }
  
    axios.post(requestUrl, data, headers)
    .then((response) => {
      dispatch({
        type: action,
        payload: response.data,
      });
    })
    .catch((error) => {
      errorHandler(dispatch, error.response, errorType);
    });
  }
  
  // Get Request
  export function getData(action, errorType, isAuthReq, url, dispatch) {
    const requestUrl = API_URL + url;
    let headers = {};
  
    if (isAuthReq) {
      headers = { headers: { Authorization: cookie.load('token') } };
    }
  
    axios.get(requestUrl, headers)
    .then((response) => {
      dispatch({
        type: action,
        payload: response.data,
      });
    })
    .catch((error) => {
      errorHandler(dispatch, error.response, errorType);
    });
  }
  
  // Put Request
  export function putData(action, errorType, isAuthReq, url, dispatch, data) {
    const requestUrl = API_URL + url;
    let headers = {};
  
    if (isAuthReq) {
      headers = { headers: { Authorization: cookie.load('token') } };
    }
  
    axios.put(requestUrl, data, headers)
    .then((response) => {
      dispatch({
        type: action,
        payload: response.data,
      });
    })
    .catch((error) => {
      errorHandler(dispatch, error.response, errorType);
    });
  }
  
  // Delete Request
  export function deleteData(action, errorType, isAuthReq, url, dispatch) {
    const requestUrl = API_URL + url;
    let headers = {};
  
    if (isAuthReq) {
      headers = { headers: { Authorization: cookie.load('token') } };
    }
  
    axios.delete(requestUrl, headers)
    .then((response) => {
      dispatch({
        type: action,
        payload: response.data,
      });
    })
    .catch((error) => {
      errorHandler(dispatch, error.response, errorType);
    });
  }
   