text: import axios from 'axios';
  import { SubmissionError } from 'redux-form'
  
  //Get current user(me) from token in localStorage
  export const ME_FROM_TOKEN = 'ME_FROM_TOKEN';
  export const ME_FROM_TOKEN_SUCCESS = 'ME_FROM_TOKEN_SUCCESS';
  export const ME_FROM_TOKEN_FAILURE = 'ME_FROM_TOKEN_FAILURE';
  export const RESET_TOKEN = 'RESET_TOKEN';
  
  //Sign Up User
  export const SIGNUP_USER = 'SIGNUP_USER';
  export const SIGNUP_USER_SUCCESS = 'SIGNUP_USER_SUCCESS';
  export const SIGNUP_USER_FAILURE = 'SIGNUP_USER_FAILURE';
  export const RESET_USER = 'RESET_USER';
  
  //Sign In User
  export const SIGNIN_USER = 'SIGNIN_USER';
  export const SIGNIN_USER_SUCCESS = 'SIGNIN_USER_SUCCESS';
  export const SIGNIN_USER_FAILURE = 'SIGNIN_USER_FAILURE';
  
  //log out user
  export const LOGOUT_USER = 'LOGOUT_USER';
  //const ROOT_URL = 'http://localhost:5000/api';
  const ROOT_URL = "https://sleepy-tor-25771.herokuapp.com/api";
  
  
  export const validateAndSignInUser = (values, dispatch) => {
    return dispatch(signInUser(values))
      .then((result) => {
        if (result.payload.response && result.payload.response.status !== 200) {
          dispatch(signInUserFailure(result.payload.response.data));
          throw new SubmissionError(result.payload.response.data);
        }
  
        localStorage.setItem('token', result.payload.data.token);
        localStorage.setItem('id', result.payload.data.user._id);
        dispatch(signInUserSuccess(result.payload.data)); 
      });
  };
  
   export const loadUserFromToken = (dispatch) => {
      let id = localStorage.getItem('id');
      let token = localStorage.getItem('token')
      if(!id || id === '') {//if there is no token, dont bother
        return;
      }
  
      //fetch user from token (if server deems it's valid token)
      return dispatch(meFromId(id, token))
        .then((response) => {
          if (!response.error) {
            //reset token (possibly new token that was regenerated by the server)
            localStorage.setItem('token', response.payload.data.token);
            dispatch(meFromIdSuccess(response.payload))
          } else {
            localStorage.removeItem('token');//remove token from storage
            dispatch(meFromIdFailure(response.payload));
          }
        });
  };
  
  export function meFromId(userId, token) {
    //check if the token is still valid, if so, get me from the server
  
    const request = axios({
      method: 'get',
      url: `${ROOT_URL}/users/${userId}`,
      headers: {
        'Authorization': `${token}`
      }
    });
  
    return {
      type: ME_FROM_TOKEN,
      payload: request
    };
  }
  
  export function meFromIdSuccess(currentUser) {
    return {
      type: ME_FROM_TOKEN_SUCCESS,
      payload: currentUser
    };
  }
  
  export function meFromIdFailure(error) {
    return {
      type: ME_FROM_TOKEN_FAILURE,
      payload: error
    };
  }
  
  
  export function meFromToken(tokenFromStorage) {
    //check if the token is still valid, if so, get me from the server
  
    const request = axios({
      method: 'get',
      url: `${ROOT_URL}/me/from/token?token=${tokenFromStorage}`,
    });
  
    return {
      type: ME_FROM_TOKEN,
      payload: request
    };
  }
  
  export function meFromTokenSuccess(currentUser) {
    return {
      type: ME_FROM_TOKEN_SUCCESS,
      payload: currentUser
    };
  }
  
  export function meFromTokenFailure(error) {
    return {
      type: ME_FROM_TOKEN_FAILURE,
      payload: error
    };
  }
  
  
  export function resetToken() {//used for logout
    return {
      type: RESET_TOKEN
    };
  }
  
  
  export function signUpUser(formValues) {
    const request = axios.post(`${ROOT_URL}/auth/register`, formValues);
  
    return {
      type: SIGNUP_USER,
      payload: request
    };
  }
  
  export function signUpUserSuccess(user) {
    return {
      type: SIGNUP_USER_SUCCESS,
      payload: user
    };
  }
  
  export function signUpUserFailure(error) {
    return {
      type: SIGNUP_USER_FAILURE,
      payload: error
    };
  }
  
  
  export function resetUser() {
    return {
      type: RESET_USER,
    };
  }
  
  export function signInUser(formValues) {
    const request = axios.post(`${ROOT_URL}/auth/login`, formValues);
  
    return {
      type: SIGNIN_USER,
      payload: request
    };
  }
  
  export function signInUserSuccess(user) {
    return {
      type: SIGNIN_USER_SUCCESS,
      payload: user
    };
  }
  
  export function signInUserFailure(error) {
    return {
      type: SIGNIN_USER_FAILURE,
      payload: error
    };
  }
  
  export function logoutUser() {
    return {
      type: LOGOUT_USER
    };
  }
  
  