Files
planilla/ui/src/stores/useTareas.js
josedario87 918a53529c
Some checks failed
build-and-deploy / filter (push) Successful in 2s
Sync to GitHub / sync (push) Failing after 1s
build-and-deploy / build (push) Successful in 9s
build-and-deploy / deploy (push) Successful in 14s
mejorado manejo de estados de los stores
2025-05-30 11:04:12 -06:00

83 lines
2.7 KiB
JavaScript

import { defineStore } from 'pinia';
import apiClient from '../apiClient'; // Assuming apiClient is configured
// Helper function to get default values for currentTarea
const getDefaultCurrentTarea = () => ({
id: null,
empleado_id: null,
planilla_id: null, // Optional
titulo: '',
precio: null, // Optional
estado: 'pendiente', // Default from schema
observacion: '', // Optional
fecha: null, // Should be a date
tipo: '', // Default from schema
// fecha_anulado is not typically part of creation/edit form directly
});
export const useTareasStore = defineStore('tareas', {
state: () => ({
tareas: [],
currentTarea: getDefaultCurrentTarea(),
}),
actions: {
async fetchTareas() {
try {
const response = await apiClient.get('/api/tareas');
this.tareas = response.data;
} catch (error) {
console.error('Error fetching tareas:', error);
// Consider more sophisticated error handling (e.g., user notifications)
}
},
async fetchTareaById(id) {
try {
const response = await apiClient.get(`/api/tareas/${id}`);
this.currentTarea = response.data;
} catch (error) {
console.error(`Error fetching tarea with id ${id}:`, error);
this.currentTarea = getDefaultCurrentTarea(); // Reset on error
}
},
async createTarea(tareaData) {
try {
const { data } = await apiClient.post('/api/tareas', tareaData);
await this.fetchTareas(); // Refresh the list
return data; // Return created tarea data if needed
} catch (error) {
console.error('Error creating tarea:', error);
throw error; // Re-throw to allow form to handle it
}
},
async updateTarea(id, tareaData) {
try {
const { data } = await apiClient.put(`/api/tareas/${id}`, tareaData);
await this.fetchTareas(); // Refresh the list
this.currentTarea = getDefaultCurrentTarea(); // Reset currentTarea
return data; // Return updated tarea data if needed
} catch (error) {
console.error(`Error updating tarea with id ${id}:`, error);
throw error; // Re-throw to allow form to handle it
}
},
async deleteTarea(id) {
try {
await apiClient.delete(`/api/tareas/${id}`);
await this.fetchTareas(); // Refresh the list
} catch (error) {
console.error(`Error deleting tarea with id ${id}:`, error);
throw error; // Re-throw to allow handling in component
}
},
// Action to clear currentTarea, useful when navigating away from a form
clearCurrentTarea() {
this.currentTarea = getDefaultCurrentTarea();
}
},
});