Files
servicio_social_front/components/admin/programa/cargaMasiva.vue
T

144 lines
3.6 KiB
Vue
Raw Normal View History

2020-12-27 20:35:46 -06:00
<template>
<div>
<p class="is-size-2 mb-5">Carga Masiva</p>
<b-field v-if="true">
<b-upload
v-model="csv"
type="is-black"
v-on:input="validarExt()"
drag-drop
expanded
>
<section class="section">
<div class="content has-text-centered">
<p>
<b-icon icon="upload" size="is-large"></b-icon>
</p>
<p>
{{
csv.name || "Arrastra aquí tu archivo o da click para buscar"
}}
</p>
</div>
</section>
</b-upload>
</b-field>
2021-01-06 21:13:18 -06:00
<b-button v-if="csv.name" @click="dialog()" class="is-info"
2020-12-27 20:35:46 -06:00
>Enviar archivo</b-button
>
<b-button v-else disabled class="is-info">Enviar archivo</b-button>
2021-01-05 02:26:47 -06:00
<b-loading
:is-full-page="true"
v-model="isLoading"
:can-cancel="false"
></b-loading>
2020-12-27 20:35:46 -06:00
</div>
</template>
<script>
2021-01-06 15:27:23 -06:00
import axios from "axios";
2020-12-27 20:35:46 -06:00
export default {
2021-01-05 02:26:47 -06:00
data() {
return {
csv: {},
2021-01-06 00:10:12 -06:00
token: {
headers: {
token: window.localStorage.getItem("token"),
},
},
2021-01-05 02:26:47 -06:00
isLoading: false,
toastType: null,
toastMessage: null,
};
2020-12-27 20:35:46 -06:00
},
methods: {
validarExt() {
const extPermitidas = /(.csv)$/i;
if (!extPermitidas.exec(this.csv.name)) {
2021-01-06 00:10:12 -06:00
this.toastType = "is-danger";
this.toastMessage = "Asegurate de ingresar un archivo .CSV";
2021-01-05 02:26:47 -06:00
this.toast();
2020-12-27 20:35:46 -06:00
this.csv = {};
}
},
2021-01-05 02:26:47 -06:00
toast() {
2020-12-27 20:35:46 -06:00
this.$buefy.toast.open({
2021-01-05 02:26:47 -06:00
message: this.toastMessage,
type: this.toastType,
2020-12-27 20:35:46 -06:00
});
},
2021-01-05 02:26:47 -06:00
dialog() {
this.$buefy.dialog.confirm({
title: "Carga masiva",
message: "¿Seguro(a) que quiere enviar este archivo?",
confirmText: "Confirmar",
cancelText: "Cancelar",
type: "is-success",
hasIcon: true,
onConfirm: () => this.enviarCargaMasiva(),
});
},
2021-01-06 00:10:12 -06:00
enviarCargaMasiva() {
2021-01-05 02:26:47 -06:00
const formData = new FormData();
2021-01-06 21:13:18 -06:00
this.isLoading = true;
formData.append("csv", this.csv);
2021-01-05 02:26:47 -06:00
axios
2021-01-06 21:13:18 -06:00
.post(`${process.env.api}/programa/carga_masiva`, formData, {
2021-01-06 00:10:12 -06:00
headers: {
"Content-Type": "multipart/form-data",
token: this.token.headers.token,
},
})
2021-01-05 02:26:47 -06:00
.then((res) => {
this.isLoading = false;
2021-01-06 00:10:12 -06:00
this.toastType = "is-success";
this.toastMessage = "Se ha enviado la carga masiva con éxito";
2021-01-05 02:26:47 -06:00
this.toast();
2021-01-15 23:08:55 -06:00
this.$router.push("/admin");
2021-01-05 02:26:47 -06:00
})
.catch((err) => {
this.isLoading = false;
2021-01-06 00:10:12 -06:00
this.error(err.response.data.message);
2021-01-05 02:26:47 -06:00
});
},
2021-01-06 15:27:23 -06:00
error(msj) {
let salir = false;
switch (msj) {
case "invalid signature":
2021-01-06 16:33:56 -06:00
msj = "Tu token no es valido, inicia sesión de nuevo.";
2021-01-06 15:27:23 -06:00
salir = true;
break;
case "jwt expired":
2021-01-06 16:33:56 -06:00
msj = "Tu sesión ha expirado, inicia sesión de nuevo.";
2021-01-06 15:27:23 -06:00
salir = true;
break;
case "jwt malformed":
2021-01-06 17:43:26 -06:00
msj = "No se encontro tu token, inicia sesión de nuevo.";
2021-01-06 15:27:23 -06:00
salir = true;
break;
case "No hay token":
2021-01-06 16:33:56 -06:00
msj = "Ocurrio un error al enviar tu token, inicia sesión de nuevo.";
2021-01-06 15:27:23 -06:00
salir = true;
break;
}
2021-01-06 00:10:12 -06:00
this.$buefy.dialog.alert({
title: "Error",
message: msj,
type: "is-danger",
hasIcon: true,
icon: "alert-circle",
iconPack: "mdi",
ariaRole: "alertdialog",
ariaModal: true,
2021-01-06 15:27:23 -06:00
});
if (salir == true) {
localStorage.clear();
this.$router.push(`/`);
}
2021-01-06 00:10:12 -06:00
},
2021-01-05 02:26:47 -06:00
},
};
2020-12-27 20:35:46 -06:00
</script>