import { actionPromise } from ".";
import gql from "../api";
import { actionFullLogin } from ".";

export const actionRegister = (login, password) =>                  //const actionRegister //actionPromise
  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) =>   //const actionFullRegister = (login, password) => //actionRegister + actionFullLogin
  async dispatch => {      
    await dispatch(actionAuthLogout());                      //+ интерфейс к этому - форму логина, регистрации, может повесить это на #/login #/register 
    let reg = await dispatch(actionRegister(login, password)); 
    if(reg){
      dispatch(actionFullLogin(login, password))
    }                                                    //+ #/orders показывает ваши бывшие заказы:
  }