text: import axios from 'axios'
  import history from '../history'
  
  /**
   * ACTION TYPES
   */
  const GET_USER = 'GET_USER'
  const REMOVE_USER = 'REMOVE_USER'
  const GET_LOGGED_IN_USER = 'GET_LOGGED_IN_USER'
  
  /**
   * INITIAL STATE
   */
  const defaultUser = {}
  
  /**
   * ACTION CREATORS
   */
  const getUser = user => ({type: GET_USER, user})
  const getLoggedInUser = user => ({type: GET_LOGGED_IN_USER, user})
  const removeUser = () => ({type: REMOVE_USER})
  
  /**
   * THUNK CREATORS
   */
  export const me = () => async dispatch => {
    try {
      const res = await axios.get('/auth/me')
      dispatch(getUser(res.data || defaultUser))
    } catch (err) {
      console.error(err)
    }
  }
  
  export const fetchLoggedInUser = userId => async dispatch => {
    try {
      const {data} = await axios.get(`/api/${userId}/orders`)
      dispatch(getLoggedInUser(data))
    } catch (err) {
      console.log(err)
    }
  }
  
  export const auth = (userInfo, method) => async dispatch => {
    let res
    try {
      const {firstName, lastName, email, password} = userInfo
      res = await axios.post(`/auth/${method}`, {
        email,
        password,
        firstName,
        lastName
      })
    } catch (authError) {
      return dispatch(getUser({error: authError}))
    }
  
    try {
      dispatch(getUser(res.data))
      history.push('/home')
    } catch (dispatchOrHistoryErr) {
      console.error(dispatchOrHistoryErr)
    }
  }
  
  export const logout = () => async dispatch => {
    try {
      await axios.post('/auth/logout')
      dispatch(removeUser())
      history.push('/login')
    } catch (err) {
      console.error(err)
    }
  }
  /**
   * REDUCER
   */
  export default function userReducer(state = defaultUser, action) {
    switch (action.type) {
      case GET_USER:
        return action.user
      case REMOVE_USER:
        return defaultUser
      case GET_LOGGED_IN_USER:
        return action.user
      default:
        return state
    }
  }
  