import fetch from 'isomorphic-fetch'

export function signUpUser(register, history) {
  return(dispatch) => {

    const options = {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        user: {
          first_name: register.name,
          email: register.email,
          password: register.password,
          password_confirmation: register.password,
        }
      }),
    };

    return fetch('api/v1/register', options)
      .then(response => response.json())
      .then(response => {
        localStorage.setItem('jwt', response.jwt);
        localStorage.setItem(
          'user',
          JSON.stringify({
            id: response.id, email: response.email, name: response.name,
          })
        );
        history.push("/");

        dispatch({
          type: 'SIGN_UP_SUCCESS',
          payload: response,
        });
      });
  }
}

export function signInUser(credentials, history, redirect) {
  return(dispatch) => {
    const options = {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        auth: {
          email: credentials.email,
          password: credentials.password,
        }
      }),
    };

    return fetch('api/v1/signin', options)
      .then(response => response.json())
      .then(response => {
        localStorage.setItem('jwt', response.jwt);
        localStorage.setItem(
          'user',
          JSON.stringify({
            id: response.id, email: response.email, name: response.name,
          })
        );

        dispatch({
          type: 'SIGN_IN_SUCCESS',
          payload: response,
        })
      }).then(() => history.push(redirect));
  }
}

export function signOutUser() {
  localStorage.removeItem('jwt');
  localStorage.removeItem('user');

  return(dispatch) => {
    dispatch({ type: 'CLEAR_CURRENT_ROUTINE' });
    dispatch({ type: 'SIGN_OUT' });
  }
}

export function loadUserTraits() {
  return {
    type: 'LOAD_USER_TRAITS',
  }
}

@@ -1986,46 +1986,129 @@
 %7B%0A  
-return %7B%0A    type: 'LOAD_USER_TRAITS',
+if (localStorage.user) %7B%0A    return %7B type: 'LOAD_USER_TRAITS' %7D;%0A  %7D else %7B%0A    return %7B type: 'NO_USER_TRAITS_FOUND' %7D;
 %0A  %7D

Handle missing user traits in localStorage