forked from RiseUP/riseup_squad_03
fix(principal): integra auth, agenda e laudos com a api
This commit is contained in:
43
src/App.jsx
43
src/App.jsx
@@ -1,5 +1,7 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react'
|
||||
|
||||
import { authRepository } from './repositories/authRepository.js'
|
||||
|
||||
import './App.css'
|
||||
import { AppShell } from './components/AppShell.jsx'
|
||||
import { AgendaPage } from './pages/AgendaPage.jsx'
|
||||
@@ -48,11 +50,16 @@ function App() {
|
||||
}, [])
|
||||
|
||||
const route = useMemo(() => resolveRoute(location.pathname, navigate), [location.pathname, navigate])
|
||||
const isAuthenticated = authRepository.isAuthenticated()
|
||||
|
||||
if (!route.withShell) {
|
||||
return route.element
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <LoginPage navigate={navigate} />
|
||||
}
|
||||
|
||||
return (
|
||||
<AppShell currentPath={location.pathname} navigate={navigate} routeTitle={route.title}>
|
||||
{route.element}
|
||||
@@ -119,15 +126,10 @@ function resolveRoute(pathname, navigate) {
|
||||
|
||||
if (pathname.startsWith('/pacientes/')) {
|
||||
const patientId = pathname.split('/')[2]
|
||||
const patient = patientRepository.getById(patientId)
|
||||
|
||||
return {
|
||||
element: patient ? (
|
||||
<PatientDetailPage navigate={navigate} patient={patient} />
|
||||
) : (
|
||||
<NotFoundPage navigate={navigate} />
|
||||
),
|
||||
title: patient?.name || 'Paciente nao encontrado',
|
||||
element: <PatientDetailRoute navigate={navigate} patientId={patientId} />,
|
||||
title: 'Paciente',
|
||||
withShell: true,
|
||||
}
|
||||
}
|
||||
@@ -195,6 +197,33 @@ function resolveRoute(pathname, navigate) {
|
||||
}
|
||||
}
|
||||
|
||||
function PatientDetailRoute({ navigate, patientId }) {
|
||||
const [patient, setPatient] = useState(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
useEffect(() => {
|
||||
let active = true
|
||||
|
||||
patientRepository.getById(patientId)
|
||||
.then((data) => {
|
||||
if (active) setPatient(data)
|
||||
})
|
||||
.finally(() => {
|
||||
if (active) setLoading(false)
|
||||
})
|
||||
|
||||
return () => {
|
||||
active = false
|
||||
}
|
||||
}, [patientId])
|
||||
|
||||
if (loading) {
|
||||
return <div className="pt-10 text-sm text-[#a3a3a3]">Carregando paciente...</div>
|
||||
}
|
||||
|
||||
return patient ? <PatientDetailPage navigate={navigate} patient={patient} /> : <NotFoundPage navigate={navigate} />
|
||||
}
|
||||
|
||||
function readLocation() {
|
||||
return {
|
||||
pathname: normalizePath(window.location.pathname),
|
||||
|
||||
Reference in New Issue
Block a user