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]);
|
}, [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);
|
const internacion = state.internaciones.find(i => i.id === internacionId);
|
||||||
if (!internacion) return;
|
if (!internacion) return;
|
||||||
|
|
||||||
@@ -357,53 +357,39 @@ export function useHospitalStore() {
|
|||||||
|
|
||||||
const pacienteId = datos.pacienteId || internacion.pacienteId;
|
const pacienteId = datos.pacienteId || internacion.pacienteId;
|
||||||
|
|
||||||
setState(prev => {
|
try {
|
||||||
const result = {
|
// API call inmediata
|
||||||
...prev,
|
await apiCall('PUT', `/internaciones/${internacionId}`, datos);
|
||||||
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;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Marcar como cambiadas para el guardado
|
// Actualizar estado local después del éxito
|
||||||
markChanged('internaciones');
|
setState(prev => {
|
||||||
if (cambioDeCama) {
|
const result = {
|
||||||
markChanged('camas');
|
...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;
|
||||||
}
|
}
|
||||||
|
}, [state.internaciones, state.currentUser, apiCall]);
|
||||||
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]);
|
|
||||||
|
|
||||||
// Acciones de evoluciones
|
// 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 internacion = state.internaciones.find(i => i.id === evolucion.internacionId);
|
||||||
const areaId = internacion?.areaId || null;
|
const areaId = internacion?.areaId || null;
|
||||||
if (!canAccessArea(areaId)) {
|
if (!canAccessArea(areaId)) {
|
||||||
@@ -414,16 +400,21 @@ export function useHospitalStore() {
|
|||||||
...evolucion,
|
...evolucion,
|
||||||
id: generateUUID(),
|
id: generateUUID(),
|
||||||
};
|
};
|
||||||
setState(prev => ({
|
|
||||||
...prev,
|
|
||||||
evoluciones: [...prev.evoluciones, nuevaEvolucion],
|
|
||||||
}));
|
|
||||||
|
|
||||||
markChanged('evoluciones');
|
try {
|
||||||
return nuevaEvolucion.id;
|
await apiCall('POST', '/evoluciones', nuevaEvolucion);
|
||||||
}, [state.internaciones, state.currentUser, markChanged]);
|
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);
|
const evolucion = state.evoluciones.find(e => e.id === id);
|
||||||
if (!evolucion) return;
|
if (!evolucion) return;
|
||||||
const internacion = state.internaciones.find(i => i.id === evolucion.internacionId);
|
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');
|
console.warn('No tiene permisos para eliminar evolución en esta área');
|
||||||
return;
|
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);
|
const evolucion = state.evoluciones.find(e => e.id === id);
|
||||||
if (!evolucion) return;
|
if (!evolucion) return;
|
||||||
const internacion = state.internaciones.find(i => i.id === evolucion.internacionId);
|
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');
|
console.warn('No tiene permisos para actualizar evolución en esta área');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setState(prev => ({
|
|
||||||
...prev,
|
try {
|
||||||
evoluciones: prev.evoluciones.map(e => e.id === id ? { ...e, ...datos } : e),
|
await apiCall('PUT', `/evoluciones/${id}`, datos);
|
||||||
}));
|
setState(prev => ({
|
||||||
markChanged('evoluciones');
|
...prev,
|
||||||
}, [state.internaciones, state.currentUser, markChanged]);
|
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
|
// 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 internacion = state.internaciones.find(i => i.id === laboratorio.internacionId);
|
||||||
const areaId = internacion?.areaId || null;
|
const areaId = internacion?.areaId || null;
|
||||||
if (!canAccessArea(areaId)) {
|
if (!canAccessArea(areaId)) {
|
||||||
@@ -467,14 +470,20 @@ export function useHospitalStore() {
|
|||||||
...laboratorio,
|
...laboratorio,
|
||||||
id: generateUUID(),
|
id: generateUUID(),
|
||||||
};
|
};
|
||||||
setState(prev => ({
|
try {
|
||||||
...prev,
|
await apiCall('POST', '/laboratorios', nuevoLaboratorio);
|
||||||
laboratorios: [...prev.laboratorios, nuevoLaboratorio],
|
setState(prev => ({
|
||||||
}));
|
...prev,
|
||||||
return nuevoLaboratorio.id;
|
laboratorios: [...prev.laboratorios, nuevoLaboratorio],
|
||||||
}, [state.internaciones, state.currentUser]);
|
}));
|
||||||
|
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);
|
const laboratorio = state.laboratorios.find(l => l.id === id);
|
||||||
if (!laboratorio) return;
|
if (!laboratorio) return;
|
||||||
const internacion = state.internaciones.find(i => i.id === laboratorio.internacionId);
|
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');
|
console.warn('No tiene permisos para eliminar laboratorio en esta área');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setState(prev => ({
|
try {
|
||||||
...prev,
|
await apiCall('DELETE', `/laboratorios/${id}`);
|
||||||
laboratorios: prev.laboratorios.filter(l => l.id !== id),
|
setState(prev => ({
|
||||||
}));
|
...prev,
|
||||||
}, [state.internaciones, state.currentUser]);
|
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);
|
const laboratorio = state.laboratorios.find(l => l.id === id);
|
||||||
if (!laboratorio) return;
|
if (!laboratorio) return;
|
||||||
const internacion = state.internaciones.find(i => i.id === laboratorio.internacionId);
|
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');
|
console.warn('No tiene permisos para actualizar laboratorio en esta área');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setState(prev => ({
|
try {
|
||||||
...prev,
|
await apiCall('PUT', `/laboratorios/${id}`, datos);
|
||||||
laboratorios: prev.laboratorios.map(l => l.id === id ? { ...l, ...datos } : l),
|
setState(prev => ({
|
||||||
}));
|
...prev,
|
||||||
}, [state.internaciones, state.currentUser]);
|
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
|
// Acciones de ácido-base
|
||||||
const agregarAcidoBase = useCallback((acidoBase: Omit<AcidoBase, 'id'>) => {
|
const agregarAcidoBase = useCallback((acidoBase: Omit<AcidoBase, 'id'>) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user