@photonhealth/react 0.0.0-stage → 2.0.0-rc.2
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 +111 -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 +126 -0
- package/dist/adapters/tailwind.js.map +1 -0
- package/dist/analytics/PhotonAnalyticsProvider.d.ts +16 -0
- package/dist/analytics/PhotonAnalyticsProvider.d.ts.map +1 -0
- package/dist/analytics/PhotonAnalyticsProvider.js +57 -0
- package/dist/analytics/PhotonAnalyticsProvider.js.map +1 -0
- package/dist/analytics/fieldInteraction.d.ts +18 -0
- package/dist/analytics/fieldInteraction.d.ts.map +1 -0
- package/dist/analytics/fieldInteraction.js +102 -0
- package/dist/analytics/fieldInteraction.js.map +1 -0
- package/dist/analytics/tracker.d.ts +17 -0
- package/dist/analytics/tracker.d.ts.map +1 -0
- package/dist/analytics/tracker.js +36 -0
- package/dist/analytics/tracker.js.map +1 -0
- package/dist/analytics/viewed.d.ts +11 -0
- package/dist/analytics/viewed.d.ts.map +1 -0
- package/dist/analytics/viewed.js +16 -0
- package/dist/analytics/viewed.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 +274 -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 +85 -0
- package/dist/components/PhotonPatient.js.map +1 -0
- package/dist/components/PhotonPrescription.d.ts +32 -0
- package/dist/components/PhotonPrescription.d.ts.map +1 -0
- package/dist/components/PhotonPrescription.js +11 -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/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/order/DraftOrderPrompt.d.ts +18 -0
- package/dist/components/order/DraftOrderPrompt.d.ts.map +1 -0
- package/dist/components/order/DraftOrderPrompt.js +26 -0
- package/dist/components/order/DraftOrderPrompt.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 +48 -0
- package/dist/components/patient/Benefits.js.map +1 -0
- package/dist/components/patient/Clinical.d.ts +56 -0
- package/dist/components/patient/Clinical.d.ts.map +1 -0
- package/dist/components/patient/Clinical.js +118 -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 +333 -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 +90 -0
- package/dist/components/patient/Summary.js.map +1 -0
- package/dist/components/patient/fields.d.ts +42 -0
- package/dist/components/patient/fields.d.ts.map +1 -0
- package/dist/components/patient/fields.js +72 -0
- package/dist/components/patient/fields.js.map +1 -0
- package/dist/components/patient/query.d.ts +37 -0
- package/dist/components/patient/query.d.ts.map +1 -0
- package/dist/components/patient/query.js +70 -0
- package/dist/components/patient/query.js.map +1 -0
- package/dist/components/prescription/PrescriptionCard.d.ts +45 -0
- package/dist/components/prescription/PrescriptionCard.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionCard.js +179 -0
- package/dist/components/prescription/PrescriptionCard.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts +15 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js +18 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts +23 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js +42 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts +27 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js +169 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts +8 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js +12 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts +11 -0
- package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js +25 -0
- package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js.map +1 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts +50 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts.map +1 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js +287 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js.map +1 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts +28 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts.map +1 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js +146 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js.map +1 -0
- package/dist/components/schemaErrors.d.ts +12 -0
- package/dist/components/schemaErrors.d.ts.map +1 -0
- package/dist/components/schemaErrors.js +24 -0
- package/dist/components/schemaErrors.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 +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/meta/version.d.ts +5 -0
- package/dist/meta/version.d.ts.map +1 -0
- package/dist/meta/version.js +5 -0
- package/dist/meta/version.js.map +1 -0
- package/dist/primitives/PhotonProvider.d.ts +60 -0
- package/dist/primitives/PhotonProvider.d.ts.map +1 -0
- package/dist/primitives/PhotonProvider.js +103 -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 +70 -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/photonContext.d.ts +3 -0
- package/dist/primitives/photonContext.d.ts.map +1 -0
- package/dist/primitives/photonContext.js +3 -0
- package/dist/primitives/photonContext.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 +288 -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/primitives/useProvidedPhoton.d.ts +8 -0
- package/dist/primitives/useProvidedPhoton.d.ts.map +1 -0
- package/dist/primitives/useProvidedPhoton.js +8 -0
- package/dist/primitives/useProvidedPhoton.js.map +1 -0
- package/dist/styles.css +653 -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 +282 -0
- package/dist/test-utils/contract.js.map +1 -0
- package/package.json +76 -4
- package/src/adapters/index.ts +1 -0
- package/src/adapters/tailwind.tsx +572 -0
- package/src/analytics/PhotonAnalyticsProvider.test.tsx +191 -0
- package/src/analytics/PhotonAnalyticsProvider.tsx +96 -0
- package/src/analytics/fieldInteraction.test.tsx +184 -0
- package/src/analytics/fieldInteraction.tsx +148 -0
- package/src/analytics/tracker.test.ts +52 -0
- package/src/analytics/tracker.ts +42 -0
- package/src/analytics/viewed.ts +27 -0
- package/src/components/ChangeList.tsx +85 -0
- package/src/components/PhotonOrder.test.tsx +256 -0
- package/src/components/PhotonOrder.tsx +508 -0
- package/src/components/PhotonPatient.tsx +188 -0
- package/src/components/PhotonPrescription.test.tsx +116 -0
- package/src/components/PhotonPrescription.tsx +45 -0
- package/src/components/ScreeningAlerts.tsx +28 -0
- package/src/components/SignedIn.tsx +144 -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/order/DraftOrderPrompt.test.tsx +80 -0
- package/src/components/order/DraftOrderPrompt.tsx +81 -0
- package/src/components/patient/Benefits.tsx +133 -0
- package/src/components/patient/Clinical.tsx +215 -0
- package/src/components/patient/IntakeForm.test.tsx +89 -0
- package/src/components/patient/IntakeForm.tsx +564 -0
- package/src/components/patient/Summary.test.tsx +106 -0
- package/src/components/patient/Summary.tsx +330 -0
- package/src/components/patient/fields.tsx +158 -0
- package/src/components/patient/query.ts +83 -0
- package/src/components/prescription/PrescriptionCard.test.tsx +85 -0
- package/src/components/prescription/PrescriptionCard.tsx +347 -0
- package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.test.tsx +85 -0
- package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.tsx +41 -0
- package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.test.tsx +209 -0
- package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.tsx +99 -0
- package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.test.tsx +456 -0
- package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.tsx +238 -0
- package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.test.ts +63 -0
- package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.ts +15 -0
- package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.test.tsx +75 -0
- package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.tsx +41 -0
- package/src/components/prescription/TreatmentSearch/TreatmentPicker.tsx +375 -0
- package/src/components/prescription/TreatmentSearch/TreatmentSearch.test.tsx +699 -0
- package/src/components/prescription/TreatmentSearch/TreatmentSearch.tsx +220 -0
- package/src/components/schemaErrors.test.ts +49 -0
- package/src/components/schemaErrors.ts +33 -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 +21 -0
- package/src/meta/version.test.ts +10 -0
- package/src/meta/version.ts +4 -0
- package/src/primitives/PhotonProvider.tsx +168 -0
- package/src/primitives/defaults.tsx +432 -0
- package/src/primitives/icons.tsx +37 -0
- package/src/primitives/index.ts +4 -0
- package/src/primitives/photonContext.ts +4 -0
- package/src/primitives/select.ts +15 -0
- package/src/primitives/types.ts +288 -0
- package/src/primitives/useProvidedPhoton.ts +21 -0
- package/src/styles.css +653 -0
- package/src/test-utils/contract.tsx +453 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { useComponents, usePhoton } from "../../primitives/index.ts";
|
|
2
|
+
import {
|
|
3
|
+
classify,
|
|
4
|
+
Patient,
|
|
5
|
+
Prescription,
|
|
6
|
+
type PatientInput,
|
|
7
|
+
type PatientResult,
|
|
8
|
+
type PrescriptionEdits,
|
|
9
|
+
type PrescriptionInput,
|
|
10
|
+
type PrescriptionRecord,
|
|
11
|
+
type PrescriptionResult,
|
|
12
|
+
type PrescriptionTemplate,
|
|
13
|
+
} from "@photonhealth/client";
|
|
14
|
+
import { useLive, useOnChange } from "../../hooks/index.ts";
|
|
15
|
+
import { useEffect, useRef, useState, type ReactNode } from "react";
|
|
16
|
+
import { useHeldActivity } from "../activity.ts";
|
|
17
|
+
import { SearchResults } from "../search.tsx";
|
|
18
|
+
import { NotFound, openFailed, toOpen, useOpened, type Given } from "../open.ts";
|
|
19
|
+
import { treatmentHeading } from "../display.ts";
|
|
20
|
+
import { dispenseSummary } from "../format.ts";
|
|
21
|
+
import { ChangeList } from "../ChangeList.tsx";
|
|
22
|
+
import { PrescriptionDetailsForm } from "./PrescriptionDetailsForm/PrescriptionDetailsForm.tsx";
|
|
23
|
+
import { ScreeningAlerts } from "../ScreeningAlerts.tsx";
|
|
24
|
+
import { TreatmentSearch, type TreatmentChoice } from "./TreatmentSearch/TreatmentSearch.tsx";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* One prescription line, bound to a live Prescription: PhotonPrescription's card (which adds the analytics workflow
|
|
28
|
+
* and the sign-in), and each line of PhotonOrder. Internal: the package index exports only PhotonPrescription.
|
|
29
|
+
*/
|
|
30
|
+
export interface PrescriptionCardProps {
|
|
31
|
+
/**
|
|
32
|
+
* The prescription: a live Prescription (used as given; anything else holding it sees the same changes), its
|
|
33
|
+
* id (`"rx_…"` or `{ id }`: read), its id with fields (that prescription updated), or fields without one
|
|
34
|
+
* (synced — e.g. `{ templateId }` or `{ treatment: { name } }`).
|
|
35
|
+
*/
|
|
36
|
+
prescription?: Prescription | Given<PrescriptionInput> | null;
|
|
37
|
+
/**
|
|
38
|
+
* Who it's for — given as a patient card's `patient` is. Until it's known, a new line only looks drugs up
|
|
39
|
+
* (a line from an order already knows).
|
|
40
|
+
*/
|
|
41
|
+
patient?: Patient | Given<PatientInput> | null;
|
|
42
|
+
/** Read-only (e.g. on a finalized order). A signed prescription is always read-only. */
|
|
43
|
+
locked?: boolean;
|
|
44
|
+
/** The prescriber signed it on this card — once per prescription. */
|
|
45
|
+
onSigned?: (prescription: Prescription) => void;
|
|
46
|
+
/**
|
|
47
|
+
* The card's prescription, once it has a record and each time that record changes from the backend
|
|
48
|
+
* (created, saved, signed).
|
|
49
|
+
*/
|
|
50
|
+
onPrescription?: (prescription: Prescription) => void;
|
|
51
|
+
/** Shows a Remove button (an order's line). */
|
|
52
|
+
onRemove?: () => void;
|
|
53
|
+
/** Put the cursor in Treatment (a line the prescriber just added). */
|
|
54
|
+
autoFocus?: boolean;
|
|
55
|
+
/** Put the cursor in Treatment each time this changes (e.g. a patient was just picked). */
|
|
56
|
+
focusSignal?: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function StatusBadge({ rx }: { rx: PrescriptionRecord | null }) {
|
|
60
|
+
const { Badge } = useComponents();
|
|
61
|
+
if (!rx) return null;
|
|
62
|
+
if (rx.signing.state === "SIGNED")
|
|
63
|
+
return (
|
|
64
|
+
<Badge tone="success" icon="check">
|
|
65
|
+
Signed
|
|
66
|
+
</Badge>
|
|
67
|
+
);
|
|
68
|
+
if (rx.signing.state === "INVALIDATED_SIGN") return <Badge tone="warning">Changed since signing</Badge>;
|
|
69
|
+
// No tag for screening: its alerts are listed above the sig. Nor for nothing found: network-api doesn't screen
|
|
70
|
+
// allergies without an RxNorm id (and doesn't say which), so "No alerts" could read as a clinical check that
|
|
71
|
+
// didn't happen.
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// One empty list, so a line with no templates doesn't hand PrescriptionDetailsForm new options every render.
|
|
76
|
+
const NO_TEMPLATES: readonly PrescriptionTemplate[] = [];
|
|
77
|
+
const prescriptionOf = (r: PrescriptionResult) => r.prescription;
|
|
78
|
+
const patientOf = (r: PatientResult) => r.patient;
|
|
79
|
+
|
|
80
|
+
export function PrescriptionCard({
|
|
81
|
+
patient,
|
|
82
|
+
prescription,
|
|
83
|
+
locked,
|
|
84
|
+
onSigned,
|
|
85
|
+
onPrescription,
|
|
86
|
+
onRemove,
|
|
87
|
+
autoFocus,
|
|
88
|
+
focusSignal,
|
|
89
|
+
}: PrescriptionCardProps) {
|
|
90
|
+
const { Alert, Button, Card, CardBody, CardHeader, Spinner, Stack } = useComponents();
|
|
91
|
+
const ph = usePhoton();
|
|
92
|
+
// A new line when the host gives no prescription (and none to open).
|
|
93
|
+
const shared = prescription instanceof Prescription ? prescription : null;
|
|
94
|
+
const given = toOpen<PrescriptionInput>(prescription, Boolean(shared));
|
|
95
|
+
const [own] = useState(() => (shared || given ? null : ph.prescription()));
|
|
96
|
+
const { opened, error: rxError } = useOpened(ph.prescription, given, prescriptionOf, "prescription");
|
|
97
|
+
// Who it's for, opened the same way; its id once it has a record.
|
|
98
|
+
const sharedPatient = patient instanceof Patient ? patient : null;
|
|
99
|
+
const { opened: openedPatient, error: patientError } = useOpened(
|
|
100
|
+
ph.patient,
|
|
101
|
+
toOpen<PatientInput>(patient, Boolean(sharedPatient)),
|
|
102
|
+
patientOf,
|
|
103
|
+
"patient",
|
|
104
|
+
);
|
|
105
|
+
const patientSnap = useLive(sharedPatient ?? openedPatient);
|
|
106
|
+
// Not given: leave it (a line from an order has its own). Given: its id once it has one; null clears it.
|
|
107
|
+
const patientId = patient === undefined ? undefined : patient === null ? null : patientSnap?.state?.id;
|
|
108
|
+
const openError = rxError ?? patientError;
|
|
109
|
+
const live = shared ?? opened ?? own;
|
|
110
|
+
const snap = useLive(live);
|
|
111
|
+
useOnChange(live, (rx) => rx.state, onPrescription);
|
|
112
|
+
const signedHere = useRef(new Set<string>()); // onSigned: once per prescription
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
if (live && patientId !== undefined)
|
|
115
|
+
void live.sync({ patient: patientId ? { id: patientId } : null }).catch(() => {});
|
|
116
|
+
}, [live, patientId]);
|
|
117
|
+
|
|
118
|
+
const rx = snap?.state ?? null;
|
|
119
|
+
const changes = classify(snap?.changes ?? { applied: [], unapplied: [] });
|
|
120
|
+
const [signing, setSigning] = useState(false);
|
|
121
|
+
// The error showing came from Add, which signs (its title says so); the next save's error is a save's again.
|
|
122
|
+
const [signFailed, setSignFailed] = useState(false);
|
|
123
|
+
const busy = signing || snap?.status === "saving" || snap?.status === "loading";
|
|
124
|
+
// Signing is per prescription, and a signed one is final here: no edits (any change would void the
|
|
125
|
+
// signature). A different drug or sig is a new line.
|
|
126
|
+
const readOnly = Boolean(locked) || Boolean(live?.signed);
|
|
127
|
+
// Add (the signature) is offered once the server left nothing open. Screening alerts don't stop it: signing is the
|
|
128
|
+
// prescriber's acknowledgement of what they show (network-api can't acknowledge an alert yet).
|
|
129
|
+
const canSign = Boolean(rx) && changes.settled;
|
|
130
|
+
// The field whose change sent the call in flight shows a spinner — nowhere else, so nothing opens and
|
|
131
|
+
// closes while the prescriber works (held briefly, so it doesn't flicker).
|
|
132
|
+
const [sentFrom, setSentFrom] = useState<string | null>(null);
|
|
133
|
+
const working = snap?.status === "saving" || snap?.status === "loading";
|
|
134
|
+
if (sentFrom && !working) setSentFrom(null); // answered: a later call (e.g. signing) isn't that field's
|
|
135
|
+
const spinning = useHeldActivity(sentFrom);
|
|
136
|
+
|
|
137
|
+
const save = (edits: PrescriptionEdits, from?: string) => {
|
|
138
|
+
if (readOnly || !live) return;
|
|
139
|
+
setSignFailed(false);
|
|
140
|
+
// Its id known: update that prescription; before, sync what's known.
|
|
141
|
+
void (live.id ? live.update(edits) : live.sync(edits)).catch(() => {}); // a failure lands on the object (snap.error)
|
|
142
|
+
if (from && live.busy) setSentFrom(from);
|
|
143
|
+
};
|
|
144
|
+
const add = () => {
|
|
145
|
+
if (!live) return;
|
|
146
|
+
setSigning(true);
|
|
147
|
+
setSignFailed(false);
|
|
148
|
+
void live
|
|
149
|
+
.sign()
|
|
150
|
+
.then(() => {
|
|
151
|
+
if (!live.signed || !live.id || signedHere.current.has(live.id)) return;
|
|
152
|
+
signedHere.current.add(live.id);
|
|
153
|
+
onSigned?.(live);
|
|
154
|
+
})
|
|
155
|
+
.catch(() => setSignFailed(true)) // a refusal or failure lands on the object (snap.error)
|
|
156
|
+
.finally(() => setSigning(false));
|
|
157
|
+
};
|
|
158
|
+
const pick = snap?.pick ?? null;
|
|
159
|
+
// A drug chosen, until the server answers: the picker stays as it was picked (a spinner on the drug's row), and
|
|
160
|
+
// the directions and dispense wait — a moment to see what was picked before the form changes under it.
|
|
161
|
+
const [choosing, setChoosing] = useState<TreatmentChoice | null>(null);
|
|
162
|
+
const chooseTreatment = (choice: TreatmentChoice) => {
|
|
163
|
+
if (!live) return;
|
|
164
|
+
setChoosing(choice);
|
|
165
|
+
void live
|
|
166
|
+
.choose(choice.option)
|
|
167
|
+
.catch(() => {}) // a failure lands on the object (snap.error)
|
|
168
|
+
.finally(() => setChoosing(null));
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// The title is the resolved treatment — not what the provider is typing (or an unresolved choice).
|
|
172
|
+
const drugChoices = changes.needsChoice.filter((c) => c.key.startsWith("treatment"));
|
|
173
|
+
const otherChoices = changes.needsChoice.filter((c) => !c.key.startsWith("treatment"));
|
|
174
|
+
const treatmentPending = drugChoices.length > 0;
|
|
175
|
+
// Choosing among the drugs: the picker's up (none while a product chosen saves: it heads the card at once).
|
|
176
|
+
const pickerUp = treatmentPending && !choosing;
|
|
177
|
+
const resolvedName = (rx ? !treatmentPending : pick) && !choosing ? (live?.name ?? null) : null;
|
|
178
|
+
// Sig / dispense can be filled once a drug is picked (and its choice has saved) — before a patient too (kept
|
|
179
|
+
// until the line attaches).
|
|
180
|
+
const fieldsOpen = Boolean(rx || pick) && !choosing;
|
|
181
|
+
useEffect(() => {
|
|
182
|
+
if (fieldsOpen && !readOnly) live?.loadTemplates();
|
|
183
|
+
}, [live, fieldsOpen, readOnly]);
|
|
184
|
+
// "Lisinopril 10 mg tablet" over "Oral tablet · 10 mg" — the picked product, said once. A product chosen heads the
|
|
185
|
+
// card as soon as it's picked, from the option itself, while the server drafts it.
|
|
186
|
+
const heading = choosing
|
|
187
|
+
? treatmentHeading(choosing.option.displayName, choosing.option.treatment)
|
|
188
|
+
: resolvedName
|
|
189
|
+
? treatmentHeading(resolvedName, live?.treatment)
|
|
190
|
+
: null;
|
|
191
|
+
// The Treatment field is for finding a drug: shown until one is picked. A different drug is a new line.
|
|
192
|
+
const searching = !choosing && (!fieldsOpen || pickerUp);
|
|
193
|
+
|
|
194
|
+
// A failed call: shown where the drug list drops down (under Treatment), with the card's other messages — and so
|
|
195
|
+
// is a prescription this line stands in for that the server doesn't have (an order link's `rx=`), until it's filled.
|
|
196
|
+
const missing = live?.missing && !rx ? new NotFound("prescription", live.missing) : null;
|
|
197
|
+
const opening = openError ?? missing;
|
|
198
|
+
const error = snap?.error ?? opening;
|
|
199
|
+
const failure = error && (
|
|
200
|
+
<Alert
|
|
201
|
+
tone="critical"
|
|
202
|
+
title={
|
|
203
|
+
!snap?.error && opening
|
|
204
|
+
? openFailed(opening === patientError ? "patient" : "prescription", opening)
|
|
205
|
+
: signFailed
|
|
206
|
+
? "Couldn't sign this prescription"
|
|
207
|
+
: snap?.error?.kind === "refused" && !rx
|
|
208
|
+
? "Can't start a new prescription"
|
|
209
|
+
: "Couldn't save this prescription"
|
|
210
|
+
}
|
|
211
|
+
description={error.message}
|
|
212
|
+
/>
|
|
213
|
+
);
|
|
214
|
+
// The body above the form shows only when something is in it. A divided card draws a line above each section, so an
|
|
215
|
+
// empty one (Treatment closed once a drug is picked, nothing to say) would show as a double line. Keep this in step
|
|
216
|
+
// with what the body renders.
|
|
217
|
+
const hasBody =
|
|
218
|
+
readOnly ||
|
|
219
|
+
searching ||
|
|
220
|
+
Boolean(error) ||
|
|
221
|
+
otherChoices.length + changes.blocking.length + changes.advisories.length > 0 ||
|
|
222
|
+
Boolean(snap?.drafts) ||
|
|
223
|
+
openAlerts(rx).length > 0 ||
|
|
224
|
+
Boolean(choosing);
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<Card divided label={heading?.title ?? "Prescription"}>
|
|
228
|
+
<CardHeader
|
|
229
|
+
title={heading?.title ?? "Prescription"}
|
|
230
|
+
// The product in parts — a brand's ingredients, its form · strength, a controlled substance's schedule —
|
|
231
|
+
// with no NDC / RxNorm stand-ins.
|
|
232
|
+
description={heading?.detail}
|
|
233
|
+
action={
|
|
234
|
+
!locked &&
|
|
235
|
+
onRemove && (
|
|
236
|
+
<Button
|
|
237
|
+
variant="ghost"
|
|
238
|
+
size="sm"
|
|
239
|
+
tone="critical"
|
|
240
|
+
icon="trash"
|
|
241
|
+
aria-label="Remove prescription"
|
|
242
|
+
title="Remove prescription"
|
|
243
|
+
onClick={onRemove}
|
|
244
|
+
/>
|
|
245
|
+
)
|
|
246
|
+
}
|
|
247
|
+
/>
|
|
248
|
+
{hasBody && (
|
|
249
|
+
<CardBody>
|
|
250
|
+
<Stack gap="4">
|
|
251
|
+
{readOnly ? (
|
|
252
|
+
<>
|
|
253
|
+
{failure}
|
|
254
|
+
<ReadOnlyRx rx={rx} status={<StatusBadge rx={rx} />} />
|
|
255
|
+
<ScreeningAlerts alerts={openAlerts(rx)} />
|
|
256
|
+
</>
|
|
257
|
+
) : (
|
|
258
|
+
<>
|
|
259
|
+
<TreatmentSearch
|
|
260
|
+
live={live}
|
|
261
|
+
rx={rx}
|
|
262
|
+
pickId={pick?.id ?? null}
|
|
263
|
+
drugChoices={drugChoices}
|
|
264
|
+
choosing={choosing}
|
|
265
|
+
open={searching}
|
|
266
|
+
busy={busy}
|
|
267
|
+
spinning={spinning}
|
|
268
|
+
autoFocus={autoFocus}
|
|
269
|
+
focusSignal={focusSignal}
|
|
270
|
+
save={save}
|
|
271
|
+
onChoose={chooseTreatment}
|
|
272
|
+
/>
|
|
273
|
+
{/* Where the drug list drops down: what failed, what the server said, what screening found. */}
|
|
274
|
+
{failure}
|
|
275
|
+
<ChangeList
|
|
276
|
+
changes={{ ...changes, needsChoice: otherChoices }}
|
|
277
|
+
onChoose={(o) => void live?.choose(o).catch(() => {})}
|
|
278
|
+
busy={busy}
|
|
279
|
+
/>
|
|
280
|
+
{/* The patient has several open prescriptions: this line continues the one picked. */}
|
|
281
|
+
{snap?.drafts && (
|
|
282
|
+
<SearchResults
|
|
283
|
+
label="Continue a draft"
|
|
284
|
+
busy={busy}
|
|
285
|
+
results={snap.drafts.map((d) => ({
|
|
286
|
+
key: d.id,
|
|
287
|
+
label: d.treatment.name,
|
|
288
|
+
description: [d.instructions, dispenseSummary(d)].filter(Boolean).join(" · ") || undefined,
|
|
289
|
+
}))}
|
|
290
|
+
onPick={(id) => void live?.continueDraft(id).catch(() => {})}
|
|
291
|
+
/>
|
|
292
|
+
)}
|
|
293
|
+
{/* What screening found (an allergy, an interaction, a condition) — read before the directions. */}
|
|
294
|
+
<ScreeningAlerts alerts={openAlerts(rx)} />
|
|
295
|
+
{/* A product chosen, saving: a spinner where its directions will be. */}
|
|
296
|
+
{choosing && <Spinner />}
|
|
297
|
+
</>
|
|
298
|
+
)}
|
|
299
|
+
</Stack>
|
|
300
|
+
</CardBody>
|
|
301
|
+
)}
|
|
302
|
+
{/* Once a drug is set: the directions first, then quantity / unit / days / refills, then DAW — and Add, which
|
|
303
|
+
signs it (before a patient too: signing then says a patient is required). */}
|
|
304
|
+
{!readOnly && (
|
|
305
|
+
<PrescriptionDetailsForm
|
|
306
|
+
live={live}
|
|
307
|
+
rx={rx}
|
|
308
|
+
open={fieldsOpen}
|
|
309
|
+
focusable={!searching}
|
|
310
|
+
templates={snap?.templates ?? NO_TEMPLATES}
|
|
311
|
+
spinning={spinning}
|
|
312
|
+
save={save}
|
|
313
|
+
status={<StatusBadge rx={rx} />}
|
|
314
|
+
onAdd={add}
|
|
315
|
+
adding={signing}
|
|
316
|
+
addDisabled={snap?.status === "loading" || (Boolean(rx) && !canSign)}
|
|
317
|
+
/>
|
|
318
|
+
)}
|
|
319
|
+
</Card>
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** A saved prescription: its instructions — with its status tag at the end of that line — then what's dispensed. */
|
|
324
|
+
export function ReadOnlyRx({ rx, status }: { rx: PrescriptionRecord | null; status: ReactNode }) {
|
|
325
|
+
const { Row, Stack, Text } = useComponents();
|
|
326
|
+
// Locked before there's anything in it: say so, rather than an empty card.
|
|
327
|
+
if (!rx) return <Text tone="tertiary">No prescription</Text>;
|
|
328
|
+
const dispense = dispenseSummary(rx);
|
|
329
|
+
const first = rx.instructions ? (
|
|
330
|
+
<span>{rx.instructions}</span>
|
|
331
|
+
) : dispense ? (
|
|
332
|
+
<Text tone="secondary">{dispense}</Text>
|
|
333
|
+
) : null;
|
|
334
|
+
return (
|
|
335
|
+
<Stack gap="1">
|
|
336
|
+
<Row gap="2" justify="between">
|
|
337
|
+
{first ?? <span />}
|
|
338
|
+
{status}
|
|
339
|
+
</Row>
|
|
340
|
+
{rx.instructions && dispense && <Text tone="secondary">{dispense}</Text>}
|
|
341
|
+
</Stack>
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** What screening found, until it's signed: the signature acknowledges them. */
|
|
346
|
+
const openAlerts = (rx: PrescriptionRecord | null) =>
|
|
347
|
+
rx?.signing.state === "SIGNED" ? [] : (rx?.screeningAlerts ?? []);
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { DirectionsInput, type DirectionsInputProps } from "./DirectionsInput.tsx";
|
|
5
|
+
|
|
6
|
+
const directions = "Take 1 capsule by mouth 3 times per day";
|
|
7
|
+
|
|
8
|
+
const renderInput = (props: Partial<DirectionsInputProps> = {}) => {
|
|
9
|
+
const all: DirectionsInputProps = {
|
|
10
|
+
value: directions,
|
|
11
|
+
edited: false,
|
|
12
|
+
focusable: false,
|
|
13
|
+
onEdit: vi.fn(),
|
|
14
|
+
onCommit: vi.fn(),
|
|
15
|
+
...props,
|
|
16
|
+
};
|
|
17
|
+
render(<DirectionsInput {...all} />);
|
|
18
|
+
return { ...all, input: screen.getByLabelText<HTMLTextAreaElement>("Directions (sig)") };
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
describe("DirectionsInput", () => {
|
|
22
|
+
afterEach(() => cleanup());
|
|
23
|
+
|
|
24
|
+
it("shows the directions", () => {
|
|
25
|
+
const { input } = renderInput();
|
|
26
|
+
expect(input.value).toBe(directions);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("when I leave the field", () => {
|
|
30
|
+
it("hands off edited directions to be saved", () => {
|
|
31
|
+
const { input, onCommit } = renderInput({ edited: true });
|
|
32
|
+
fireEvent.blur(input);
|
|
33
|
+
expect(onCommit).toHaveBeenCalledOnce();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("doesn't save directions I didn't change", () => {
|
|
37
|
+
const { input, onCommit } = renderInput();
|
|
38
|
+
fireEvent.blur(input);
|
|
39
|
+
expect(onCommit).not.toHaveBeenCalled();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("when I focus the field", () => {
|
|
44
|
+
it("selects an unedited value, so typing replaces it", () => {
|
|
45
|
+
const { input } = renderInput();
|
|
46
|
+
act(() => input.focus());
|
|
47
|
+
expect([input.selectionStart, input.selectionEnd]).toEqual([0, directions.length]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("doesn't select an edited value", () => {
|
|
51
|
+
const { input } = renderInput({ edited: true });
|
|
52
|
+
act(() => input.focus());
|
|
53
|
+
expect(input.selectionStart).toBe(input.selectionEnd);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("error message", () => {
|
|
58
|
+
const noteOf = (input: HTMLElement) => {
|
|
59
|
+
const note = input.getAttribute("aria-describedby");
|
|
60
|
+
return note ? document.getElementById(note)?.textContent : null;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
it("shows the error message it's given under the field, and marks the field invalid", () => {
|
|
64
|
+
const { input } = renderInput({ errorMessage: "Required" });
|
|
65
|
+
expect(input.getAttribute("aria-invalid")).toBe("true");
|
|
66
|
+
expect(noteOf(input)).toBe("Required");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("shows no errors when none are specified", () => {
|
|
70
|
+
const { input } = renderInput();
|
|
71
|
+
expect(input.getAttribute("aria-invalid")).toBeNull();
|
|
72
|
+
expect(noteOf(input) ?? null).toBeNull();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("is where the card flow lands when focusable", () => {
|
|
77
|
+
const { input } = renderInput({ focusable: true });
|
|
78
|
+
expect(input.closest("[data-prx-focus]")).not.toBeNull();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("isn't where the card flow lands when not focusable", () => {
|
|
82
|
+
const { input } = renderInput();
|
|
83
|
+
expect(input.closest("[data-prx-focus]")).toBeNull();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useComponents } from "../../../primitives/index.ts";
|
|
2
|
+
import { focusProps } from "../../flow.ts";
|
|
3
|
+
import { useFieldInteractions } from "../../../analytics/fieldInteraction.tsx";
|
|
4
|
+
|
|
5
|
+
/** A prescription's directions (sig). Its value is kept by PrescriptionDetailsForm, which decides what's saved. */
|
|
6
|
+
export interface DirectionsInputProps {
|
|
7
|
+
value: string;
|
|
8
|
+
/** What's wrong with it, shown under it. */
|
|
9
|
+
errorMessage?: string;
|
|
10
|
+
/** Typed since it was last committed. */
|
|
11
|
+
edited: boolean;
|
|
12
|
+
/** Where the card flow (← / →) lands, when Treatment isn't. */
|
|
13
|
+
focusable: boolean;
|
|
14
|
+
onEdit: (value: string) => void;
|
|
15
|
+
/** Leaving the field after editing it. */
|
|
16
|
+
onCommit: () => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function DirectionsInput({ value, errorMessage, edited, focusable, onEdit, onCommit }: DirectionsInputProps) {
|
|
20
|
+
const { Textarea } = useComponents();
|
|
21
|
+
const directions = useFieldInteractions("add_prescription_form")("instructions", { isOptional: false });
|
|
22
|
+
return (
|
|
23
|
+
<div {...(focusable ? focusProps : {})} {...directions}>
|
|
24
|
+
<Textarea
|
|
25
|
+
label="Directions (sig)"
|
|
26
|
+
rows={2}
|
|
27
|
+
value={value}
|
|
28
|
+
invalid={Boolean(errorMessage)}
|
|
29
|
+
errorMessage={errorMessage}
|
|
30
|
+
// A value the server filled in (e.g. "Take as directed") is selected, so typing replaces it.
|
|
31
|
+
onFocus={(e) => {
|
|
32
|
+
if (!edited) e.currentTarget.select();
|
|
33
|
+
}}
|
|
34
|
+
onChange={(e) => onEdit(e.target.value)}
|
|
35
|
+
onBlur={() => {
|
|
36
|
+
if (edited) onCommit();
|
|
37
|
+
}}
|
|
38
|
+
/>
|
|
39
|
+
</div>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { act, cleanup, fireEvent, render, screen, within } from "@testing-library/react";
|
|
3
|
+
import userEvent from "@testing-library/user-event";
|
|
4
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
5
|
+
import { fakeNetworkApiClient } from "@photonhealth/client/testing";
|
|
6
|
+
import { DispenseInputs, type DispenseInputsProps, type DispenseValues } from "./DispenseInputs.tsx";
|
|
7
|
+
import {
|
|
8
|
+
edited,
|
|
9
|
+
leaveField,
|
|
10
|
+
meAsPrescriber,
|
|
11
|
+
recordAnalytics,
|
|
12
|
+
unmountAndRestoreFetch,
|
|
13
|
+
withAnalytics,
|
|
14
|
+
} from "../../../../test/analytics.fixtures.tsx";
|
|
15
|
+
|
|
16
|
+
const dispense: DispenseValues = {
|
|
17
|
+
quantity: "30",
|
|
18
|
+
unit: "Capsule",
|
|
19
|
+
daysSupply: "10",
|
|
20
|
+
refills: "0",
|
|
21
|
+
dispenseAsWritten: false,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const renderInputs = (props: Partial<DispenseInputsProps> = {}, values: Partial<DispenseValues> = {}) => {
|
|
25
|
+
const all: DispenseInputsProps = {
|
|
26
|
+
values: { ...dispense, ...values },
|
|
27
|
+
units: [],
|
|
28
|
+
edited: new Set(),
|
|
29
|
+
spinning: null,
|
|
30
|
+
onEdit: vi.fn(),
|
|
31
|
+
save: vi.fn(),
|
|
32
|
+
...props,
|
|
33
|
+
};
|
|
34
|
+
render(<DispenseInputs {...all} />);
|
|
35
|
+
return all;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const input = (label: string) => screen.getByLabelText<HTMLInputElement>(label);
|
|
39
|
+
|
|
40
|
+
const unitOptions = () =>
|
|
41
|
+
within(screen.getByRole("combobox", { name: "Unit" }))
|
|
42
|
+
.getAllByRole<HTMLOptionElement>("option")
|
|
43
|
+
.filter((option) => !option.disabled)
|
|
44
|
+
.map((option) => option.textContent);
|
|
45
|
+
|
|
46
|
+
describe("DispenseInputs", () => {
|
|
47
|
+
afterEach(() => cleanup());
|
|
48
|
+
|
|
49
|
+
it("shows the dispense", () => {
|
|
50
|
+
renderInputs();
|
|
51
|
+
expect(input("Quantity").value).toBe("30");
|
|
52
|
+
expect(input("Unit").value).toBe("Capsule");
|
|
53
|
+
expect(input("Days supply").value).toBe("10");
|
|
54
|
+
expect(input("Refills").value).toBe("0");
|
|
55
|
+
expect(screen.getByRole<HTMLInputElement>("checkbox", { name: "Dispense as written" }).checked).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("when I leave a field", () => {
|
|
59
|
+
it.each([
|
|
60
|
+
{ edit: "Quantity set to 60", label: "Quantity", field: "quantity", typed: "60", patch: { quantity: 60 } },
|
|
61
|
+
{ edit: "Quantity set to 2.5", label: "Quantity", field: "quantity", typed: "2.5", patch: { quantity: 2.5 } },
|
|
62
|
+
{ edit: "Quantity cleared", label: "Quantity", field: "quantity", typed: "", patch: { quantity: undefined } },
|
|
63
|
+
{
|
|
64
|
+
edit: "Days supply set to 14",
|
|
65
|
+
label: "Days supply",
|
|
66
|
+
field: "daysSupply",
|
|
67
|
+
typed: "14",
|
|
68
|
+
patch: { daysSupply: 14 },
|
|
69
|
+
},
|
|
70
|
+
{ edit: "Refills set to 2", label: "Refills", field: "refills", typed: "2", patch: { refillsAllowed: 2 } },
|
|
71
|
+
{ edit: "Unit set to Bottle", label: "Unit", field: "unit", typed: "Bottle", patch: { unit: "Bottle" } },
|
|
72
|
+
{ edit: "Unit cleared", label: "Unit", field: "unit", typed: "", patch: { unit: undefined } },
|
|
73
|
+
] as const)(
|
|
74
|
+
"saves $edit on its own, so it can't overwrite newer values in other fields",
|
|
75
|
+
({ label, field, typed, patch }) => {
|
|
76
|
+
const { save } = renderInputs({ edited: new Set([field]) }, { [field]: typed });
|
|
77
|
+
fireEvent.blur(input(label));
|
|
78
|
+
expect(save).toHaveBeenCalledExactlyOnceWith(field, patch);
|
|
79
|
+
expect(Object.keys(vi.mocked(save).mock.calls[0]![1])).toEqual(Object.keys(patch));
|
|
80
|
+
},
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
it("doesn't save a field I didn't change", () => {
|
|
84
|
+
const { save } = renderInputs();
|
|
85
|
+
fireEvent.blur(input("Quantity"));
|
|
86
|
+
expect(save).not.toHaveBeenCalled();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("when I focus a field", () => {
|
|
91
|
+
it("selects an unedited value, so typing replaces it", () => {
|
|
92
|
+
renderInputs();
|
|
93
|
+
act(() => input("Quantity").focus());
|
|
94
|
+
expect([input("Quantity").selectionStart, input("Quantity").selectionEnd]).toEqual([0, 2]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("doesn't select an edited value", () => {
|
|
98
|
+
renderInputs({ edited: new Set(["quantity"]) }, { quantity: "60" });
|
|
99
|
+
act(() => input("Quantity").focus());
|
|
100
|
+
expect(input("Quantity").selectionStart).toBe(input("Quantity").selectionEnd);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("error messages", () => {
|
|
105
|
+
const labels = ["Quantity", "Unit", "Days supply", "Refills"];
|
|
106
|
+
const noteOf = (label: string) => {
|
|
107
|
+
const note = input(label).getAttribute("aria-describedby");
|
|
108
|
+
return note ? document.getElementById(note)?.textContent : null;
|
|
109
|
+
};
|
|
110
|
+
const isInvalid = (label: string) => input(label).getAttribute("aria-invalid") === "true";
|
|
111
|
+
|
|
112
|
+
it.each([
|
|
113
|
+
{ field: "quantity", label: "Quantity" },
|
|
114
|
+
{ field: "unit", label: "Unit" },
|
|
115
|
+
{ field: "daysSupply", label: "Days supply" },
|
|
116
|
+
{ field: "refills", label: "Refills" },
|
|
117
|
+
] as const)(
|
|
118
|
+
"shows the error message it's given under $label, and marks only that field invalid",
|
|
119
|
+
({ field, label }) => {
|
|
120
|
+
renderInputs({ errors: { [field]: "Must be greater than 0" } });
|
|
121
|
+
expect(isInvalid(label)).toBe(true);
|
|
122
|
+
expect(noteOf(label)).toBe("Must be greater than 0");
|
|
123
|
+
for (const other of labels.filter((l) => l !== label)) expect(isInvalid(other)).toBe(false);
|
|
124
|
+
},
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
it("shows each field's own error message", () => {
|
|
128
|
+
renderInputs({ errors: { quantity: "Required", daysSupply: "Must be a number" } });
|
|
129
|
+
expect(noteOf("Quantity")).toBe("Required");
|
|
130
|
+
expect(noteOf("Days supply")).toBe("Must be a number");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("shows no errors when none are specified", () => {
|
|
134
|
+
renderInputs();
|
|
135
|
+
for (const label of labels) expect(isInvalid(label)).toBe(false);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("shows the field whose save is in flight as busy", () => {
|
|
140
|
+
renderInputs({ spinning: "quantity" });
|
|
141
|
+
expect(input("Quantity").getAttribute("aria-busy")).toBe("true");
|
|
142
|
+
expect(input("Days supply").getAttribute("aria-busy")).toBeNull();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('saves "Dispense as written" as soon as it\'s checked', () => {
|
|
146
|
+
const { save } = renderInputs();
|
|
147
|
+
fireEvent.click(screen.getByRole("checkbox", { name: "Dispense as written" }));
|
|
148
|
+
expect(save).toHaveBeenCalledExactlyOnceWith("daw", { dispenseAsWritten: true });
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
describe("unit", () => {
|
|
152
|
+
it("is typed when the drug has no dispense units", () => {
|
|
153
|
+
renderInputs();
|
|
154
|
+
expect(screen.getByRole("textbox", { name: "Unit" })).toBeTruthy();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("is chosen from the drug's dispense units", () => {
|
|
158
|
+
renderInputs({ units: ["Capsule", "Bottle"] });
|
|
159
|
+
expect(unitOptions()).toEqual(["Capsule", "Bottle"]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("still offers a unit the drug isn't dispensed in", () => {
|
|
163
|
+
renderInputs({ units: ["Tablet", "Bottle"] });
|
|
164
|
+
expect(unitOptions()).toEqual(["Tablet", "Bottle", "Capsule"]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("matches a dispense unit whatever its case", () => {
|
|
168
|
+
renderInputs({ units: ["Capsule", "Bottle"] }, { unit: "capsule" });
|
|
169
|
+
expect(unitOptions()).toEqual(["Capsule", "Bottle"]);
|
|
170
|
+
expect(input("Unit").value).toBe("Capsule");
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("shows and saves a chosen unit right away", () => {
|
|
174
|
+
const { onEdit, save } = renderInputs({ units: ["Capsule", "Bottle"] });
|
|
175
|
+
fireEvent.change(screen.getByRole("combobox", { name: "Unit" }), { target: { value: "Bottle" } });
|
|
176
|
+
expect(onEdit).toHaveBeenCalledWith("unit", "Bottle");
|
|
177
|
+
expect(save).toHaveBeenCalledExactlyOnceWith("unit", { unit: "Bottle" });
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe("Field Interaction", () => {
|
|
182
|
+
afterEach(unmountAndRestoreFetch);
|
|
183
|
+
|
|
184
|
+
it("the unit picked from the drug's units is dispenseUnit: focus, change on the pick, blur", async () => {
|
|
185
|
+
const analytics = recordAnalytics();
|
|
186
|
+
const all: DispenseInputsProps = {
|
|
187
|
+
values: { ...dispense, unit: "" },
|
|
188
|
+
units: ["Capsule", "Tablet"],
|
|
189
|
+
edited: new Set(),
|
|
190
|
+
spinning: null,
|
|
191
|
+
onEdit: vi.fn(),
|
|
192
|
+
save: vi.fn(),
|
|
193
|
+
};
|
|
194
|
+
render(<DispenseInputs {...all} />, { wrapper: withAnalytics(fakeNetworkApiClient(meAsPrescriber).client) });
|
|
195
|
+
|
|
196
|
+
await userEvent.selectOptions(screen.getByRole("combobox", { name: "Unit" }), "Tablet");
|
|
197
|
+
leaveField();
|
|
198
|
+
|
|
199
|
+
await analytics.expectEventLog([...edited("dispenseUnit")]);
|
|
200
|
+
expect(analytics.sentFor("dispenseUnit")).toEqual([
|
|
201
|
+
expect.objectContaining({ hasValue: false, isOptional: false }),
|
|
202
|
+
expect.objectContaining({ hasValue: true }),
|
|
203
|
+
expect.objectContaining({ changed: true }),
|
|
204
|
+
]);
|
|
205
|
+
expect(all.save).toHaveBeenCalledWith("unit", { unit: "Tablet" });
|
|
206
|
+
for (const body of analytics.bodies()) expect(body).not.toContain("Tablet");
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
});
|