mejorado manejo de estados de los stores
This commit is contained in:
@@ -48,8 +48,9 @@ export const useAsistenciasStore = defineStore('asistencias', {
|
|||||||
// If asistenciaData.entrada/salida are Date objects, convert them:
|
// If asistenciaData.entrada/salida are Date objects, convert them:
|
||||||
// if (asistenciaData.entrada instanceof Date) asistenciaData.entrada = asistenciaData.entrada.toISOString();
|
// if (asistenciaData.entrada instanceof Date) asistenciaData.entrada = asistenciaData.entrada.toISOString();
|
||||||
// if (asistenciaData.salida instanceof Date) asistenciaData.salida = asistenciaData.salida.toISOString();
|
// if (asistenciaData.salida instanceof Date) asistenciaData.salida = asistenciaData.salida.toISOString();
|
||||||
await apiClient.post('/api/asistencias', asistenciaData);
|
const {data} = await apiClient.post('/api/asistencias', asistenciaData);
|
||||||
await this.fetchAsistencias(); // Refresh the list
|
await this.fetchAsistencias(); // Refresh the list
|
||||||
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error creating asistencia:', error);
|
console.error('Error creating asistencia:', error);
|
||||||
throw error; // Re-throw to allow form to handle it
|
throw error; // Re-throw to allow form to handle it
|
||||||
@@ -61,9 +62,10 @@ export const useAsistenciasStore = defineStore('asistencias', {
|
|||||||
// Similar date conversion logic as in createAsistencia if needed
|
// Similar date conversion logic as in createAsistencia if needed
|
||||||
// if (asistenciaData.entrada instanceof Date) asistenciaData.entrada = asistenciaData.entrada.toISOString();
|
// if (asistenciaData.entrada instanceof Date) asistenciaData.entrada = asistenciaData.entrada.toISOString();
|
||||||
// if (asistenciaData.salida instanceof Date) asistenciaData.salida = asistenciaData.salida.toISOString();
|
// if (asistenciaData.salida instanceof Date) asistenciaData.salida = asistenciaData.salida.toISOString();
|
||||||
await apiClient.put(`/api/asistencias/${id}`, asistenciaData);
|
const {data} = await apiClient.put(`/api/asistencias/${id}`, asistenciaData);
|
||||||
await this.fetchAsistencias(); // Refresh the list
|
await this.fetchAsistencias(); // Refresh the list
|
||||||
this.currentAsistencia = getDefaultCurrentAsistencia(); // Reset currentAsistencia
|
this.currentAsistencia = getDefaultCurrentAsistencia(); // Reset currentAsistencia
|
||||||
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error updating asistencia with id ${id}:`, error);
|
console.error(`Error updating asistencia with id ${id}:`, error);
|
||||||
throw error; // Re-throw to allow form to handle it
|
throw error; // Re-throw to allow form to handle it
|
||||||
|
|||||||
@@ -1,81 +1,79 @@
|
|||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia'
|
||||||
import apiClient from '../apiClient'; // Assuming apiClient is configured for API calls
|
import apiClient from '../apiClient'
|
||||||
|
|
||||||
|
// 🎛 helper para valores por defecto
|
||||||
|
const getDefaultCurrentPlanilla = () => ({
|
||||||
|
id: null,
|
||||||
|
fecha_desde: null,
|
||||||
|
fecha_hasta: null,
|
||||||
|
titulo: '',
|
||||||
|
total: null,
|
||||||
|
estado: 'pagado',
|
||||||
|
fecha_anulado: null,
|
||||||
|
empleado_id: null,
|
||||||
|
})
|
||||||
|
|
||||||
export const usePlanillasStore = defineStore('planillas', {
|
export const usePlanillasStore = defineStore('planillas', {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
planillas: [],
|
planillas: [],
|
||||||
currentPlanilla: {
|
currentPlanilla: getDefaultCurrentPlanilla(),
|
||||||
id: null,
|
|
||||||
fecha_desde: null,
|
|
||||||
fecha_hasta: null,
|
|
||||||
titulo: '',
|
|
||||||
total: null,
|
|
||||||
estado: 'pagado', // Default value from schema
|
|
||||||
fecha_anulado: null,
|
|
||||||
empleado_id: null,
|
|
||||||
// Empleado relation is not included here, will be handled by ID.
|
|
||||||
},
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
async fetchPlanillas() {
|
async fetchPlanillas() {
|
||||||
try {
|
try {
|
||||||
const response = await apiClient.get('/api/planillas');
|
const { data } = await apiClient.get('/api/planillas')
|
||||||
this.planillas = response.data;
|
this.planillas = data
|
||||||
} catch (error) {
|
} catch (e) {
|
||||||
console.error('Error fetching planillas:', error);
|
console.error('Error fetching planillas:', e)
|
||||||
// Handle error (e.g., show a notification to the user)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async fetchPlanillaById(id) {
|
async fetchPlanillaById(id) {
|
||||||
try {
|
try {
|
||||||
const response = await apiClient.get(`/api/planillas/${id}`);
|
const { data } = await apiClient.get(`/api/planillas/${id}`)
|
||||||
this.currentPlanilla = response.data;
|
this.currentPlanilla = data
|
||||||
} catch (error) {
|
} catch (e) {
|
||||||
console.error(`Error fetching planilla with id ${id}:`, error);
|
console.error(`Error fetching planilla ${id}:`, e)
|
||||||
// Handle error
|
this.currentPlanilla = getDefaultCurrentPlanilla()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async createPlanilla(planillaData) {
|
async createPlanilla(planillaData) {
|
||||||
try {
|
try {
|
||||||
await apiClient.post('/api/planillas', planillaData);
|
const { data } = await apiClient.post('/api/planillas', planillaData)
|
||||||
await this.fetchPlanillas(); // Refresh the list
|
await this.fetchPlanillas()
|
||||||
} catch (error) {
|
return data
|
||||||
console.error('Error creating planilla:', error);
|
} catch (e) {
|
||||||
// Handle error
|
console.error('Error creating planilla:', e)
|
||||||
|
throw e
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async updatePlanilla(id, planillaData) {
|
async updatePlanilla(id, planillaData) {
|
||||||
try {
|
try {
|
||||||
await apiClient.put(`/api/planillas/${id}`, planillaData);
|
const { data } = await apiClient.put(`/api/planillas/${id}`, planillaData)
|
||||||
await this.fetchPlanillas(); // Refresh the list
|
await this.fetchPlanillas()
|
||||||
this.currentPlanilla = { // Reset currentPlanilla
|
this.clearCurrentPlanilla()
|
||||||
id: null,
|
return data
|
||||||
fecha_desde: null,
|
} catch (e) {
|
||||||
fecha_hasta: null,
|
console.error(`Error updating planilla ${id}:`, e)
|
||||||
titulo: '',
|
throw e
|
||||||
total: null,
|
|
||||||
estado: 'pagado',
|
|
||||||
fecha_anulado: null,
|
|
||||||
empleado_id: null,
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Error updating planilla with id ${id}:`, error);
|
|
||||||
// Handle error
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async deletePlanilla(id) {
|
async deletePlanilla(id) {
|
||||||
try {
|
try {
|
||||||
await apiClient.delete(`/api/planillas/${id}`);
|
await apiClient.delete(`/api/planillas/${id}`)
|
||||||
await this.fetchPlanillas(); // Refresh the list
|
await this.fetchPlanillas()
|
||||||
} catch (error)
|
} catch (e) {
|
||||||
{
|
console.error(`Error deleting planilla ${id}:`, e)
|
||||||
console.error(`Error deleting planilla with id ${id}:`, error);
|
throw e
|
||||||
// Handle error
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
clearCurrentPlanilla() {
|
||||||
|
this.currentPlanilla = getDefaultCurrentPlanilla()
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
})
|
||||||
|
|||||||
@@ -43,8 +43,9 @@ export const useTareasStore = defineStore('tareas', {
|
|||||||
|
|
||||||
async createTarea(tareaData) {
|
async createTarea(tareaData) {
|
||||||
try {
|
try {
|
||||||
await apiClient.post('/api/tareas', tareaData);
|
const { data } = await apiClient.post('/api/tareas', tareaData);
|
||||||
await this.fetchTareas(); // Refresh the list
|
await this.fetchTareas(); // Refresh the list
|
||||||
|
return data; // Return created tarea data if needed
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error creating tarea:', error);
|
console.error('Error creating tarea:', error);
|
||||||
throw error; // Re-throw to allow form to handle it
|
throw error; // Re-throw to allow form to handle it
|
||||||
@@ -53,9 +54,10 @@ export const useTareasStore = defineStore('tareas', {
|
|||||||
|
|
||||||
async updateTarea(id, tareaData) {
|
async updateTarea(id, tareaData) {
|
||||||
try {
|
try {
|
||||||
await apiClient.put(`/api/tareas/${id}`, tareaData);
|
const { data } = await apiClient.put(`/api/tareas/${id}`, tareaData);
|
||||||
await this.fetchTareas(); // Refresh the list
|
await this.fetchTareas(); // Refresh the list
|
||||||
this.currentTarea = getDefaultCurrentTarea(); // Reset currentTarea
|
this.currentTarea = getDefaultCurrentTarea(); // Reset currentTarea
|
||||||
|
return data; // Return updated tarea data if needed
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error updating tarea with id ${id}:`, error);
|
console.error(`Error updating tarea with id ${id}:`, error);
|
||||||
throw error; // Re-throw to allow form to handle it
|
throw error; // Re-throw to allow form to handle it
|
||||||
|
|||||||
Reference in New Issue
Block a user