
import { creditCancelDemand } from "./IlinkConstants";
import { store } from "../redux/store";
import axios from "axios";
import I18n from 'react-native-i18n'
import { fetchTreatCreditCancelPending, fetchTreatCreditCancelSucsess, fetchTreatCreditCancelError, fetchTreatCreditCancelReset } from "../redux/actions/CreditCancelDemandAction";

export const treatCancelDemand = (idDemand) => {

     const auth = store.getState().authKeyReducer;
     const authKey = auth !== null ? `${auth.authKey.token_type} ${auth.authKey.access_token}` : '';

     return dispatch => {
          dispatch(fetchTreatCreditCancelPending());

          axios({
               url: `${creditCancelDemand}/${idDemand}`,
               method: 'PUT',
               headers: {
                    'Authorization': authKey,
                    'X-Localization': I18n.currentLocale()
               }
          })
               .then(response => {
                    console.log(response);
                    dispatch(fetchTreatCreditCancelSucsess(response));
               })
               .catch(error => {
                    if (error.response)
                         dispatch(fetchTreatCreditCancelError(error.response));
                    else if (error.request)
                         dispatch(fetchTreatCreditCancelError(error.request))
                    else
                         dispatch(fetchTreatCreditCancelError(error.message))
               });
     }
}

export const creditCancelResetReducer = () => {
     return dispatch => {
          dispatch(fetchTreatCreditCancelReset());
     }
}