export default {
    actions: {
        crudRequest({getters, dispatch},  {endpoint, method, parameters}){
            return dispatch('ensureReCaptchaIsSet').then(function () {
                return fetch(endpoint, {
                    method: method,
                    responseType: 'json',
                    body: parameters ? JSON.stringify(parameters):null,
                    headers: {
                        'content-type': 'application/json',
                        'Authorization': 'Bearer '+getters.getAccessToken,
                        'captcha-token': getters.getReCaptcha
                    }
                }).then(response => {

                    if(response.status === 401 && window.location.href !== route('login')){
                        dispatch('userAuthentication', {access_token: '', redirect_url: '/'});
                    }

                    return response;

                })
            });
        }
    }
}