import { useEffect, useState } from "react"; import { AlertCircle, Loader2 } from "lucide-react"; import { useAuthServiceContext } from "../../../../services/AuthService.tsx"; import { useOrganizationServiceContext } from "../../../../services/OrganizationService.tsx"; import { Checkbox } from "../../../../components/ui/checkbox.tsx"; import { Label } from "../../../../components/ui/label.tsx"; import { useOwnedSettingSave } from "../../../../hooks/useOwnedSettingSave"; import { DIAGNOSTICS_FIELDS, type DiagnosticsDeclaration, type DiagnosticsField, hasDeclaredDiagnostics, readDiagnosticsDeclaration, } from "../../../../utils/organizationDiagnostics"; import "./DiagnosticsPage.css"; type Answers = Record; const seed = (declaration: DiagnosticsDeclaration): Answers => Object.fromEntries(DIAGNOSTICS_FIELDS.map((field) => [field, declaration[field] === true])) as Answers; const DiagnosticsSettingsTab = ({ onSaved, onManage }: { onSaved?: () => void; onManage?: (tab: "laboratories" | "catalogue") => void; }) => { const { currentOrganization, setWpOrganization } = useAuthServiceContext(); const { updateDiagnosticsSettings, fetchOrganizationAsCaller } = useOrganizationServiceContext(); const declaration = readDiagnosticsDeclaration(currentOrganization ?? undefined); const neverAsked = !hasDeclaredDiagnostics(declaration); // Saves and their recovery belong to this practice as opened here; the answer is what the practice stored. const { state, save: saveSetting, check, owner, ownsOpening } = useOwnedSettingSave(currentOrganization?.id, { read: (id, transport) => fetchOrganizationAsCaller(id, undefined, id, transport), publish: setWpOrganization, }); const [draft, setDraft] = useState(() => ({ owner, answers: seed(declaration), dirty: false, editedSinceSave: false, seeded: state.stored, })); const owned = ownsOpening(); const answers = owned && draft.owner === owner ? draft.answers : seed(declaration); const saving = state.status === "saving"; const held = state.status === "unconfirmed" || state.status === "checking"; const stored = owned && state.stored ? readDiagnosticsDeclaration(state.stored) : undefined; useEffect(() => { if (!ownsOpening()) return; setDraft((was) => { if (was.owner !== owner) return { owner, answers: seed(readDiagnosticsDeclaration(state.stored ?? currentOrganization ?? undefined)), dirty: false, editedSinceSave: false, seeded: state.stored, }; // A confirmed read seeds a fresh owner's draft, never choices they have already edited. if (state.stored && !was.dirty && was.seeded !== state.stored) return { ...was, answers: seed(readDiagnosticsDeclaration(state.stored)), seeded: state.stored, }; return was; }); }, [owner, ownsOpening, state.stored, currentOrganization]); const choice = (field: DiagnosticsField, label: string) => (
{ if (!ownsOpening()) return; setDraft((current) => current.owner === owner ? { ...current, answers: { ...current.answers, [field]: checked === true }, dirty: true, editedSinceSave: true, } : current); }} />
); const save = async () => { if (!ownsOpening() || draft.owner !== owner) return; // Who the laboratory accepts orders from only means something while it performs tests, so // those two are left as they were rather than answered by turning the laboratory off. const { acceptsOwnOrders, acceptsOutsideOrders, ...rest } = answers; const sent: DiagnosticsDeclaration = answers.laboratory ? { ...rest, acceptsOwnOrders, acceptsOutsideOrders } : rest; setDraft((current) => current.owner === owner ? { ...current, editedSinceSave: false } : current); await saveSetting((transport) => updateDiagnosticsSettings(sent, transport), { onSaved: () => onSaved?.(), refusal: "Could not save that. Please try again.", }); }; return (

How does your practice handle laboratory work?

Choose everything your practice does. Collecting a specimen and performing the test are separate.

{neverAsked ? (

Nothing can be added to the test catalogue until the practice performs tests or has imaging on site.

) : null} {owned && state.message ? (
) : null} {stored ? (

We could not confirm the last save. Stored now: {storedSummary(stored)}.

) : null} {choice("ordersExternal", "We order tests from external laboratories")} {choice("collectsSpecimens", "We collect specimens at our practice")} {choice("laboratory", "We perform tests in our own laboratory")} {answers.laboratory ? (

Orders our laboratory accepts

{choice("acceptsOwnOrders", "From our own practitioners")} {choice("acceptsOutsideOrders", "From other practices")}

Outside practices do not need an Akashic account to send a requisition.

) : null}

Imaging

{choice("imaging", "We have imaging equipment on site")}

{owned && draft.owner === owner && state.status === "saved" && !draft.editedSinceSave ? "Practice setup saved." : owned && state.status === "checking" ? "Checking what is stored." : ""}

Next, configure the work you do

External laboratories

Connections, practice accounts and each laboratory’s order codes.


Our test catalogue

Tests we perform, specimen requirements, entry questions and expected results.


); }; const STORED_LABELS: Record = { ordersExternal: "orders from external laboratories", collectsSpecimens: "collects specimens", laboratory: "performs tests", acceptsOwnOrders: "accepts its own orders", acceptsOutsideOrders: "accepts outside orders", imaging: "imaging on site", }; const storedSummary = (declaration: DiagnosticsDeclaration): string => { const yes = DIAGNOSTICS_FIELDS.filter((field) => declaration[field] === true).map((field) => STORED_LABELS[field]); return yes.length ? yes.join(", ") : "nothing declared"; }; export default DiagnosticsSettingsTab;