Fix internacion lookup, persistence and fallback patient resolution
This commit is contained in:
+44
-8
@@ -131,12 +131,30 @@ function AppContent() {
|
||||
/>
|
||||
);
|
||||
case 'historiaclinica': {
|
||||
const internacionId = store.currentInternacionId || '';
|
||||
const internacionId = store.currentInternacionId || sessionStorage.getItem('hospital_current_internacion_id') || '';
|
||||
const internacion = store.getInternacionById(internacionId);
|
||||
const paciente = internacion ? store.getPacienteById(internacion.pacienteId) : undefined;
|
||||
if (!internacion || !paciente) {
|
||||
const paciente = internacion
|
||||
? (store.getPacienteById(internacion.pacienteId) || store.pacientes.find(p => p.id === internacion.pacienteId || p.dni === internacion.pacienteId) || {
|
||||
id: internacion.pacienteId || 'paciente-unknown',
|
||||
apellido: 'Paciente',
|
||||
nombre: 'Sin registrar',
|
||||
dni: internacion.pacienteId || 'N/A',
|
||||
fechaNacimiento: '1990-01-01',
|
||||
sexo: 'Otro' as const,
|
||||
fechaRegistro: new Date().toISOString()
|
||||
})
|
||||
: undefined;
|
||||
|
||||
if (!internacion) {
|
||||
if (!store.isLoaded) {
|
||||
return (
|
||||
<div className="p-8 text-center text-gray-500">
|
||||
<p>Cargando información de la internación...</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="p-4 text-center">
|
||||
<div className="p-4 text-center space-y-4">
|
||||
<p className="text-gray-500 dark:text-gray-400">Internación no encontrada (id: {internacionId})</p>
|
||||
<Button onClick={() => store.setVista('internaciones')}>Volver a Internaciones</Button>
|
||||
</div>
|
||||
@@ -215,12 +233,30 @@ function AppContent() {
|
||||
);
|
||||
|
||||
case 'editaringreso': {
|
||||
const internacionId = store.currentInternacionId || '';
|
||||
const internacionId = store.currentInternacionId || sessionStorage.getItem('hospital_current_internacion_id') || '';
|
||||
const internacion = store.getInternacionById(internacionId);
|
||||
const paciente = internacion ? store.getPacienteById(internacion.pacienteId) : undefined;
|
||||
if (!internacion || !paciente) {
|
||||
const paciente = internacion
|
||||
? (store.getPacienteById(internacion.pacienteId) || store.pacientes.find(p => p.id === internacion.pacienteId || p.dni === internacion.pacienteId) || {
|
||||
id: internacion.pacienteId || 'paciente-unknown',
|
||||
apellido: 'Paciente',
|
||||
nombre: 'Sin registrar',
|
||||
dni: internacion.pacienteId || 'N/A',
|
||||
fechaNacimiento: '1990-01-01',
|
||||
sexo: 'Otro' as const,
|
||||
fechaRegistro: new Date().toISOString()
|
||||
})
|
||||
: undefined;
|
||||
|
||||
if (!internacion) {
|
||||
if (!store.isLoaded) {
|
||||
return (
|
||||
<div className="p-8 text-center text-gray-500">
|
||||
<p>Cargando información de la internación...</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="p-4 text-center">
|
||||
<div className="p-4 text-center space-y-4">
|
||||
<p className="text-gray-500 dark:text-gray-400">Internación no encontrada (id: {internacionId})</p>
|
||||
<Button onClick={() => store.setVista('internaciones')}>Volver a Internaciones</Button>
|
||||
</div>
|
||||
|
||||
@@ -94,6 +94,7 @@ export function useHospitalStore() {
|
||||
if (mounted && body) {
|
||||
const defaults = defaultState();
|
||||
const storedUser = sessionStorage.getItem('hospital_user');
|
||||
const storedInternacionId = sessionStorage.getItem('hospital_current_internacion_id');
|
||||
const currentUser = storedUser ? JSON.parse(storedUser) : null;
|
||||
const normalized = {
|
||||
...defaults,
|
||||
@@ -103,6 +104,7 @@ export function useHospitalStore() {
|
||||
atb: body.atb || [],
|
||||
glucemias: body.glucemias || [],
|
||||
movimientosIndicaciones: body.movimientosIndicaciones || [],
|
||||
currentInternacionId: storedInternacionId || body.currentInternacionId || null,
|
||||
currentUser,
|
||||
isAuthenticated: !!currentUser,
|
||||
};
|
||||
@@ -1237,15 +1239,24 @@ const actualizarInternacion = useCallback(async (internacionId: string, datos: P
|
||||
|
||||
// Funciones de utilidad para consultas
|
||||
const getPacienteById = useCallback((id: string) => {
|
||||
return state.pacientes.find(p => p.id === id);
|
||||
if (!id) return undefined;
|
||||
const target = String(id).trim().toLowerCase();
|
||||
return state.pacientes.find(p =>
|
||||
(p.id && String(p.id).trim().toLowerCase() === target) ||
|
||||
(p.dni && String(p.dni).trim().toLowerCase() === target)
|
||||
);
|
||||
}, [state.pacientes]);
|
||||
|
||||
const getCamaById = useCallback((id: string) => {
|
||||
return state.camas.find(c => c.id === id);
|
||||
if (!id) return undefined;
|
||||
const target = String(id).trim().toLowerCase();
|
||||
return state.camas.find(c => c.id && String(c.id).trim().toLowerCase() === target);
|
||||
}, [state.camas]);
|
||||
|
||||
const getInternacionById = useCallback((id: string) => {
|
||||
return state.internaciones.find(i => i.id === id);
|
||||
if (!id) return undefined;
|
||||
const target = String(id).trim().toLowerCase();
|
||||
return state.internaciones.find(i => i.id && String(i.id).trim().toLowerCase() === target);
|
||||
}, [state.internaciones]);
|
||||
|
||||
const getInternacionActivaByPaciente = useCallback((pacienteId: string) => {
|
||||
@@ -1309,6 +1320,11 @@ const actualizarInternacion = useCallback(async (internacionId: string, datos: P
|
||||
|
||||
// Current internacion selection (for HC page)
|
||||
const setCurrentInternacion = useCallback((id?: string | null) => {
|
||||
if (id) {
|
||||
sessionStorage.setItem('hospital_current_internacion_id', id);
|
||||
} else {
|
||||
sessionStorage.removeItem('hospital_current_internacion_id');
|
||||
}
|
||||
setState(prev => ({ ...prev, currentInternacionId: id ?? null }));
|
||||
}, []);
|
||||
|
||||
@@ -1434,5 +1450,6 @@ const actualizarInternacion = useCallback(async (internacionId: string, datos: P
|
||||
getCamaGrupoId,
|
||||
getInternacionGrupoId,
|
||||
getPacienteGrupoId,
|
||||
isLoaded,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user