/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

import axios from "axios";
window.axios = axios;
export const axiosInstance = axios;

window.axios.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
window.axios.interceptors.request.use(
    function (config) {
        let list;
        list = document.querySelectorAll(".btn-spinner");
        for (let i = 0; i < list.length; ++i) {
            list[i].classList.add("spinner-on");
            list[i].disabled = true;
        }

        return config;
    },
    function (error) {
        return Promise.reject(error);
    }
);

window.axios.interceptors.response.use(
    function (response) {
        let list;
        list = document.querySelectorAll(".btn-spinner");
        for (let i = 0; i < list.length; ++i) {
            list[i].classList.remove("spinner-on");
            list[i].disabled = false;
        }
        return response;
    },
    function (error) {
        let list;
        list = document.querySelectorAll(".btn-spinner");
        for (let i = 0; i < list.length; ++i) {
            list[i].classList.remove("spinner-on");
            list[i].disabled = false;
        }
        return Promise.reject(error);
    }
);

