Feat: avanzar actualización del store para MariaDB
- Actualizar laboratorios: agregar, eliminar, actualizar con API inmediata - Actualizar acidos base: agregar, eliminar, actualizar con API inmediata - Funciones actualizadas: iniciarInternacion, finalizarInternacion, actualizarInternacion - Funciones actualizadas: evoluciones CRUD - Pendiente: cultivos, estudios, interconsultas, ATB, indicaciones, pacientes, camas, areas
This commit is contained in:
@@ -337,7 +337,7 @@ export function useHospitalStore() {
|
||||
}
|
||||
}, [state.areas, apiCall]);
|
||||
|
||||
const actualizarInternacion = useCallback((internacionId: string, datos: Partial<Internacion>) => {
|
||||
const actualizarInternacion = useCallback(async (internacionId: string, datos: Partial<Internacion>) => {
|
||||
const internacion = state.internaciones.find(i => i.id === internacionId);
|
||||
if (!internacion) return;
|
||||
|
||||
@@ -357,53 +357,39 @@ export function useHospitalStore() {
|
||||
|
||||
const pacienteId = datos.pacienteId || internacion.pacienteId;
|
||||
|
||||
setState(prev => {
|
||||
const result = {
|
||||
...prev,
|
||||
internaciones: prev.internaciones.map(i =>
|
||||
i.id === internacionId
|
||||
? { ...i, ...datos }
|
||||
: i
|
||||
),
|
||||
camas: prev.camas.map(c => {
|
||||
if (c.id === newCamaId && newCamaId !== oldCamaId) {
|
||||
return { ...c, estado: 'Ocupada' as const, pacienteId, internacionId };
|
||||
}
|
||||
if (c.id === oldCamaId && oldCamaId !== newCamaId) {
|
||||
return { ...c, estado: 'Disponible' as const, pacienteId: undefined, internacionId: undefined };
|
||||
}
|
||||
return c;
|
||||
}),
|
||||
};
|
||||
return result;
|
||||
});
|
||||
try {
|
||||
// API call inmediata
|
||||
await apiCall('PUT', `/internaciones/${internacionId}`, datos);
|
||||
|
||||
// Marcar como cambiadas para el guardado
|
||||
markChanged('internaciones');
|
||||
if (cambioDeCama) {
|
||||
markChanged('camas');
|
||||
// Actualizar estado local después del éxito
|
||||
setState(prev => {
|
||||
const result = {
|
||||
...prev,
|
||||
internaciones: prev.internaciones.map(i =>
|
||||
i.id === internacionId
|
||||
? { ...i, ...datos }
|
||||
: i
|
||||
),
|
||||
camas: prev.camas.map(c => {
|
||||
if (c.id === newCamaId && newCamaId !== oldCamaId) {
|
||||
return { ...c, estado: 'Ocupada' as const, pacienteId, internacionId };
|
||||
}
|
||||
if (c.id === oldCamaId && oldCamaId !== newCamaId) {
|
||||
return { ...c, estado: 'Disponible' as const, pacienteId: undefined, internacionId: undefined };
|
||||
}
|
||||
return c;
|
||||
}),
|
||||
};
|
||||
return result;
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Error al actualizar internación:', err);
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (oldCamaId !== newCamaId && newCamaId && oldCamaId) {
|
||||
setTimeout(() => {
|
||||
fetch(`${API_BASE}/camas/${newCamaId}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ estado: 'Ocupada' }),
|
||||
}).catch(() => {});
|
||||
}, 500);
|
||||
setTimeout(() => {
|
||||
fetch(`${API_BASE}/camas/${oldCamaId}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ estado: 'Disponible' }),
|
||||
}).catch(() => {});
|
||||
}, 1000);
|
||||
}
|
||||
}, [state.internaciones, state.currentUser]);
|
||||
}, [state.internaciones, state.currentUser, apiCall]);
|
||||
|
||||
// Acciones de evoluciones
|
||||
const agregarEvolucion = useCallback((evolucion: Omit<Evolucion, 'id'>) => {
|
||||
const agregarEvolucion = useCallback(async (evolucion: Omit<Evolucion, 'id'>) => {
|
||||
const internacion = state.internaciones.find(i => i.id === evolucion.internacionId);
|
||||
const areaId = internacion?.areaId || null;
|
||||
if (!canAccessArea(areaId)) {
|
||||
@@ -414,16 +400,21 @@ export function useHospitalStore() {
|
||||
...evolucion,
|
||||
id: generateUUID(),
|
||||
};
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
evoluciones: [...prev.evoluciones, nuevaEvolucion],
|
||||
}));
|
||||
|
||||
markChanged('evoluciones');
|
||||
return nuevaEvolucion.id;
|
||||
}, [state.internaciones, state.currentUser, markChanged]);
|
||||
try {
|
||||
await apiCall('POST', '/evoluciones', nuevaEvolucion);
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
evoluciones: [...prev.evoluciones, nuevaEvolucion],
|
||||
}));
|
||||
return nuevaEvolucion.id;
|
||||
} catch (err) {
|
||||
console.error('Error al agregar evolución:', err);
|
||||
throw err;
|
||||
}
|
||||
}, [state.internaciones, state.currentUser, apiCall]);
|
||||
|
||||
const eliminarEvolucion = useCallback((id: string) => {
|
||||
const eliminarEvolucion = useCallback(async (id: string) => {
|
||||
const evolucion = state.evoluciones.find(e => e.id === id);
|
||||
if (!evolucion) return;
|
||||
const internacion = state.internaciones.find(i => i.id === evolucion.internacionId);
|
||||
@@ -432,14 +423,20 @@ export function useHospitalStore() {
|
||||
console.warn('No tiene permisos para eliminar evolución en esta área');
|
||||
return;
|
||||
}
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
evoluciones: prev.evoluciones.filter(e => e.id !== id),
|
||||
}));
|
||||
markChanged('evoluciones');
|
||||
}, [markChanged]);
|
||||
|
||||
const actualizarEvolucion = useCallback((id: string, datos: Partial<Evolucion>) => {
|
||||
try {
|
||||
await apiCall('DELETE', `/evoluciones/${id}`);
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
evoluciones: prev.evoluciones.filter(e => e.id !== id),
|
||||
}));
|
||||
} catch (err) {
|
||||
console.error('Error al eliminar evolución:', err);
|
||||
throw err;
|
||||
}
|
||||
}, [state.internaciones, state.currentUser, apiCall]);
|
||||
|
||||
const actualizarEvolucion = useCallback(async (id: string, datos: Partial<Evolucion>) => {
|
||||
const evolucion = state.evoluciones.find(e => e.id === id);
|
||||
if (!evolucion) return;
|
||||
const internacion = state.internaciones.find(i => i.id === evolucion.internacionId);
|
||||
@@ -448,15 +445,21 @@ export function useHospitalStore() {
|
||||
console.warn('No tiene permisos para actualizar evolución en esta área');
|
||||
return;
|
||||
}
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
evoluciones: prev.evoluciones.map(e => e.id === id ? { ...e, ...datos } : e),
|
||||
}));
|
||||
markChanged('evoluciones');
|
||||
}, [state.internaciones, state.currentUser, markChanged]);
|
||||
|
||||
try {
|
||||
await apiCall('PUT', `/evoluciones/${id}`, datos);
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
evoluciones: prev.evoluciones.map(e => e.id === id ? { ...e, ...datos } : e),
|
||||
}));
|
||||
} catch (err) {
|
||||
console.error('Error al actualizar evolución:', err);
|
||||
throw err;
|
||||
}
|
||||
}, [state.internaciones, state.currentUser, apiCall]);
|
||||
|
||||
// Acciones de laboratorios
|
||||
const agregarLaboratorio = useCallback((laboratorio: Omit<Laboratorio, 'id'>) => {
|
||||
const agregarLaboratorio = useCallback(async (laboratorio: Omit<Laboratorio, 'id'>) => {
|
||||
const internacion = state.internaciones.find(i => i.id === laboratorio.internacionId);
|
||||
const areaId = internacion?.areaId || null;
|
||||
if (!canAccessArea(areaId)) {
|
||||
@@ -467,14 +470,20 @@ export function useHospitalStore() {
|
||||
...laboratorio,
|
||||
id: generateUUID(),
|
||||
};
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
laboratorios: [...prev.laboratorios, nuevoLaboratorio],
|
||||
}));
|
||||
return nuevoLaboratorio.id;
|
||||
}, [state.internaciones, state.currentUser]);
|
||||
try {
|
||||
await apiCall('POST', '/laboratorios', nuevoLaboratorio);
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
laboratorios: [...prev.laboratorios, nuevoLaboratorio],
|
||||
}));
|
||||
return nuevoLaboratorio.id;
|
||||
} catch (err) {
|
||||
console.error('Error al agregar laboratorio:', err);
|
||||
throw err;
|
||||
}
|
||||
}, [state.internaciones, state.currentUser, apiCall]);
|
||||
|
||||
const eliminarLaboratorio = useCallback((id: string) => {
|
||||
const eliminarLaboratorio = useCallback(async (id: string) => {
|
||||
const laboratorio = state.laboratorios.find(l => l.id === id);
|
||||
if (!laboratorio) return;
|
||||
const internacion = state.internaciones.find(i => i.id === laboratorio.internacionId);
|
||||
@@ -483,13 +492,19 @@ export function useHospitalStore() {
|
||||
console.warn('No tiene permisos para eliminar laboratorio en esta área');
|
||||
return;
|
||||
}
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
laboratorios: prev.laboratorios.filter(l => l.id !== id),
|
||||
}));
|
||||
}, [state.internaciones, state.currentUser]);
|
||||
try {
|
||||
await apiCall('DELETE', `/laboratorios/${id}`);
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
laboratorios: prev.laboratorios.filter(l => l.id !== id),
|
||||
}));
|
||||
} catch (err) {
|
||||
console.error('Error al eliminar laboratorio:', err);
|
||||
throw err;
|
||||
}
|
||||
}, [state.internaciones, state.currentUser, apiCall]);
|
||||
|
||||
const actualizarLaboratorio = useCallback((id: string, datos: Partial<Laboratorio>) => {
|
||||
const actualizarLaboratorio = useCallback(async (id: string, datos: Partial<Laboratorio>) => {
|
||||
const laboratorio = state.laboratorios.find(l => l.id === id);
|
||||
if (!laboratorio) return;
|
||||
const internacion = state.internaciones.find(i => i.id === laboratorio.internacionId);
|
||||
@@ -498,11 +513,17 @@ export function useHospitalStore() {
|
||||
console.warn('No tiene permisos para actualizar laboratorio en esta área');
|
||||
return;
|
||||
}
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
laboratorios: prev.laboratorios.map(l => l.id === id ? { ...l, ...datos } : l),
|
||||
}));
|
||||
}, [state.internaciones, state.currentUser]);
|
||||
try {
|
||||
await apiCall('PUT', `/laboratorios/${id}`, datos);
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
laboratorios: prev.laboratorios.map(l => l.id === id ? { ...l, ...datos } : l),
|
||||
}));
|
||||
} catch (err) {
|
||||
console.error('Error al actualizar laboratorio:', err);
|
||||
throw err;
|
||||
}
|
||||
}, [state.internaciones, state.currentUser, apiCall]);
|
||||
|
||||
// Acciones de ácido-base
|
||||
const agregarAcidoBase = useCallback((acidoBase: Omit<AcidoBase, 'id'>) => {
|
||||
|
||||
Reference in New Issue
Block a user