@photonhealth/react 0.0.0-stage → 2.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +84 -2
- package/dist/adapters/index.d.ts +2 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +2 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/tailwind.d.ts +3 -0
- package/dist/adapters/tailwind.d.ts.map +1 -0
- package/dist/adapters/tailwind.js +123 -0
- package/dist/adapters/tailwind.js.map +1 -0
- package/dist/components/ChangeList.d.ts +21 -0
- package/dist/components/ChangeList.d.ts.map +1 -0
- package/dist/components/ChangeList.js +40 -0
- package/dist/components/ChangeList.js.map +1 -0
- package/dist/components/PhotonOrder.d.ts +46 -0
- package/dist/components/PhotonOrder.d.ts.map +1 -0
- package/dist/components/PhotonOrder.js +270 -0
- package/dist/components/PhotonOrder.js.map +1 -0
- package/dist/components/PhotonPatient.d.ts +48 -0
- package/dist/components/PhotonPatient.d.ts.map +1 -0
- package/dist/components/PhotonPatient.js +83 -0
- package/dist/components/PhotonPatient.js.map +1 -0
- package/dist/components/PhotonPrescription.d.ts +43 -0
- package/dist/components/PhotonPrescription.d.ts.map +1 -0
- package/dist/components/PhotonPrescription.js +364 -0
- package/dist/components/PhotonPrescription.js.map +1 -0
- package/dist/components/ScreeningAlerts.d.ts +6 -0
- package/dist/components/ScreeningAlerts.d.ts.map +1 -0
- package/dist/components/ScreeningAlerts.js +11 -0
- package/dist/components/ScreeningAlerts.js.map +1 -0
- package/dist/components/SignedIn.d.ts +12 -0
- package/dist/components/SignedIn.d.ts.map +1 -0
- package/dist/components/SignedIn.js +84 -0
- package/dist/components/SignedIn.js.map +1 -0
- package/dist/components/TreatmentPicker.d.ts +45 -0
- package/dist/components/TreatmentPicker.d.ts.map +1 -0
- package/dist/components/TreatmentPicker.js +279 -0
- package/dist/components/TreatmentPicker.js.map +1 -0
- package/dist/components/activity.d.ts +7 -0
- package/dist/components/activity.d.ts.map +1 -0
- package/dist/components/activity.js +24 -0
- package/dist/components/activity.js.map +1 -0
- package/dist/components/display.d.ts +90 -0
- package/dist/components/display.d.ts.map +1 -0
- package/dist/components/display.js +223 -0
- package/dist/components/display.js.map +1 -0
- package/dist/components/flow.d.ts +35 -0
- package/dist/components/flow.d.ts.map +1 -0
- package/dist/components/flow.js +53 -0
- package/dist/components/flow.js.map +1 -0
- package/dist/components/format.d.ts +26 -0
- package/dist/components/format.d.ts.map +1 -0
- package/dist/components/format.js +61 -0
- package/dist/components/format.js.map +1 -0
- package/dist/components/index.d.ts +6 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +6 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/open.d.ts +47 -0
- package/dist/components/open.d.ts.map +1 -0
- package/dist/components/open.js +73 -0
- package/dist/components/open.js.map +1 -0
- package/dist/components/patient/Benefits.d.ts +25 -0
- package/dist/components/patient/Benefits.d.ts.map +1 -0
- package/dist/components/patient/Benefits.js +47 -0
- package/dist/components/patient/Benefits.js.map +1 -0
- package/dist/components/patient/Clinical.d.ts +55 -0
- package/dist/components/patient/Clinical.d.ts.map +1 -0
- package/dist/components/patient/Clinical.js +117 -0
- package/dist/components/patient/Clinical.js.map +1 -0
- package/dist/components/patient/IntakeForm.d.ts +10 -0
- package/dist/components/patient/IntakeForm.d.ts.map +1 -0
- package/dist/components/patient/IntakeForm.js +324 -0
- package/dist/components/patient/IntakeForm.js.map +1 -0
- package/dist/components/patient/Summary.d.ts +48 -0
- package/dist/components/patient/Summary.d.ts.map +1 -0
- package/dist/components/patient/Summary.js +80 -0
- package/dist/components/patient/Summary.js.map +1 -0
- package/dist/components/patient/fields.d.ts +43 -0
- package/dist/components/patient/fields.d.ts.map +1 -0
- package/dist/components/patient/fields.js +71 -0
- package/dist/components/patient/fields.js.map +1 -0
- package/dist/components/patient/query.d.ts +23 -0
- package/dist/components/patient/query.d.ts.map +1 -0
- package/dist/components/patient/query.js +65 -0
- package/dist/components/patient/query.js.map +1 -0
- package/dist/components/search.d.ts +154 -0
- package/dist/components/search.d.ts.map +1 -0
- package/dist/components/search.js +295 -0
- package/dist/components/search.js.map +1 -0
- package/dist/hooks/index.d.ts +5 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +5 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useLatestCallback.d.ts +7 -0
- package/dist/hooks/useLatestCallback.d.ts.map +1 -0
- package/dist/hooks/useLatestCallback.js +14 -0
- package/dist/hooks/useLatestCallback.js.map +1 -0
- package/dist/hooks/useLive.d.ts +15 -0
- package/dist/hooks/useLive.d.ts.map +1 -0
- package/dist/hooks/useLive.js +6 -0
- package/dist/hooks/useLive.js.map +1 -0
- package/dist/hooks/useOnChange.d.ts +11 -0
- package/dist/hooks/useOnChange.d.ts.map +1 -0
- package/dist/hooks/useOnChange.js +28 -0
- package/dist/hooks/useOnChange.js.map +1 -0
- package/dist/hooks/useSession.d.ts +7 -0
- package/dist/hooks/useSession.d.ts.map +1 -0
- package/dist/hooks/useSession.js +10 -0
- package/dist/hooks/useSession.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/primitives/PhotonProvider.d.ts +58 -0
- package/dist/primitives/PhotonProvider.d.ts.map +1 -0
- package/dist/primitives/PhotonProvider.js +91 -0
- package/dist/primitives/PhotonProvider.js.map +1 -0
- package/dist/primitives/defaults.d.ts +3 -0
- package/dist/primitives/defaults.d.ts.map +1 -0
- package/dist/primitives/defaults.js +67 -0
- package/dist/primitives/defaults.js.map +1 -0
- package/dist/primitives/icons.d.ts +6 -0
- package/dist/primitives/icons.d.ts.map +1 -0
- package/dist/primitives/icons.js +6 -0
- package/dist/primitives/icons.js.map +1 -0
- package/dist/primitives/index.d.ts +5 -0
- package/dist/primitives/index.d.ts.map +1 -0
- package/dist/primitives/index.js +5 -0
- package/dist/primitives/index.js.map +1 -0
- package/dist/primitives/select.d.ts +7 -0
- package/dist/primitives/select.d.ts.map +1 -0
- package/dist/primitives/select.js +16 -0
- package/dist/primitives/select.js.map +1 -0
- package/dist/primitives/types.d.ts +276 -0
- package/dist/primitives/types.d.ts.map +1 -0
- package/dist/primitives/types.js +3 -0
- package/dist/primitives/types.js.map +1 -0
- package/dist/styles.css +611 -0
- package/dist/test-utils/contract.d.ts +17 -0
- package/dist/test-utils/contract.d.ts.map +1 -0
- package/dist/test-utils/contract.js +261 -0
- package/dist/test-utils/contract.js.map +1 -0
- package/package.json +74 -4
- package/src/adapters/index.ts +1 -0
- package/src/adapters/tailwind.tsx +544 -0
- package/src/components/ChangeList.tsx +85 -0
- package/src/components/PhotonOrder.tsx +493 -0
- package/src/components/PhotonPatient.tsx +184 -0
- package/src/components/PhotonPrescription.tsx +605 -0
- package/src/components/ScreeningAlerts.tsx +28 -0
- package/src/components/SignedIn.tsx +144 -0
- package/src/components/TreatmentPicker.tsx +350 -0
- package/src/components/activity.ts +23 -0
- package/src/components/display.ts +278 -0
- package/src/components/flow.ts +67 -0
- package/src/components/format.ts +73 -0
- package/src/components/index.ts +5 -0
- package/src/components/open.ts +100 -0
- package/src/components/patient/Benefits.tsx +125 -0
- package/src/components/patient/Clinical.tsx +210 -0
- package/src/components/patient/IntakeForm.tsx +532 -0
- package/src/components/patient/Summary.tsx +310 -0
- package/src/components/patient/fields.tsx +144 -0
- package/src/components/patient/query.ts +77 -0
- package/src/components/search.tsx +377 -0
- package/src/hooks/index.ts +4 -0
- package/src/hooks/useLatestCallback.ts +16 -0
- package/src/hooks/useLive.ts +21 -0
- package/src/hooks/useOnChange.ts +34 -0
- package/src/hooks/useSession.ts +11 -0
- package/src/index.ts +19 -0
- package/src/primitives/PhotonProvider.tsx +153 -0
- package/src/primitives/defaults.tsx +409 -0
- package/src/primitives/icons.tsx +37 -0
- package/src/primitives/index.ts +4 -0
- package/src/primitives/select.ts +15 -0
- package/src/primitives/types.ts +276 -0
- package/src/styles.css +611 -0
- package/src/test-utils/contract.tsx +410 -0
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
import { useComponents, usePhoton } from "../primitives/index.ts";
|
|
2
|
+
import { SignedIn } from "./SignedIn.tsx";
|
|
3
|
+
import {
|
|
4
|
+
asksForAddress,
|
|
5
|
+
Order,
|
|
6
|
+
Patient,
|
|
7
|
+
Prescription,
|
|
8
|
+
type Change,
|
|
9
|
+
type OrderInput,
|
|
10
|
+
type OrderUpdate,
|
|
11
|
+
type PatientInput,
|
|
12
|
+
} from "@photonhealth/client";
|
|
13
|
+
import { useLive, useOnChange } from "../hooks/index.ts";
|
|
14
|
+
import { useEffect, useRef, useState } from "react";
|
|
15
|
+
import { displayName } from "./format.ts";
|
|
16
|
+
import { callFor, NotFound, openFailed, openGiven, shared, toOpen, type Given } from "./open.ts";
|
|
17
|
+
import { PatientCard } from "./PhotonPatient.tsx";
|
|
18
|
+
import { RxLine } from "./PhotonPrescription.tsx";
|
|
19
|
+
import { ScreeningAlerts } from "./ScreeningAlerts.tsx";
|
|
20
|
+
import { SearchResults } from "./search.tsx";
|
|
21
|
+
import { cardFlowKeys, cardProps, focusProps } from "./flow.ts";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The order is the treatment: who it's for, their prescriptions, and sending them — on one card, bound to a
|
|
25
|
+
* live Order from @photonhealth/client: the one given (live, used as given; or opened from what's given, see `Given`),
|
|
26
|
+
* else a new order for the patient given, or for someone to find or add. The rules are the objects'; this renders
|
|
27
|
+
* them, plus what's only about the screen: which line is focused, and the Sent summary. Send shows once the order
|
|
28
|
+
* is ready and everything is saved.
|
|
29
|
+
*
|
|
30
|
+
* It renders only cards: no page, landmark or app chrome, so it sits in a page, a dialog or a side panel.
|
|
31
|
+
*/
|
|
32
|
+
export interface PhotonOrderProps {
|
|
33
|
+
/**
|
|
34
|
+
* The order: a live Order (used as given), its id (`"ord_…"` or `{ id }`: read), its id with fields (that order
|
|
35
|
+
* updated — e.g. prescriptions added), or what's known of one (synced): `{ patient: { id }, prescriptions: {
|
|
36
|
+
* add: [{ templateId: "tmp_…" }, { id: "rx_…" }] } }` — new lines from a template, existing prescriptions (the
|
|
37
|
+
* order takes their patient). Without prescriptions, it starts with one empty line.
|
|
38
|
+
*/
|
|
39
|
+
order?: Order | Given<OrderInput> | null;
|
|
40
|
+
/**
|
|
41
|
+
* Who a new order is for, when `order` doesn't say — given as a patient card's `patient` is. Without one,
|
|
42
|
+
* someone to find or add.
|
|
43
|
+
*/
|
|
44
|
+
patient?: Patient | Given<PatientInput> | null;
|
|
45
|
+
/**
|
|
46
|
+
* The order's patient, once it has a record and each time that record changes from the backend; `null` on
|
|
47
|
+
* "Change patient".
|
|
48
|
+
*/
|
|
49
|
+
onPatient?: (patient: Patient | null) => void;
|
|
50
|
+
/** The order was sent from this card — once per order; e.g. to close the dialog it's in. */
|
|
51
|
+
onSent?: (order: Order) => void;
|
|
52
|
+
/**
|
|
53
|
+
* The order this card shows — given, opened, or the next one (after "Change patient" on a saved draft) — once
|
|
54
|
+
* it, its patient or a prescription has a record, and each time one of those records changes from the
|
|
55
|
+
* backend (a line created or signed, the draft saved, sent). Its ids are what a URL keeps.
|
|
56
|
+
*/
|
|
57
|
+
onOrder?: (order: Order) => void;
|
|
58
|
+
/**
|
|
59
|
+
* The order's patient is fixed: "Change patient" is hidden — e.g. embedded beside the host's own view of that
|
|
60
|
+
* patient. The patient and the prescriptions stay editable. (An order handed in, or with existing
|
|
61
|
+
* prescriptions, is fixed anyway.)
|
|
62
|
+
*/
|
|
63
|
+
fixed?: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Its id known: update that order; before, sync what's known. A failure lands on the object (snapshot.error). */
|
|
67
|
+
const change = (order: Order, input: OrderUpdate) =>
|
|
68
|
+
(order.id ? order.update(input) : order.sync(input)).catch(() => {});
|
|
69
|
+
|
|
70
|
+
/** The events PhotonOrder passes on from its cards and its Send. */
|
|
71
|
+
type OrderEvents = Pick<PhotonOrderProps, "onPatient" | "onSent">;
|
|
72
|
+
|
|
73
|
+
/** What makes an order's onOrder fire: its record, its patient's and its prescriptions'. */
|
|
74
|
+
const orderRecords = (order: Order) => {
|
|
75
|
+
const lines = order.prescriptions.flatMap((rx) => (rx.state ? [rx.state] : []));
|
|
76
|
+
const patient = order.patient?.state ?? null;
|
|
77
|
+
return order.state || patient || lines.length ? [order.state, patient, lines] : null;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export function PhotonOrder(props: PhotonOrderProps) {
|
|
81
|
+
return (
|
|
82
|
+
<SignedIn>
|
|
83
|
+
<OrderCard {...props} />
|
|
84
|
+
</SignedIn>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** PhotonOrder, once signed in. */
|
|
89
|
+
function OrderCard(props: PhotonOrderProps) {
|
|
90
|
+
const { Alert, Text } = useComponents();
|
|
91
|
+
const ph = usePhoton();
|
|
92
|
+
// The order opened from what's given, and the next one (another patient) — a host's order is used as given.
|
|
93
|
+
const [opened, setOpened] = useState<Order | null>(null);
|
|
94
|
+
// A patient given by id that the server doesn't have: said above someone to find or add.
|
|
95
|
+
const [missingPatient, setMissingPatient] = useState<NotFound | null>(null);
|
|
96
|
+
const [next, setNext] = useState<Order | null>(null);
|
|
97
|
+
const sharedOrder = props.order instanceof Order ? props.order : null;
|
|
98
|
+
const order = next ?? sharedOrder ?? opened;
|
|
99
|
+
// Orders started on this card (not handed to it): their patient can change — a saved draft by starting over.
|
|
100
|
+
const [ours] = useState(() => new WeakSet<Order>()); // added to alongside the state that shows the order
|
|
101
|
+
useOnChange(order, orderRecords, props.onOrder);
|
|
102
|
+
// Why it couldn't be opened, for what it was given: given something else (or a live order), that's gone.
|
|
103
|
+
const [failed, setFailed] = useState<{ key: string; error: Error } | null>(null);
|
|
104
|
+
const sharedPatient = props.patient instanceof Patient ? props.patient : null;
|
|
105
|
+
const orderGiven = toOpen<OrderInput>(props.order, Boolean(sharedOrder));
|
|
106
|
+
const patientGiven = toOpen<PatientInput>(props.patient, Boolean(sharedPatient));
|
|
107
|
+
// What it's given, by value: a host re-rendering with the same values doesn't open it again.
|
|
108
|
+
const key = JSON.stringify([orderGiven, patientGiven]);
|
|
109
|
+
const how = orderGiven ? callFor(orderGiven) : null;
|
|
110
|
+
// Handed off — an existing order, or existing prescriptions (not template lines) — it keeps its patient.
|
|
111
|
+
const handedOff = Boolean(
|
|
112
|
+
sharedOrder ||
|
|
113
|
+
(how && how.call !== "sync") ||
|
|
114
|
+
(how?.call === "sync" &&
|
|
115
|
+
how.input.prescriptions?.add?.some((p) => ("id" in p && p.id) || ("externalId" in p && p.externalId))),
|
|
116
|
+
);
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
if (sharedOrder) return;
|
|
119
|
+
let live = true;
|
|
120
|
+
const [wanted, forPatient] = JSON.parse(key) as [Given<OrderInput> | null, Given<PatientInput> | null];
|
|
121
|
+
// Shared while it's in flight: StrictMode runs this twice, and a new order's draft is saved as it opens.
|
|
122
|
+
shared(sharedPatient ?? ph, key, async () => {
|
|
123
|
+
const opening = wanted ? callFor(wanted) : null;
|
|
124
|
+
if (opening && opening.call !== "sync") {
|
|
125
|
+
const existing = await openGiven(ph.order, wanted!, (r) => r.order);
|
|
126
|
+
if (!existing) throw new NotFound("order", opening.id);
|
|
127
|
+
return { order: existing, missing: null };
|
|
128
|
+
}
|
|
129
|
+
// Synced: what's known of it — its patient (or the one given; or someone to find or add) and prescriptions
|
|
130
|
+
// (or one empty line). The order's own rules decide the rest. A patient given by id that isn't there is said,
|
|
131
|
+
// and the order goes on with someone to find or add — its lines still open.
|
|
132
|
+
const input: OrderInput = opening?.input ?? {};
|
|
133
|
+
const givenPatient = input.patient ?? forPatient;
|
|
134
|
+
let patient: Patient | undefined = sharedPatient ?? undefined;
|
|
135
|
+
let missing: NotFound | null = null;
|
|
136
|
+
if (!patient && givenPatient) {
|
|
137
|
+
patient = (await openGiven(ph.patient, givenPatient, (r) => r.patient)) ?? undefined;
|
|
138
|
+
const id = typeof givenPatient === "string" ? givenPatient : givenPatient.id;
|
|
139
|
+
if (!patient && id) missing = new NotFound("patient", id);
|
|
140
|
+
}
|
|
141
|
+
const add = input.prescriptions?.add?.length ? input.prescriptions.add : [{}];
|
|
142
|
+
const started = ph.order();
|
|
143
|
+
// A save that fails (the draft order) shows on the order; only a record that can't be opened stops here.
|
|
144
|
+
await started.sync({ patient, prescriptions: { add } }).catch((e: unknown) => {
|
|
145
|
+
if (!started.prescriptions.length) throw e;
|
|
146
|
+
});
|
|
147
|
+
// An order with no patient yet (none given, no prescription to take one from) has someone to find or add.
|
|
148
|
+
if (!started.patient) await started.sync({ patient: ph.patient() });
|
|
149
|
+
return { order: started, missing };
|
|
150
|
+
})
|
|
151
|
+
.then(({ order: opening, missing }) => {
|
|
152
|
+
if (!handedOff && !opening.id) ours.add(opening);
|
|
153
|
+
if (!live) return;
|
|
154
|
+
setNext(null);
|
|
155
|
+
setOpened(opening);
|
|
156
|
+
setMissingPatient(missing);
|
|
157
|
+
})
|
|
158
|
+
.catch((error: Error) => live && setFailed({ key, error }));
|
|
159
|
+
return () => {
|
|
160
|
+
live = false;
|
|
161
|
+
};
|
|
162
|
+
}, [ph, ours, sharedOrder, sharedPatient, key, handedOff]);
|
|
163
|
+
|
|
164
|
+
const error = !sharedOrder && failed?.key === key ? failed.error : null;
|
|
165
|
+
if (error) return <Alert tone="critical" title={openFailed("order", error)} description={error.message} />;
|
|
166
|
+
if (!order) return <Text tone="tertiary">Loading…</Text>;
|
|
167
|
+
return (
|
|
168
|
+
<OrderView
|
|
169
|
+
key={keyOf(order)}
|
|
170
|
+
order={order}
|
|
171
|
+
handoff={(!ours.has(order) && handedOff) || Boolean(props.fixed)}
|
|
172
|
+
missingPatient={order === opened ? missingPatient : null}
|
|
173
|
+
events={props}
|
|
174
|
+
onNewOrder={(started) => {
|
|
175
|
+
ours.add(started);
|
|
176
|
+
setNext(started);
|
|
177
|
+
}}
|
|
178
|
+
/>
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function OrderView({
|
|
183
|
+
order,
|
|
184
|
+
handoff,
|
|
185
|
+
missingPatient,
|
|
186
|
+
events,
|
|
187
|
+
onNewOrder,
|
|
188
|
+
}: {
|
|
189
|
+
order: Order;
|
|
190
|
+
handoff: boolean;
|
|
191
|
+
/** The patient it was given by id, not found: said until someone is found or added instead. */
|
|
192
|
+
missingPatient: NotFound | null;
|
|
193
|
+
events: OrderEvents;
|
|
194
|
+
onNewOrder: (order: Order) => void;
|
|
195
|
+
}) {
|
|
196
|
+
const { Alert, Button, Stack } = useComponents();
|
|
197
|
+
const ph = usePhoton();
|
|
198
|
+
const snap = useLive(order)!;
|
|
199
|
+
const final = order.final;
|
|
200
|
+
const [sentHere, setSentHere] = useState(false);
|
|
201
|
+
const [added, setAdded] = useState<Prescription | null>(null);
|
|
202
|
+
const lines = snap.prescriptions;
|
|
203
|
+
const patient = snap.patient;
|
|
204
|
+
// New lines ("Add prescription", the next order) take the template the order's first line was started from.
|
|
205
|
+
const templateId = lines[0]?.templateId ?? null;
|
|
206
|
+
// A line has its drug once it has a record, or a pick waiting for the patient — "Add another" waits for all.
|
|
207
|
+
const filled = (line: Prescription) => Boolean(line.id || line.snapshot.pick);
|
|
208
|
+
const allFilled = lines.length > 0 && lines.every(filled);
|
|
209
|
+
|
|
210
|
+
// An open order with no prescriptions (a draft saved that way, or its last one taken off) gets an empty line to
|
|
211
|
+
// write one in, as a new order starts with. Its live lines, not this render's: StrictMode runs this twice.
|
|
212
|
+
useEffect(() => {
|
|
213
|
+
if (!final && order.prescriptions.length === 0) void change(order, { prescriptions: { add: [{}] } });
|
|
214
|
+
}, [order, final, lines.length]);
|
|
215
|
+
|
|
216
|
+
// Once a patient is found, the cursor moves on to the first line without a drug.
|
|
217
|
+
const [focus, setFocus] = useState<{ line: Prescription; signal: number; patientId: string | null } | null>(null);
|
|
218
|
+
const patientId = patient?.id ?? null;
|
|
219
|
+
if (patientId && focus?.patientId !== patientId) {
|
|
220
|
+
const next = lines.find((l) => !filled(l));
|
|
221
|
+
setFocus((f) => ({ line: next ?? lines[0]!, signal: (f?.signal ?? 0) + (next ? 1 : 0), patientId }));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Another patient: an order not saved yet takes them; a saved draft keeps its patient, so it's a new order
|
|
225
|
+
// (the draft stays on the server).
|
|
226
|
+
const switchPatient = (next: Patient) => {
|
|
227
|
+
if (!order.id) return void order.sync({ patient: next }).catch(() => {});
|
|
228
|
+
const fresh = ph.order();
|
|
229
|
+
void fresh.sync({ patient: next, prescriptions: { add: [{ templateId }] } }).then(
|
|
230
|
+
() => onNewOrder(fresh),
|
|
231
|
+
() => {},
|
|
232
|
+
);
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const flow = useRef<HTMLDivElement>(null);
|
|
236
|
+
// ← / → move between the cards: the patient, each prescription, "Add another", Send (flow.ts).
|
|
237
|
+
return (
|
|
238
|
+
<div ref={flow} onKeyDown={cardFlowKeys}>
|
|
239
|
+
<Stack gap="6">
|
|
240
|
+
{final && !sentHere && <Alert tone="info" title="This order was already sent" />}
|
|
241
|
+
|
|
242
|
+
<div {...cardProps}>
|
|
243
|
+
<PatientCard
|
|
244
|
+
patient={patient}
|
|
245
|
+
locked={final} // a sent order's patient is read-only
|
|
246
|
+
fixed={handoff}
|
|
247
|
+
addressRequest={final ? null : snap.contactRequest}
|
|
248
|
+
notFound={snap.patient?.id ? null : missingPatient}
|
|
249
|
+
onPatient={(next) => {
|
|
250
|
+
events.onPatient?.(next);
|
|
251
|
+
if (!next) switchPatient(ph.patient()); // "Change patient": someone new to find or add
|
|
252
|
+
}}
|
|
253
|
+
/>
|
|
254
|
+
</div>
|
|
255
|
+
|
|
256
|
+
{!final && snap.candidates.length > 0 && (
|
|
257
|
+
// The server asked which of the patient's draft orders this is; nothing is saved until one is picked.
|
|
258
|
+
<div {...cardProps}>
|
|
259
|
+
<Stack gap="2">
|
|
260
|
+
<Alert tone="warning" title="Several draft orders for this patient" />
|
|
261
|
+
<div {...focusProps}>
|
|
262
|
+
<SearchResults
|
|
263
|
+
label="Draft orders"
|
|
264
|
+
results={snap.candidates.map((c) => ({
|
|
265
|
+
key: c.id,
|
|
266
|
+
label: c.prescriptions.map((rx) => rx.treatment.name).join(", ") || "No prescriptions",
|
|
267
|
+
meta: c.pharmacy?.name,
|
|
268
|
+
}))}
|
|
269
|
+
onPick={(id) => void order.pick(id).catch(() => {})}
|
|
270
|
+
busy={snap.status === "loading" || snap.status === "saving"}
|
|
271
|
+
/>
|
|
272
|
+
</div>
|
|
273
|
+
</Stack>
|
|
274
|
+
</div>
|
|
275
|
+
)}
|
|
276
|
+
|
|
277
|
+
{lines.map((line) => (
|
|
278
|
+
<div key={keyOf(line)} {...cardProps}>
|
|
279
|
+
<RxLine
|
|
280
|
+
prescription={line}
|
|
281
|
+
locked={final} // a signed line is read-only on its own
|
|
282
|
+
// A line comes off; the only one, once it has a drug, gives way to a fresh line (a different drug).
|
|
283
|
+
onRemove={
|
|
284
|
+
final
|
|
285
|
+
? undefined
|
|
286
|
+
: lines.length > 1
|
|
287
|
+
? () => void change(order, { prescriptions: { remove: [line] } })
|
|
288
|
+
: filled(line)
|
|
289
|
+
? () => {
|
|
290
|
+
void change(order, { prescriptions: { remove: [line], add: [{ templateId }] } });
|
|
291
|
+
setAdded(order.prescriptions.at(-1) ?? null); // the fresh line, at once
|
|
292
|
+
}
|
|
293
|
+
: undefined
|
|
294
|
+
}
|
|
295
|
+
autoFocus={line === added}
|
|
296
|
+
focusSignal={focus?.line === line ? focus.signal : undefined}
|
|
297
|
+
/>
|
|
298
|
+
</div>
|
|
299
|
+
))}
|
|
300
|
+
|
|
301
|
+
{!final && allFilled && (
|
|
302
|
+
<div data-prx-card="">
|
|
303
|
+
<span {...focusProps}>
|
|
304
|
+
<Button
|
|
305
|
+
variant="ghost"
|
|
306
|
+
icon="add"
|
|
307
|
+
onClick={() => {
|
|
308
|
+
void change(order, { prescriptions: { add: [{ templateId }] } });
|
|
309
|
+
setAdded(order.prescriptions.at(-1) ?? null); // a new line is added at once
|
|
310
|
+
}}
|
|
311
|
+
>
|
|
312
|
+
Add another prescription
|
|
313
|
+
</Button>
|
|
314
|
+
</span>
|
|
315
|
+
</div>
|
|
316
|
+
)}
|
|
317
|
+
|
|
318
|
+
<div {...cardProps}>
|
|
319
|
+
<SendSection
|
|
320
|
+
order={order}
|
|
321
|
+
scope={flow}
|
|
322
|
+
// Send appears once the order is ready — the patient found, each prescription signed — and saved.
|
|
323
|
+
ready={order.readyToSend}
|
|
324
|
+
onSubmitted={() => {
|
|
325
|
+
setSentHere(true);
|
|
326
|
+
events.onSent?.(order);
|
|
327
|
+
}}
|
|
328
|
+
/>
|
|
329
|
+
</div>
|
|
330
|
+
</Stack>
|
|
331
|
+
</div>
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Send, then the Sent summary — what's left to know once it's gone (the prescriptions are on their cards above;
|
|
337
|
+
* where to go next is the host's navigation). No pharmacy — the patient chooses from the marketplace (a pharmacy already on
|
|
338
|
+
* an opened order is shown and kept). Send shows only when the order can be sent, never as a greyed-out
|
|
339
|
+
* button; what stopped the last send stays shown.
|
|
340
|
+
*/
|
|
341
|
+
function SendSection({
|
|
342
|
+
order,
|
|
343
|
+
ready,
|
|
344
|
+
onSubmitted,
|
|
345
|
+
scope,
|
|
346
|
+
}: {
|
|
347
|
+
order: Order;
|
|
348
|
+
ready: boolean;
|
|
349
|
+
onSubmitted: () => void;
|
|
350
|
+
/** The order's element: ⌘↵ sends from inside it. */
|
|
351
|
+
scope: { current: HTMLElement | null };
|
|
352
|
+
}) {
|
|
353
|
+
const { ActionBar, Alert, Badge, Button, Card, CardBody, CardHeader, Row, Stack, Text } = useComponents();
|
|
354
|
+
const snap = useLive(order)!;
|
|
355
|
+
const [sending, setSending] = useState(false);
|
|
356
|
+
// What the last send left on the order (its changes, or its error) — told apart from a later save's.
|
|
357
|
+
const [sent, setSent] = useState<{ changes: unknown; error: unknown } | null>(null);
|
|
358
|
+
const record = snap.state;
|
|
359
|
+
const pharmacy = record?.pharmacy ?? null;
|
|
360
|
+
const count = snap.prescriptions.length;
|
|
361
|
+
const patient = snap.patient?.state;
|
|
362
|
+
const first = patient?.demographic?.name.first ? displayName(patient.demographic.name.first) : null;
|
|
363
|
+
|
|
364
|
+
const send = () => {
|
|
365
|
+
if (sending) return;
|
|
366
|
+
setSending(true);
|
|
367
|
+
order
|
|
368
|
+
.submit()
|
|
369
|
+
.then(() => order.final && onSubmitted())
|
|
370
|
+
.catch(() => {}) // a refusal or failure lands on the object (snap.error)
|
|
371
|
+
.finally(() => {
|
|
372
|
+
setSent({ changes: order.changes, error: order.snapshot.error });
|
|
373
|
+
setSending(false);
|
|
374
|
+
});
|
|
375
|
+
};
|
|
376
|
+
// ⌘↵ / Ctrl↵ sends from anywhere in the order — never page-wide: in a host's app the same keys are the
|
|
377
|
+
// host's (a ⌘↵ in its own chat box must not send prescriptions).
|
|
378
|
+
useEffect(() => {
|
|
379
|
+
const el = scope.current;
|
|
380
|
+
const onKey = (e: KeyboardEvent) => {
|
|
381
|
+
if (e.key === "Enter" && (e.metaKey || e.ctrlKey) && ready && !sending) {
|
|
382
|
+
e.preventDefault();
|
|
383
|
+
e.stopPropagation();
|
|
384
|
+
send();
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
el?.addEventListener("keydown", onKey);
|
|
388
|
+
return () => el?.removeEventListener("keydown", onKey);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
if (order.final && record) {
|
|
392
|
+
return (
|
|
393
|
+
<Card divided>
|
|
394
|
+
<CardHeader title={first ? `Sent to ${first}` : "Sent"} />
|
|
395
|
+
<CardBody>
|
|
396
|
+
<Stack gap="3">
|
|
397
|
+
{/* The status tag at the end of the first line, as on a saved prescription. */}
|
|
398
|
+
<Row gap="2" justify="between">
|
|
399
|
+
<Text tone="secondary">
|
|
400
|
+
{pharmacy ? `Sent to ${pharmacy.name}.` : "The patient will choose a pharmacy."}
|
|
401
|
+
</Text>
|
|
402
|
+
<Badge tone="success" icon="check">
|
|
403
|
+
Sent
|
|
404
|
+
</Badge>
|
|
405
|
+
</Row>
|
|
406
|
+
{record.exceptions.map((x) => (
|
|
407
|
+
<Alert
|
|
408
|
+
key={x.id}
|
|
409
|
+
tone="warning"
|
|
410
|
+
title={x.type.replace(/_/g, " ").toLowerCase()}
|
|
411
|
+
description={x.message ?? undefined}
|
|
412
|
+
/>
|
|
413
|
+
))}
|
|
414
|
+
</Stack>
|
|
415
|
+
</CardBody>
|
|
416
|
+
</Card>
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
// What stopped the last send (the server's reasons, or a failure / refusal) — or, between sends, the draft
|
|
420
|
+
// order not saving (e.g. this patient's last order was sent).
|
|
421
|
+
const fromSend = Boolean(sent && sent.changes === snap.changes && sent.error === snap.error);
|
|
422
|
+
const notSent = fromSend ? reasons(snap.changes.unapplied) : [];
|
|
423
|
+
const notSaved = !fromSend && !sending && !snap.error ? reasons(snap.changes.unapplied) : [];
|
|
424
|
+
const contact = Boolean(snap.contactRequest);
|
|
425
|
+
const failure = snap.error;
|
|
426
|
+
const showSend = ready || sending;
|
|
427
|
+
// Every alert no one has acknowledged — the prescriptions' interactions with each other too — before Send.
|
|
428
|
+
const alerts = order.unacknowledgedAlerts;
|
|
429
|
+
if (!alerts.length && !notSent.length && !notSaved.length && !failure && !showSend) return null;
|
|
430
|
+
return (
|
|
431
|
+
<Stack gap="2">
|
|
432
|
+
<ScreeningAlerts alerts={alerts} />
|
|
433
|
+
{notSent.length > 0 && (
|
|
434
|
+
// A missing phone or address is asked for on the patient card, not here.
|
|
435
|
+
<Alert tone="warning" title="Not sent" description={notSent.map((c) => c.reason ?? c.label).join(" ")} />
|
|
436
|
+
)}
|
|
437
|
+
{failure && !(contact && asksForAddress(null, failure.message)) && (
|
|
438
|
+
<Alert
|
|
439
|
+
tone={failure.kind === "refused" ? "warning" : "critical"}
|
|
440
|
+
title={
|
|
441
|
+
fromSend
|
|
442
|
+
? failure.kind === "refused"
|
|
443
|
+
? "Not sent"
|
|
444
|
+
: "Couldn't send"
|
|
445
|
+
: failure.kind === "refused"
|
|
446
|
+
? "Order not saved"
|
|
447
|
+
: "Couldn't save the order"
|
|
448
|
+
}
|
|
449
|
+
description={failure.message}
|
|
450
|
+
/>
|
|
451
|
+
)}
|
|
452
|
+
{notSaved.length > 0 && (
|
|
453
|
+
<Alert
|
|
454
|
+
tone="warning"
|
|
455
|
+
title="Order not saved"
|
|
456
|
+
description={notSaved.map((c) => c.reason ?? c.label).join(" ")}
|
|
457
|
+
/>
|
|
458
|
+
)}
|
|
459
|
+
{showSend && (
|
|
460
|
+
<ActionBar>
|
|
461
|
+
<span {...focusProps}>
|
|
462
|
+
<Button loading={sending} onClick={send}>
|
|
463
|
+
{pharmacy
|
|
464
|
+
? `Send to ${pharmacy.name}`
|
|
465
|
+
: count > 1
|
|
466
|
+
? "Send prescriptions to patient"
|
|
467
|
+
: "Send prescription to patient"}
|
|
468
|
+
</Button>
|
|
469
|
+
</span>
|
|
470
|
+
</ActionBar>
|
|
471
|
+
)}
|
|
472
|
+
</Stack>
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* What to tell the prescriber about an unsent order: the server's unapplied changes, minus the patient's
|
|
478
|
+
* contact (the patient card asks for it, and it's cleared once they've given it) and the order's own `state`
|
|
479
|
+
* when something else explains why it wasn't sent (network-api adds "a sibling change was rejected").
|
|
480
|
+
*/
|
|
481
|
+
function reasons(unapplied: readonly Change[]): Change[] {
|
|
482
|
+
const own = unapplied.filter((c) => !c.key.startsWith("patient.demographic."));
|
|
483
|
+
const explained = own.some((c) => c.key !== "state") || own.length < unapplied.length;
|
|
484
|
+
return explained ? own.filter((c) => c.key !== "state") : own;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** A stable key per live object. */
|
|
488
|
+
const objectKeys = new WeakMap<object, number>();
|
|
489
|
+
let nextKey = 0;
|
|
490
|
+
const keyOf = (obj: object) => {
|
|
491
|
+
if (!objectKeys.has(obj)) objectKeys.set(obj, ++nextKey);
|
|
492
|
+
return objectKeys.get(obj)!;
|
|
493
|
+
};
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { useComponents, usePhoton } from "../primitives/index.ts";
|
|
2
|
+
import { SignedIn } from "./SignedIn.tsx";
|
|
3
|
+
import { classify, Patient, type ChangeOption, type PatientInput, type PatientResult } from "@photonhealth/client";
|
|
4
|
+
import { useLive, useOnChange } from "../hooks/index.ts";
|
|
5
|
+
import { useState } from "react";
|
|
6
|
+
import { NotFound, openFailed, toOpen, useOpened, type Given } from "./open.ts";
|
|
7
|
+
import { ChangeList } from "./ChangeList.tsx";
|
|
8
|
+
import { PatientIntakeForm } from "./patient/IntakeForm.tsx";
|
|
9
|
+
import { diagnosisRef, withoutInline } from "./patient/Clinical.tsx";
|
|
10
|
+
import { PatientSummary } from "./patient/Summary.tsx";
|
|
11
|
+
|
|
12
|
+
type PatientClinicalInput = NonNullable<PatientInput["clinical"]>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* "Who is this patient?" — standalone or inside PhotonOrder. Bound to a live Patient from @photonhealth/client:
|
|
16
|
+
* the one given (`patient`), one opened from what's given (get / update / sync, see `Given`), or a new one to
|
|
17
|
+
* find or add. Before it has a record, every field the prescriber leaves is synced — the server decides: the
|
|
18
|
+
* patient (found, or added), patients it might be (pick one), or what it still needs. Once it has one, edits
|
|
19
|
+
* update it.
|
|
20
|
+
*/
|
|
21
|
+
export interface PhotonPatientProps {
|
|
22
|
+
/**
|
|
23
|
+
* The patient: a live Patient (used as given; anything else holding it sees the same changes), its id
|
|
24
|
+
* (`"pat_…"` or `{ id }`: read), its id with fields (that patient updated), or what's known of them without
|
|
25
|
+
* one (synced: found, matched or added).
|
|
26
|
+
*/
|
|
27
|
+
patient?: Patient | Given<PatientInput> | null;
|
|
28
|
+
/**
|
|
29
|
+
* The card's patient, once it has a record (found, picked, added, or opened with) and each time that record
|
|
30
|
+
* changes from the backend. `null` on "Change patient": a card given `patient` leaves what's next to the
|
|
31
|
+
* host (pass it a new one); otherwise it starts over itself.
|
|
32
|
+
*/
|
|
33
|
+
onPatient?: (patient: Patient | null) => void;
|
|
34
|
+
/** Read-only (e.g. on a sent order): nothing to edit, and no "Change patient". */
|
|
35
|
+
locked?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* The patient is fixed — e.g. beside the host's own view of them: no "Change patient", but still editable.
|
|
38
|
+
* (An embed whose link names the patient by id is fixed.)
|
|
39
|
+
*/
|
|
40
|
+
fixed?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A patient card. */
|
|
44
|
+
export function PhotonPatient(props: PhotonPatientProps) {
|
|
45
|
+
return (
|
|
46
|
+
<SignedIn>
|
|
47
|
+
<PatientCard {...props} />
|
|
48
|
+
</SignedIn>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A stable key per live object: a different patient object gets a fresh form. */
|
|
53
|
+
const objectKeys = new WeakMap<object, number>();
|
|
54
|
+
let nextKey = 0;
|
|
55
|
+
const keyOf = (obj: object) => {
|
|
56
|
+
if (!objectKeys.has(obj)) objectKeys.set(obj, ++nextKey);
|
|
57
|
+
return objectKeys.get(obj)!;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const patientOf = (r: PatientResult) => r.patient;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* PhotonPatient, and what only PhotonOrder gives it: the server's request for a way to reach the patient (its
|
|
64
|
+
* reason), which opens the contact fields. Internal: the package index exports only PhotonPatient.
|
|
65
|
+
*/
|
|
66
|
+
export function PatientCard({
|
|
67
|
+
patient,
|
|
68
|
+
onPatient,
|
|
69
|
+
locked,
|
|
70
|
+
fixed,
|
|
71
|
+
addressRequest,
|
|
72
|
+
notFound,
|
|
73
|
+
}: PhotonPatientProps & {
|
|
74
|
+
addressRequest?: string | null;
|
|
75
|
+
/** The patient it was given by id isn't there (PhotonOrder's): said in the form, with the matches. */
|
|
76
|
+
notFound?: NotFound | null;
|
|
77
|
+
}) {
|
|
78
|
+
const { Alert, Card, CardHeader } = useComponents();
|
|
79
|
+
const ph = usePhoton();
|
|
80
|
+
// A new patient to find or add when the host gives none (and none to open) — and after "Change patient",
|
|
81
|
+
// when the host didn't give one.
|
|
82
|
+
const shared = patient instanceof Patient ? patient : null;
|
|
83
|
+
const given = toOpen<PatientInput>(patient, Boolean(shared));
|
|
84
|
+
const [own, setOwn] = useState<Patient | null>(() => (shared || given ? null : ph.patient()));
|
|
85
|
+
const { opened, error: openError } = useOpened(ph.patient, given, patientOf, "patient");
|
|
86
|
+
// Given by id and not there: someone to find or add instead, the reason said in the form — where its matches,
|
|
87
|
+
// and anything else the server says, show. (Read-only, there's nothing to find: just the reason.)
|
|
88
|
+
const missing = openError instanceof NotFound && !locked ? openError : null;
|
|
89
|
+
// One patient object, always: the one given, the one opened from what's given, or the card's own — made once
|
|
90
|
+
// there's none to show (nothing given, "Change patient", or an id the server doesn't have).
|
|
91
|
+
if (missing && !own && !shared) setOwn(ph.patient());
|
|
92
|
+
// The host gives another patient: that one, not the card's own.
|
|
93
|
+
const givenKey = JSON.stringify(given);
|
|
94
|
+
const [ownFor, setOwnFor] = useState(givenKey);
|
|
95
|
+
if (givenKey !== ownFor) {
|
|
96
|
+
setOwnFor(givenKey);
|
|
97
|
+
if (given && own) setOwn(null);
|
|
98
|
+
}
|
|
99
|
+
const live = shared ?? own ?? opened;
|
|
100
|
+
const snap = useLive(live);
|
|
101
|
+
const record = snap?.state ?? null;
|
|
102
|
+
useOnChange(live, (p) => p.state, onPatient);
|
|
103
|
+
|
|
104
|
+
if (openError && !missing)
|
|
105
|
+
return <Alert tone="critical" title={openFailed("patient", openError)} description={openError.message} />;
|
|
106
|
+
if (!live || !snap) {
|
|
107
|
+
return (
|
|
108
|
+
<Card divided>
|
|
109
|
+
<CardHeader title="Patient" description="Loading…" />
|
|
110
|
+
</Card>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const changes = classify(snap.changes);
|
|
115
|
+
const update = (input: Omit<PatientInput, "id">) => void live.update(input).catch(() => {}); // failures land on the object
|
|
116
|
+
const patchClinical = (clinical: PatientClinicalInput) => update({ clinical });
|
|
117
|
+
const choose = (option: ChangeOption) => void live.choose(option).catch(() => {});
|
|
118
|
+
|
|
119
|
+
if (record) {
|
|
120
|
+
const messages = withoutInline(changes);
|
|
121
|
+
const hasMessages =
|
|
122
|
+
messages.needsChoice.length + messages.blocking.length + messages.advisories.length > 0 || Boolean(snap.error);
|
|
123
|
+
return (
|
|
124
|
+
<PatientSummary
|
|
125
|
+
key={record.id}
|
|
126
|
+
patient={record}
|
|
127
|
+
pickedNames={snap.pickedNames}
|
|
128
|
+
saving={snap.status === "saving"}
|
|
129
|
+
changes={
|
|
130
|
+
// Only when there's something to say — the card has no body otherwise.
|
|
131
|
+
hasMessages && (
|
|
132
|
+
<>
|
|
133
|
+
<ChangeList changes={messages} onChoose={choose} busy={snap.status === "saving"} />
|
|
134
|
+
{/* A failed save must never be silent (e.g. a rejected clinical patch). */}
|
|
135
|
+
{snap.error && <Alert tone="critical" title="Couldn't save" description={snap.error.message} />}
|
|
136
|
+
</>
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
choices={changes.needsChoice}
|
|
140
|
+
problems={changes.blocking}
|
|
141
|
+
onChoose={choose}
|
|
142
|
+
onAddAllergy={(name) => patchClinical({ allergies: { add: [{ name }] } })}
|
|
143
|
+
onConfirmNone={(list) => patchClinical({ [list]: { none: true } })}
|
|
144
|
+
onRemoveAllergy={(a) => patchClinical({ allergies: { remove: [{ id: a.id }] } })}
|
|
145
|
+
onRemoveMedication={(m) => patchClinical({ medications: { remove: [{ id: m.id, status: m.status }] } })}
|
|
146
|
+
onAddDiagnosis={(text) => patchClinical({ diagnoses: { add: [diagnosisRef(text)] } })}
|
|
147
|
+
onRemoveDiagnosis={(d) => patchClinical({ diagnoses: { remove: [{ id: d.id }] } })}
|
|
148
|
+
onAddBenefit={(b) => update({ demographic: { benefits: { add: [b] } } })}
|
|
149
|
+
onRemoveBenefit={(b) => update({ demographic: { benefits: { remove: [{ id: b.id }] } } })}
|
|
150
|
+
onConfirmNoBenefits={() => update({ demographic: { benefits: { none: true } } })}
|
|
151
|
+
onAddMedication={(name) => patchClinical({ medications: { add: [{ name, status: "ACTIVE" }] } })}
|
|
152
|
+
onSaveContact={(contact) => update({ demographic: contact })}
|
|
153
|
+
onSaveDetails={(details) => update({ demographic: details })}
|
|
154
|
+
addressRequest={locked ? null : (addressRequest ?? null)}
|
|
155
|
+
readOnly={locked}
|
|
156
|
+
onChangePatient={
|
|
157
|
+
locked || fixed
|
|
158
|
+
? undefined
|
|
159
|
+
: () => {
|
|
160
|
+
onPatient?.(null); // given a patient, the host decides what's next
|
|
161
|
+
if (!shared) setOwn(ph.patient());
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/>
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// A different patient object (e.g. after "Change patient") starts a fresh form.
|
|
169
|
+
const why = notFound ?? missing;
|
|
170
|
+
return (
|
|
171
|
+
<PatientIntakeForm
|
|
172
|
+
key={keyOf(live)}
|
|
173
|
+
patient={live}
|
|
174
|
+
notice={why && { title: openFailed("patient", why), description: why.message }}
|
|
175
|
+
/>
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Find or add the patient. Each field the prescriber leaves (and Enter) sends everything known so far to the
|
|
181
|
+
* live Patient — one query that searches and syncs. The server decides: the patient, patients it might be
|
|
182
|
+
* (listed to pick from), or what it still needs (shown; an address only when it asks). Fields left, not
|
|
183
|
+
* keystrokes: a query with enough to add a patient adds them.
|
|
184
|
+
*/
|