From c9ed9b37f7e40e2cd5b9a19833605508b0439da9 Mon Sep 17 00:00:00 2001 From: Sergio Nicolas Lavaise Date: Fri, 24 Apr 2026 02:41:05 -0300 Subject: [PATCH] =?UTF-8?q?Feat:=20avanzar=20actualizaci=C3=B3n=20del=20st?= =?UTF-8?q?ore=20para=20MariaDB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- src/hooks/useHospitalStore-mariadb.ts | 191 ++++++++++++++------------ 1 file changed, 106 insertions(+), 85 deletions(-) diff --git a/src/hooks/useHospitalStore-mariadb.ts b/src/hooks/useHospitalStore-mariadb.ts index f168d42..d4e4232 100644 --- a/src/hooks/useHospitalStore-mariadb.ts +++ b/src/hooks/useHospitalStore-mariadb.ts @@ -337,7 +337,7 @@ export function useHospitalStore() { } }, [state.areas, apiCall]); - const actualizarInternacion = useCallback((internacionId: string, datos: Partial) => { + const actualizarInternacion = useCallback(async (internacionId: string, datos: Partial) => { 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; - }); - - // Marcar como cambiadas para el guardado - markChanged('internaciones'); - if (cambioDeCama) { - markChanged('camas'); + try { + // API call inmediata + await apiCall('PUT', `/internaciones/${internacionId}`, datos); + + // 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) => { + const agregarEvolucion = useCallback(async (evolucion: Omit) => { 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]); + + 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((id: string, datos: Partial) => { + const actualizarEvolucion = useCallback(async (id: string, datos: Partial) => { 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) => { + const agregarLaboratorio = useCallback(async (laboratorio: Omit) => { 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) => { + const actualizarLaboratorio = useCallback(async (id: string, datos: Partial) => { 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) => {