import { useState } from "react"; import { isSaveRetired } from "../../../utils/saveOperation"; import type { RegistrationList, RegistrationListEntry, RegistrationReceipt } from "../../../services/registrationReceipt"; // The desk's own registrations, completed and unfinished, from the server: the one way back to a registration whose // answer was lost. Check asks the server what it stored; it never sends the form again. interface RegistrationRecoveryPanelProps { disabled: boolean; load: (cursor?: string) => Promise; check: (registrationId: string) => Promise; onRecovered: (receipt: RegistrationReceipt) => void; } const statusOf = (entry: RegistrationListEntry): string => { if (!entry.readable) return "Could not be read"; if (!entry.patientId) return "Not confirmed"; return entry.settled ? "Complete" : "Patient record exists; some steps are still open"; }; export default function RegistrationRecoveryPanel({ disabled, load, check, onRecovered }: RegistrationRecoveryPanelProps) { const [open, setOpen] = useState(false); const [entries, setEntries] = useState([]); const [cursor, setCursor] = useState(); const [complete, setComplete] = useState(true); const [busy, setBusy] = useState(false); const [notice, setNotice] = useState(); const read = async (from?: string) => { setBusy(true); setNotice(undefined); try { const page = await load(from); setEntries((known) => (from ? [...known, ...page.receipts] : page.receipts)); setCursor(page.cursor); setComplete(page.complete); } catch (error) { if (isSaveRetired(error)) return; setNotice("Your registrations could not be read. This does not mean there are none."); setComplete(false); } finally { setBusy(false); } }; const recover = async (registrationId: string) => { setBusy(true); setNotice(undefined); try { onRecovered(await check(registrationId)); } catch (error) { if (isSaveRetired(error)) return; setNotice("We could not confirm that registration. Nothing was created again; try Check later."); } finally { setBusy(false); } }; if (!open) { return (
); } return (
    {entries.map((entry) => (
  • {entry.displayName ?? "Unreadable registration"} {new Date(entry.startedAt).toLocaleString()} {statusOf(entry)} {entry.readable && ( )}
  • ))}
{busy &&

Reading your registrations…

} {!busy && entries.length === 0 && complete && !notice &&

You have no registrations at this practice.

} {!busy && !complete && !cursor && !notice &&

Some registrations could not be read.

} {notice &&

{notice}

} {cursor && ( )}
); }