feature: a minimal v1 backend implemented for treatments feature.
This commit is contained in:
@@ -8,16 +8,10 @@ import { formatApiErrorMessage } from '@/components/shared/formatApiError';
|
||||
import { useAuth } from '@/lib/hooks/useAuth';
|
||||
import { useToast } from '@/lib/hooks/useToast';
|
||||
import { hasPermission } from '@/components/shared/permissions';
|
||||
import {
|
||||
CreatePatientInput,
|
||||
CreateTreatmentHistoryInput,
|
||||
Patient,
|
||||
TreatmentHistoryItem,
|
||||
} from '@/types/patient';
|
||||
import { CreatePatientInput, Patient } from '@/types/patient';
|
||||
import { PatientSearchSelect } from '../../../components/ui/patient/PatientSearchSelect';
|
||||
import { CreatePatientModal } from '../../../components/ui/patient/CreatePatientModal';
|
||||
import { PatientSummaryCard } from '../../../components/ui/patient/PatientSummaryCard';
|
||||
import { TreatmentHistoryPreview } from '../../../components/ui/patient/TreatmentHistoryPreview';
|
||||
|
||||
const EMPTY_PATIENT_FORM: CreatePatientInput = {
|
||||
firstName: '',
|
||||
@@ -32,12 +26,9 @@ export default function PatientsPage() {
|
||||
const [search, setSearch] = useState('');
|
||||
const [patients, setPatients] = useState<Patient[]>([]);
|
||||
const [selectedPatient, setSelectedPatient] = useState<Patient | undefined>();
|
||||
const [treatments, setTreatments] = useState<TreatmentHistoryItem[]>([]);
|
||||
const [loadingPatients, setLoadingPatients] = useState(false);
|
||||
const [loadingTreatments, setLoadingTreatments] = useState(false);
|
||||
const [isCreateOpen, setIsCreateOpen] = useState(false);
|
||||
const [savingPatient, setSavingPatient] = useState(false);
|
||||
const [savingTreatment, setSavingTreatment] = useState(false);
|
||||
const [patientForm, setPatientForm] = useState<CreatePatientInput>(EMPTY_PATIENT_FORM);
|
||||
const canEditPatients = hasPermission(currentOrganization, 'TAB_PATIENTS_EDIT');
|
||||
|
||||
@@ -79,19 +70,6 @@ export default function PatientsPage() {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadTreatments(patientId: string) {
|
||||
setLoadingTreatments(true);
|
||||
toast.setError('');
|
||||
try {
|
||||
const response = await patientsApi.listTreatments(patientId);
|
||||
setTreatments(response.data);
|
||||
} catch (error: unknown) {
|
||||
toast.showError(formatApiErrorMessage(error, 'Failed to load treatment history.'));
|
||||
} finally {
|
||||
setLoadingTreatments(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCreatePatient() {
|
||||
setSavingPatient(true);
|
||||
toast.setError('');
|
||||
@@ -101,7 +79,6 @@ export default function PatientsPage() {
|
||||
setPatientForm(EMPTY_PATIENT_FORM);
|
||||
await loadPatients(search);
|
||||
setSelectedPatient(response.data);
|
||||
await loadTreatments(response.data.id);
|
||||
toast.showSuccess(
|
||||
`Patient ${response.data.firstName} ${response.data.lastName} was saved successfully.`,
|
||||
);
|
||||
@@ -112,31 +89,6 @@ export default function PatientsPage() {
|
||||
}
|
||||
}
|
||||
|
||||
async function handleQuickAddTreatment() {
|
||||
if (!selectedPatient) {
|
||||
return;
|
||||
}
|
||||
|
||||
const payload: CreateTreatmentHistoryInput = {
|
||||
title: 'Initial consultation',
|
||||
status: 'scheduled',
|
||||
treatmentAt: new Date().toISOString(),
|
||||
notes: 'Created from quick action on patients page.',
|
||||
};
|
||||
|
||||
setSavingTreatment(true);
|
||||
toast.setError('');
|
||||
try {
|
||||
await patientsApi.addTreatment(selectedPatient.id, payload);
|
||||
await loadTreatments(selectedPatient.id);
|
||||
toast.showSuccess('Treatment entry added successfully.');
|
||||
} catch (error: unknown) {
|
||||
toast.showError(formatApiErrorMessage(error, 'Failed to add treatment entry.'));
|
||||
} finally {
|
||||
setSavingTreatment(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
@@ -179,31 +131,13 @@ export default function PatientsPage() {
|
||||
onSearchChange={setSearch}
|
||||
patients={sortedPatients}
|
||||
selectedPatientId={selectedPatient?.id}
|
||||
onSelectPatient={(patient) => {
|
||||
setSelectedPatient(patient);
|
||||
void loadTreatments(patient.id);
|
||||
}}
|
||||
onSelectPatient={setSelectedPatient}
|
||||
loading={loadingPatients}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="xl:col-span-2 space-y-4">
|
||||
<PatientSummaryCard patient={selectedPatient} />
|
||||
<div className="flex">
|
||||
<Button
|
||||
variant="secondary"
|
||||
disabled={!selectedPatient || !canEditPatients}
|
||||
isLoading={savingTreatment}
|
||||
onClick={() => {
|
||||
if (!canEditPatients) return;
|
||||
void handleQuickAddTreatment();
|
||||
}}
|
||||
title={!canEditPatients ? 'Read-only access for this organization.' : undefined}
|
||||
>
|
||||
Add Quick Treatment Entry
|
||||
</Button>
|
||||
</div>
|
||||
<TreatmentHistoryPreview items={treatments} loading={loadingTreatments} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user