import axios from "axios";
import { authKeyUrl, authKeyData } from "./IlinkConstants";
import { fetchAuthKeySuccess, fetchAuthKeyPending, fetchAuthKeyError } from "../redux/actions/AuthKeyActions";

const getAuthApiKey = (phone) => {

     const data = authKeyData;
     authKeyData.username = phone;
     authKeyData.password = "";

     return dispatch => {
          dispatch(fetchAuthKeyPending());
          axios.post(authKeyUrl, authKeyData)
               .then(response => {
                    dispatch(fetchAuthKeySuccess(response));
               })
               .catch(error => {
                    if (error.response)
                         dispatch(fetchAuthKeyError(error.response));
                    else if (error.request)
                         dispatch(fetchAuthKeyError(error.request))
                    else
                         dispatch(fetchAuthKeyError(error.message))
               });
     }
}

export default getAuthApiKey;