ldc-boutique-cnc/resources/ts/components/TimeslotExceptionsComponent.vue
2023-02-15 15:56:05 +01:00

209 lines
5.5 KiB
Vue

<template>
<div>
<div
class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"
>
<h1 class="h2">
{{ __("Jours Exception") }} -
<small class="text-muted">{{ __("Fermetures exceptionnelles") }}</small>
</h1>
<transition name="fade">
<span
class="float-right text-success"
v-show="processing"
>{{
response
}}</span>
</transition>
<button
class="btn btn-primary btn-sm text-bg-primary float-right"
@click.prevent="saveData()"
:disabled="processing"
>
{{ __("Sauvegarder les exceptions") }}
<div
class="spinner-border spinner-border-sm text-light"
role="status"
v-show="processing"
>
<span class="sr-only">{{ __("Chargement") }}...</span>
</div>
</button>
</div>
<table class="table table-bordered text-center">
<thead>
<tr>
<th scope="col">
{{ __("Jour") }}
</th>
<th scope="col" />
</tr>
</thead>
<tbody>
<tr
v-for="(item, index) in datas"
:key="index"
>
<td>
<input
class="form-control form-control-sm"
v-model="item.date"
type="date"
required
>
</td>
<td>
<button
@click.prevent="removeItem(index)"
class="btn btn-danger btn-sm"
>
{{ __("Supprimer") }}
</button>
</td>
</tr>
</tbody>
</table>
<div class="form-group text-right">
<button
class="btn btn-primary btn-sm text-bg-primary"
@click.prevent="newItem"
>
{{ __("Ajouter une date") }}
</button>
</div>
</div>
</template>
<script lang="ts">
import trans from "../modules/trans";
import route from "../modules/route";
import { defineComponent } from "vue";
export default defineComponent({
mixins: [trans, route],
mounted() {
if (window.vueDebug) {
console.log("CreneauxExceptions Component mounted.");
}
const json = String(this.$attrs.json ?? "{}"),
data = JSON.parse(json);
this.shopId = data.shopId;
this.datas = data.dbdatas;
},
data(): {
datas: LaravelModelList;
processing: boolean;
response: string | null;
shopId: number;
} {
return {
datas: [],
response: null,
processing: false,
shopId: 0,
};
},
watch: {},
methods: {
newItem() {
this.datas.push({ date: "", shop_id: this.shopId });
},
removeItem(index: number) {
if (this.datas[index]["id"] !== undefined) {
this.processing = true;
this.response = this.__("En de suppression...");
const deleteRoute = this.route(
"bo.shops.collection_exceptions.delete",
{
ID: this.shopId,
}
);
if (!deleteRoute) {
throw new Error(
"undefined route bo.shops.collection_exceptions.delete"
);
}
window.axios
.post(deleteRoute, {
id: this.datas[index]["id"],
})
.then((response) => {
this.datas = response.data;
this.response = this.__("Supression terminée !");
this.processing = false;
})
.catch((error) => {
if (window.vueDebug) {
console.log(error);
}
this.response = this.__("Erreur lors de la suppression.");
this.processing = false;
});
this.datas.splice(index, 1);
} else {
this.datas.splice(index, 1);
}
},
saveData() {
this.processing = true;
this.response = this.__("En cours de sauvegarde...");
const saveRoute = this.route("bo.shops.collection_exceptions.save", {
ID: this.shopId,
});
if (!saveRoute) {
throw new Error("undefined route bo.shops.collection_exceptions.save");
}
window.axios
.post(saveRoute, {
dates: this.datas,
})
.then((response) => {
this.datas = response.data;
this.response = this.__("Sauvegarde terminée !");
this.processing = false;
})
.catch((error) => {
this.processing = false;
let message = this.__(
"Une erreur est survenue merci de ressayer plus tard"
);
if (error.response.status === 422) {
const errors = Array.from(
Object.values(error.response?.data?.errors)
) as Array<Array<string> | string>;
message = errors
.map((error) => {
return typeof error === "string"
? error
: Array.from(error).join("\n");
})
.join("\n");
this.response = message;
return;
}
if (error.response) {
console.error(error.response.data);
} else if (error.request) {
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.error("Error", error.message);
this.response = this.__("Erreur lors de la sauvegarde.");
}
});
},
},
});
</script>
<style lang="scss">
.fade-enter-active,
.fade-leave-active {
transition: opacity 6.5s;
}
.fade-enter,
.fade-leave-to {
opacity: 0;
}
</style>