bugfix: patients feature toasts unified with the other features.
This commit is contained in:
@@ -2,8 +2,11 @@
|
|||||||
|
|
||||||
import { useEffect, useMemo, useState } from 'react';
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
import { Button } from '@/components/ui/common/Button';
|
import { Button } from '@/components/ui/common/Button';
|
||||||
|
import { ToastStack } from '@/components/ui/common/Toast';
|
||||||
import { patientsApi } from '@/lib/api/patients';
|
import { patientsApi } from '@/lib/api/patients';
|
||||||
|
import { formatApiErrorMessage } from '@/lib/formatApiError';
|
||||||
import { useAuth } from '@/lib/hooks/useAuth';
|
import { useAuth } from '@/lib/hooks/useAuth';
|
||||||
|
import { useToast } from '@/lib/hooks/useToast';
|
||||||
import { hasPermission } from '@/shared/permissions';
|
import { hasPermission } from '@/shared/permissions';
|
||||||
import {
|
import {
|
||||||
CreatePatientInput,
|
CreatePatientInput,
|
||||||
@@ -25,6 +28,7 @@ const EMPTY_PATIENT_FORM: CreatePatientInput = {
|
|||||||
|
|
||||||
export default function PatientsPage() {
|
export default function PatientsPage() {
|
||||||
const { currentOrganization } = useAuth();
|
const { currentOrganization } = useAuth();
|
||||||
|
const toast = useToast();
|
||||||
const [search, setSearch] = useState('');
|
const [search, setSearch] = useState('');
|
||||||
const [patients, setPatients] = useState<Patient[]>([]);
|
const [patients, setPatients] = useState<Patient[]>([]);
|
||||||
const [selectedPatient, setSelectedPatient] = useState<Patient | undefined>();
|
const [selectedPatient, setSelectedPatient] = useState<Patient | undefined>();
|
||||||
@@ -35,8 +39,6 @@ export default function PatientsPage() {
|
|||||||
const [savingPatient, setSavingPatient] = useState(false);
|
const [savingPatient, setSavingPatient] = useState(false);
|
||||||
const [savingTreatment, setSavingTreatment] = useState(false);
|
const [savingTreatment, setSavingTreatment] = useState(false);
|
||||||
const [patientForm, setPatientForm] = useState<CreatePatientInput>(EMPTY_PATIENT_FORM);
|
const [patientForm, setPatientForm] = useState<CreatePatientInput>(EMPTY_PATIENT_FORM);
|
||||||
const [errorMessage, setErrorMessage] = useState<string>('');
|
|
||||||
const [successMessage, setSuccessMessage] = useState<string>('');
|
|
||||||
const canEditPatients = hasPermission(currentOrganization, 'TAB_PATIENTS_EDIT');
|
const canEditPatients = hasPermission(currentOrganization, 'TAB_PATIENTS_EDIT');
|
||||||
|
|
||||||
const sortedPatients = useMemo(
|
const sortedPatients = useMemo(
|
||||||
@@ -58,21 +60,9 @@ export default function PatientsPage() {
|
|||||||
void loadPatients('');
|
void loadPatients('');
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!successMessage) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const timeout = setTimeout(() => {
|
|
||||||
setSuccessMessage('');
|
|
||||||
}, 3000);
|
|
||||||
|
|
||||||
return () => clearTimeout(timeout);
|
|
||||||
}, [successMessage]);
|
|
||||||
|
|
||||||
async function loadPatients(q: string) {
|
async function loadPatients(q: string) {
|
||||||
setLoadingPatients(true);
|
setLoadingPatients(true);
|
||||||
setErrorMessage('');
|
toast.setError('');
|
||||||
try {
|
try {
|
||||||
const response = await patientsApi.list({ q, page: 1, limit: 25 });
|
const response = await patientsApi.list({ q, page: 1, limit: 25 });
|
||||||
const items = response.data.items;
|
const items = response.data.items;
|
||||||
@@ -82,9 +72,8 @@ export default function PatientsPage() {
|
|||||||
const freshSelected = items.find((item) => item.id === selectedPatient.id);
|
const freshSelected = items.find((item) => item.id === selectedPatient.id);
|
||||||
setSelectedPatient(freshSelected);
|
setSelectedPatient(freshSelected);
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
const message = Array.isArray(error?.message) ? error.message.join(', ') : error?.message;
|
toast.showError(formatApiErrorMessage(error, 'Failed to load patients.'));
|
||||||
setErrorMessage(message || 'Failed to load patients.');
|
|
||||||
} finally {
|
} finally {
|
||||||
setLoadingPatients(false);
|
setLoadingPatients(false);
|
||||||
}
|
}
|
||||||
@@ -92,13 +81,12 @@ export default function PatientsPage() {
|
|||||||
|
|
||||||
async function loadTreatments(patientId: string) {
|
async function loadTreatments(patientId: string) {
|
||||||
setLoadingTreatments(true);
|
setLoadingTreatments(true);
|
||||||
setErrorMessage('');
|
toast.setError('');
|
||||||
try {
|
try {
|
||||||
const response = await patientsApi.listTreatments(patientId);
|
const response = await patientsApi.listTreatments(patientId);
|
||||||
setTreatments(response.data);
|
setTreatments(response.data);
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
const message = Array.isArray(error?.message) ? error.message.join(', ') : error?.message;
|
toast.showError(formatApiErrorMessage(error, 'Failed to load treatment history.'));
|
||||||
setErrorMessage(message || 'Failed to load treatment history.');
|
|
||||||
} finally {
|
} finally {
|
||||||
setLoadingTreatments(false);
|
setLoadingTreatments(false);
|
||||||
}
|
}
|
||||||
@@ -106,8 +94,7 @@ export default function PatientsPage() {
|
|||||||
|
|
||||||
async function handleCreatePatient() {
|
async function handleCreatePatient() {
|
||||||
setSavingPatient(true);
|
setSavingPatient(true);
|
||||||
setErrorMessage('');
|
toast.setError('');
|
||||||
setSuccessMessage('');
|
|
||||||
try {
|
try {
|
||||||
const response = await patientsApi.create(patientForm);
|
const response = await patientsApi.create(patientForm);
|
||||||
setIsCreateOpen(false);
|
setIsCreateOpen(false);
|
||||||
@@ -115,12 +102,11 @@ export default function PatientsPage() {
|
|||||||
await loadPatients(search);
|
await loadPatients(search);
|
||||||
setSelectedPatient(response.data);
|
setSelectedPatient(response.data);
|
||||||
await loadTreatments(response.data.id);
|
await loadTreatments(response.data.id);
|
||||||
setSuccessMessage(
|
toast.showSuccess(
|
||||||
`Patient ${response.data.firstName} ${response.data.lastName} was saved successfully.`,
|
`Patient ${response.data.firstName} ${response.data.lastName} was saved successfully.`,
|
||||||
);
|
);
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
const message = Array.isArray(error?.message) ? error.message.join(', ') : error?.message;
|
toast.showError(formatApiErrorMessage(error, 'Failed to save patient.'));
|
||||||
setErrorMessage(message || 'Failed to save patient.');
|
|
||||||
} finally {
|
} finally {
|
||||||
setSavingPatient(false);
|
setSavingPatient(false);
|
||||||
}
|
}
|
||||||
@@ -139,29 +125,28 @@ export default function PatientsPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
setSavingTreatment(true);
|
setSavingTreatment(true);
|
||||||
setErrorMessage('');
|
toast.setError('');
|
||||||
setSuccessMessage('');
|
|
||||||
try {
|
try {
|
||||||
await patientsApi.addTreatment(selectedPatient.id, payload);
|
await patientsApi.addTreatment(selectedPatient.id, payload);
|
||||||
await loadTreatments(selectedPatient.id);
|
await loadTreatments(selectedPatient.id);
|
||||||
setSuccessMessage('Treatment entry added successfully.');
|
toast.showSuccess('Treatment entry added successfully.');
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
const message = Array.isArray(error?.message) ? error.message.join(', ') : error?.message;
|
toast.showError(formatApiErrorMessage(error, 'Failed to add treatment entry.'));
|
||||||
setErrorMessage(message || 'Failed to add treatment entry.');
|
|
||||||
} finally {
|
} finally {
|
||||||
setSavingTreatment(false);
|
setSavingTreatment(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="relative space-y-6 pb-20">
|
<div className="space-y-6">
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between gap-3">
|
||||||
<h1 className="text-2xl font-semibold text-text-primary">Patients</h1>
|
<h1 className="text-2xl font-semibold text-text-primary">Patients</h1>
|
||||||
<Button
|
<Button
|
||||||
variant="primary"
|
variant="primary"
|
||||||
disabled={!canEditPatients}
|
disabled={!canEditPatients}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
if (!canEditPatients) return;
|
if (!canEditPatients) return;
|
||||||
|
toast.clear();
|
||||||
setIsCreateOpen(true);
|
setIsCreateOpen(true);
|
||||||
}}
|
}}
|
||||||
title={!canEditPatients ? 'Read-only access for this organization.' : undefined}
|
title={!canEditPatients ? 'Read-only access for this organization.' : undefined}
|
||||||
@@ -170,14 +155,18 @@ export default function PatientsPage() {
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<CreatePatientModal
|
<ToastStack {...toast.messages} />
|
||||||
isOpen={isCreateOpen}
|
|
||||||
formData={patientForm}
|
{isCreateOpen && (
|
||||||
onChange={(patch) => setPatientForm((prev) => ({ ...prev, ...patch }))}
|
<CreatePatientModal
|
||||||
onSubmit={handleCreatePatient}
|
isOpen={isCreateOpen}
|
||||||
onClose={() => setIsCreateOpen(false)}
|
formData={patientForm}
|
||||||
loading={savingPatient}
|
onChange={(patch) => setPatientForm((prev) => ({ ...prev, ...patch }))}
|
||||||
/>
|
onSubmit={() => void handleCreatePatient()}
|
||||||
|
onClose={() => setIsCreateOpen(false)}
|
||||||
|
loading={savingPatient}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className="grid grid-cols-1 xl:grid-cols-3 gap-6">
|
<div className="grid grid-cols-1 xl:grid-cols-3 gap-6">
|
||||||
<div className="xl:col-span-1">
|
<div className="xl:col-span-1">
|
||||||
@@ -213,21 +202,6 @@ export default function PatientsPage() {
|
|||||||
<TreatmentHistoryPreview items={treatments} loading={loadingTreatments} />
|
<TreatmentHistoryPreview items={treatments} loading={loadingTreatments} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{(errorMessage || successMessage) && (
|
|
||||||
<div className="absolute bottom-0 left-0 right-0 z-10 w-full">
|
|
||||||
{errorMessage && (
|
|
||||||
<div className="rounded-[var(--radius-sm)] border border-red-500/50 bg-red-500/10 px-3 py-2 text-sm text-red-300 shadow-lg">
|
|
||||||
{errorMessage}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{successMessage && (
|
|
||||||
<div className="rounded-[var(--radius-sm)] border border-emerald-500/50 bg-emerald-500/10 px-3 py-2 text-sm text-emerald-300 shadow-lg">
|
|
||||||
{successMessage}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user