import * as actionTypes from '../actionTypes/actionTypes';
import 'whatwg-fetch';

/** ACTIONS ***
Actions define the action type, and do something based on the value of 'type' (i.e. receiveUserData)
Action creators will send information to the 'store', by going through reducers, which will change
 state based on the action type. Action creators (i.e. getUserData) are executable in containers
that use Redux's 'connect' helper. 'bindActionCreators()' binds action creators to dispatch */

export const requestUserData = (data) => {
  return {
    type: actionTypes.GET_USER,
    data,
  }
}


/**
  * Receive data from fetch
**/
export const receiveUserData = (data) => {
  return {
    type: actionTypes.GET_USER_SUCCESS,
    results: data,
  }
}

/**
  * Fetch current user's information
**/
export const getUserData = () => {
  console.log('INSIDE GET USER DATA FUNCTION');
  return (dispatch) => {
    dispatch(requestUserData)

    return fetch('/get_info/', {
      method: 'get',
      mode: 'no-cors',
    }).then(res => res.json())
    .then(json => dispatch(receiveUserData(json)))
    .catch(err => dispatch({
      type: actionTypes.GET_USER_FAILURE,
      results: err,
    }))

  }
}
@@ -485,16 +485,52 @@
 tch */%0A%0A
+/** %0A  *Default request handler%0A**/%0A
 export c
@@ -624,17 +624,16 @@
 %0A  %7D%0A%7D%0A%0A
-%0A
 /**%0A  * 
@@ -772,32 +772,179 @@
 s: data,%0A  %7D%0A%7D%0A%0A
+/**%0A  *Error handling for failed fetch%0A**/%0Aexport const failedFetch = (err) =%3E %7B%0A  return %7B%0A    type: actionTypes.GET_USER_FAILURE,%0A    err%0A  %7D%0A%7D%0A%0A
 /**%0A  * Fetch cu
@@ -1169,31 +1169,8 @@
 t',%0A
-      mode: 'no-cors',%0A
     
@@ -1278,82 +1278,29 @@
 tch(
-%7B%0A      type: actionTypes.GET_USER_FAILURE,%0A      results: err,%0A    %7D
+failedFetch(err)
 ))%0A
-%0A
   %7D%0A
-
 %7D
+%0A

Resolve bug with CORS, get user data