Files
exchange-2.0/resources/assets/vue/general/mixins/request.js
T

71 lines
3.1 KiB
JavaScript
Vendored

export default {
methods: {
submit(url, method, section, successNotification = true, errorNotification = true){
if(!this.validate()){ return; }
if (section) {
this.$store.dispatch('toggleLoading', {name: section, status: true})
}
this.$store.dispatch('crudRequest', {
endpoint: url,
method: method,
parameters: method === 'get' ? null : this.parameters
}).then(response => {
let statusCode = response.status,
success = response.ok;
if (response.headers.get("content-type") === "application/zip") {
const fileName = response.headers.get('Content-Disposition').split('filename=')[1].replaceAll('"', '');
response.blob().then(response => {
if (!success) {
this.openModal();
errorNotification ? this.$store.dispatch('createNotification', { title: response.title, message: response.message, type: 'error' }) : null;
this.errorHandler(response, statusCode); return;
}
successNotification ? this.$store.dispatch('createNotification', { title: response.title, message: response.message, type: 'success' }) : null;
const link = document.createElement('a');
link.href = window.URL.createObjectURL(response);
link.download = fileName.trim();
link.click();
this.successHandler(response)
});
} else {
response.json().then(response => {
if (!success) {
this.openModal();
errorNotification ? this.$store.dispatch('createNotification', { title: response.title, message: response.message, type: 'error' }) : null;
this.errorHandler(response, statusCode); return;
}
successNotification ? this.$store.dispatch('createNotification', { title: response.title, message: response.message, type: 'success' }) : null;
this.successHandler(response)
});
}
}).catch((error) => {
console.log(error);
this.$store.dispatch('createNotification', {title: 'Unexpected Error', message: 'An unexpected error has occurred. Try again!', type: 'error'});
}).then(() => {
this.$root.execute();
if (section) {
this.$store.dispatch('toggleLoading', {name: section, status: false})
}
})
},
validate() {
if(this.$v){
this.$v.$touch();
return !this.$v.$invalid;
}
return true;
},
successHandler(response){},
errorHandler(response){}
},
}