import { actionPromise, actionFullLogin } from '.';
import gql from '../api';

export const actionRegister = (login, password) =>
  actionPromise('register', gql(`mutation register($user:UserInput){
    UserUpsert(user:$user){
      _id login
    }
  }`, {
    user: {
      login: `${login}`,
      password: `${password}`
    }
  }
  )
  );

export const actionAuthLogout = () => ({ type: 'AUTH_LOGOUT' });

export const actionFullRegister = (login, password) =>
  async dispatch => {
    await dispatch(actionAuthLogout());
    const reg = await dispatch(actionRegister(login, password));
    if (reg) {
      dispatch(actionFullLogin(login, password));
    }
  };
