35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
// frontend/src/stores/fabrications.ts
|
|
import { defineStore } from 'pinia';
|
|
import { ref } from 'vue';
|
|
import { fabricationsApi } from '@/api/fabrications';
|
|
export const useFabricationsStore = defineStore('fabrications', () => {
|
|
const fabrications = ref([]);
|
|
const loading = ref(false);
|
|
async function fetchAll(params) {
|
|
loading.value = true;
|
|
try {
|
|
fabrications.value = await fabricationsApi.list(params);
|
|
}
|
|
finally {
|
|
loading.value = false;
|
|
}
|
|
}
|
|
async function create(f) {
|
|
const created = await fabricationsApi.create(f);
|
|
fabrications.value.unshift(created);
|
|
return created;
|
|
}
|
|
async function updateStatut(id, statut) {
|
|
const updated = await fabricationsApi.updateStatut(id, statut);
|
|
const idx = fabrications.value.findIndex(f => f.id === id);
|
|
if (idx !== -1)
|
|
fabrications.value[idx] = updated;
|
|
return updated;
|
|
}
|
|
async function remove(id) {
|
|
await fabricationsApi.delete(id);
|
|
fabrications.value = fabrications.value.filter(f => f.id !== id);
|
|
}
|
|
return { fabrications, loading, fetchAll, create, updateStatut, remove };
|
|
});
|