import fetch from 'isomorphic-fetch';

import { loginActionCreators } from '../../routes/login/modules/index';

import { url } from '../../../utils/url';

const UPDATE_USER = 'UPDATE_USER';

const LOAD_USER_REQUEST = 'LOAD_USER_REQUEST';
const LOAD_USER_SUCCESS = 'LOAD_USER_SUCCESS';
const LOAD_USER_FAIL = 'LOAD_USER_FAIL';

function loadUserRequest() {
  return {
    type: LOAD_USER_REQUEST
  };
}

function loadUserSuccess() {
  return {
    type: LOAD_USER_SUCCESS
  };
}

function loadUserFail(payload) {
  return {
    type: LOAD_USER_FAIL,
    payload
  };
}

export function updateUser(payload) {
  return {
    type: UPDATE_USER,
    payload
  };
}

export function getUser() {
  return dispatch => {
    dispatch(loadUserRequest())

    return fetch(url + `api/v1/users/`, 
      { method: 'GET',
        credentials: 'include'
      })
      .then(response => {
        if (response.status === 401) {
          dispatch(loginActionCreators.logoutUser());
        } else {
          return response.json();
        }
      })
      .then(json => {
        const jsonData = json.data;

        if (jsonData.uiError || jsonData.error) {
          dispatch(loadUserFail(jsonData))
        } else {
          dispatch(loadUserSuccess())
          dispatch(updateUser(jsonData));
        }
      }
    );
  };
}

const initialState = {
  errorMessage: '',
  fullName: '',
  userId: '',

  isFetchingUserSuccessful: false,
  isFetchingUser: false
}

export default function user(state = initialState, action) {
  switch (action.type) {
    case LOAD_USER_REQUEST:
      return Object.assign({}, state, {
        isFetching: true,
      });
    case LOAD_USER_SUCCESS:
      return Object.assign({}, state, {
        isFetchingUserSuccessful: true,
        isFetchingUser: false
      });
    case LOAD_USER_FAIL:
      return Object.assign({}, state, {
        isFetchingUserSuccessful: false,
        isFetchingUser: false,

        errorMessage: action.payload.error
      });
    case UPDATE_USER:
      return Object.assign({}, state, {
        userId: action.payload.user._id,
        fullName: action.payload.user.fullname,
      });
    default: return state;
  }
}
@@ -802,16 +802,93 @@
  'GET',%0A
+        headers: %7B%0A          'csrf': localStorage.getItem('csrf')%0A        %7D,%0A
         

Save token in local storage