@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.
Files changed (277) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +111 -2
  3. package/dist/adapters/index.d.ts +2 -0
  4. package/dist/adapters/index.d.ts.map +1 -0
  5. package/dist/adapters/index.js +2 -0
  6. package/dist/adapters/index.js.map +1 -0
  7. package/dist/adapters/tailwind.d.ts +3 -0
  8. package/dist/adapters/tailwind.d.ts.map +1 -0
  9. package/dist/adapters/tailwind.js +126 -0
  10. package/dist/adapters/tailwind.js.map +1 -0
  11. package/dist/analytics/PhotonAnalyticsProvider.d.ts +16 -0
  12. package/dist/analytics/PhotonAnalyticsProvider.d.ts.map +1 -0
  13. package/dist/analytics/PhotonAnalyticsProvider.js +57 -0
  14. package/dist/analytics/PhotonAnalyticsProvider.js.map +1 -0
  15. package/dist/analytics/fieldInteraction.d.ts +18 -0
  16. package/dist/analytics/fieldInteraction.d.ts.map +1 -0
  17. package/dist/analytics/fieldInteraction.js +102 -0
  18. package/dist/analytics/fieldInteraction.js.map +1 -0
  19. package/dist/analytics/tracker.d.ts +17 -0
  20. package/dist/analytics/tracker.d.ts.map +1 -0
  21. package/dist/analytics/tracker.js +36 -0
  22. package/dist/analytics/tracker.js.map +1 -0
  23. package/dist/analytics/viewed.d.ts +11 -0
  24. package/dist/analytics/viewed.d.ts.map +1 -0
  25. package/dist/analytics/viewed.js +16 -0
  26. package/dist/analytics/viewed.js.map +1 -0
  27. package/dist/components/ChangeList.d.ts +21 -0
  28. package/dist/components/ChangeList.d.ts.map +1 -0
  29. package/dist/components/ChangeList.js +40 -0
  30. package/dist/components/ChangeList.js.map +1 -0
  31. package/dist/components/PhotonOrder.d.ts +46 -0
  32. package/dist/components/PhotonOrder.d.ts.map +1 -0
  33. package/dist/components/PhotonOrder.js +274 -0
  34. package/dist/components/PhotonOrder.js.map +1 -0
  35. package/dist/components/PhotonPatient.d.ts +48 -0
  36. package/dist/components/PhotonPatient.d.ts.map +1 -0
  37. package/dist/components/PhotonPatient.js +85 -0
  38. package/dist/components/PhotonPatient.js.map +1 -0
  39. package/dist/components/PhotonPrescription.d.ts +32 -0
  40. package/dist/components/PhotonPrescription.d.ts.map +1 -0
  41. package/dist/components/PhotonPrescription.js +11 -0
  42. package/dist/components/PhotonPrescription.js.map +1 -0
  43. package/dist/components/ScreeningAlerts.d.ts +6 -0
  44. package/dist/components/ScreeningAlerts.d.ts.map +1 -0
  45. package/dist/components/ScreeningAlerts.js +11 -0
  46. package/dist/components/ScreeningAlerts.js.map +1 -0
  47. package/dist/components/SignedIn.d.ts +12 -0
  48. package/dist/components/SignedIn.d.ts.map +1 -0
  49. package/dist/components/SignedIn.js +84 -0
  50. package/dist/components/SignedIn.js.map +1 -0
  51. package/dist/components/activity.d.ts +7 -0
  52. package/dist/components/activity.d.ts.map +1 -0
  53. package/dist/components/activity.js +24 -0
  54. package/dist/components/activity.js.map +1 -0
  55. package/dist/components/display.d.ts +90 -0
  56. package/dist/components/display.d.ts.map +1 -0
  57. package/dist/components/display.js +223 -0
  58. package/dist/components/display.js.map +1 -0
  59. package/dist/components/flow.d.ts +35 -0
  60. package/dist/components/flow.d.ts.map +1 -0
  61. package/dist/components/flow.js +53 -0
  62. package/dist/components/flow.js.map +1 -0
  63. package/dist/components/format.d.ts +26 -0
  64. package/dist/components/format.d.ts.map +1 -0
  65. package/dist/components/format.js +61 -0
  66. package/dist/components/format.js.map +1 -0
  67. package/dist/components/index.d.ts +6 -0
  68. package/dist/components/index.d.ts.map +1 -0
  69. package/dist/components/index.js +6 -0
  70. package/dist/components/index.js.map +1 -0
  71. package/dist/components/open.d.ts +47 -0
  72. package/dist/components/open.d.ts.map +1 -0
  73. package/dist/components/open.js +73 -0
  74. package/dist/components/open.js.map +1 -0
  75. package/dist/components/order/DraftOrderPrompt.d.ts +18 -0
  76. package/dist/components/order/DraftOrderPrompt.d.ts.map +1 -0
  77. package/dist/components/order/DraftOrderPrompt.js +26 -0
  78. package/dist/components/order/DraftOrderPrompt.js.map +1 -0
  79. package/dist/components/patient/Benefits.d.ts +25 -0
  80. package/dist/components/patient/Benefits.d.ts.map +1 -0
  81. package/dist/components/patient/Benefits.js +48 -0
  82. package/dist/components/patient/Benefits.js.map +1 -0
  83. package/dist/components/patient/Clinical.d.ts +56 -0
  84. package/dist/components/patient/Clinical.d.ts.map +1 -0
  85. package/dist/components/patient/Clinical.js +118 -0
  86. package/dist/components/patient/Clinical.js.map +1 -0
  87. package/dist/components/patient/IntakeForm.d.ts +10 -0
  88. package/dist/components/patient/IntakeForm.d.ts.map +1 -0
  89. package/dist/components/patient/IntakeForm.js +333 -0
  90. package/dist/components/patient/IntakeForm.js.map +1 -0
  91. package/dist/components/patient/Summary.d.ts +48 -0
  92. package/dist/components/patient/Summary.d.ts.map +1 -0
  93. package/dist/components/patient/Summary.js +90 -0
  94. package/dist/components/patient/Summary.js.map +1 -0
  95. package/dist/components/patient/fields.d.ts +42 -0
  96. package/dist/components/patient/fields.d.ts.map +1 -0
  97. package/dist/components/patient/fields.js +72 -0
  98. package/dist/components/patient/fields.js.map +1 -0
  99. package/dist/components/patient/query.d.ts +37 -0
  100. package/dist/components/patient/query.d.ts.map +1 -0
  101. package/dist/components/patient/query.js +70 -0
  102. package/dist/components/patient/query.js.map +1 -0
  103. package/dist/components/prescription/PrescriptionCard.d.ts +45 -0
  104. package/dist/components/prescription/PrescriptionCard.d.ts.map +1 -0
  105. package/dist/components/prescription/PrescriptionCard.js +179 -0
  106. package/dist/components/prescription/PrescriptionCard.js.map +1 -0
  107. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts +15 -0
  108. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts.map +1 -0
  109. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js +18 -0
  110. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js.map +1 -0
  111. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts +23 -0
  112. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts.map +1 -0
  113. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js +42 -0
  114. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js.map +1 -0
  115. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts +27 -0
  116. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts.map +1 -0
  117. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js +169 -0
  118. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js.map +1 -0
  119. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts +8 -0
  120. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts.map +1 -0
  121. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js +12 -0
  122. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js.map +1 -0
  123. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts +11 -0
  124. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts.map +1 -0
  125. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js +25 -0
  126. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js.map +1 -0
  127. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts +50 -0
  128. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts.map +1 -0
  129. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js +287 -0
  130. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js.map +1 -0
  131. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts +28 -0
  132. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts.map +1 -0
  133. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js +146 -0
  134. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js.map +1 -0
  135. package/dist/components/schemaErrors.d.ts +12 -0
  136. package/dist/components/schemaErrors.d.ts.map +1 -0
  137. package/dist/components/schemaErrors.js +24 -0
  138. package/dist/components/schemaErrors.js.map +1 -0
  139. package/dist/components/search.d.ts +154 -0
  140. package/dist/components/search.d.ts.map +1 -0
  141. package/dist/components/search.js +295 -0
  142. package/dist/components/search.js.map +1 -0
  143. package/dist/hooks/index.d.ts +5 -0
  144. package/dist/hooks/index.d.ts.map +1 -0
  145. package/dist/hooks/index.js +5 -0
  146. package/dist/hooks/index.js.map +1 -0
  147. package/dist/hooks/useLatestCallback.d.ts +7 -0
  148. package/dist/hooks/useLatestCallback.d.ts.map +1 -0
  149. package/dist/hooks/useLatestCallback.js +14 -0
  150. package/dist/hooks/useLatestCallback.js.map +1 -0
  151. package/dist/hooks/useLive.d.ts +15 -0
  152. package/dist/hooks/useLive.d.ts.map +1 -0
  153. package/dist/hooks/useLive.js +6 -0
  154. package/dist/hooks/useLive.js.map +1 -0
  155. package/dist/hooks/useOnChange.d.ts +11 -0
  156. package/dist/hooks/useOnChange.d.ts.map +1 -0
  157. package/dist/hooks/useOnChange.js +28 -0
  158. package/dist/hooks/useOnChange.js.map +1 -0
  159. package/dist/hooks/useSession.d.ts +7 -0
  160. package/dist/hooks/useSession.d.ts.map +1 -0
  161. package/dist/hooks/useSession.js +10 -0
  162. package/dist/hooks/useSession.js.map +1 -0
  163. package/dist/index.d.ts +22 -0
  164. package/dist/index.d.ts.map +1 -0
  165. package/dist/index.js +20 -0
  166. package/dist/index.js.map +1 -0
  167. package/dist/meta/version.d.ts +5 -0
  168. package/dist/meta/version.d.ts.map +1 -0
  169. package/dist/meta/version.js +5 -0
  170. package/dist/meta/version.js.map +1 -0
  171. package/dist/primitives/PhotonProvider.d.ts +60 -0
  172. package/dist/primitives/PhotonProvider.d.ts.map +1 -0
  173. package/dist/primitives/PhotonProvider.js +103 -0
  174. package/dist/primitives/PhotonProvider.js.map +1 -0
  175. package/dist/primitives/defaults.d.ts +3 -0
  176. package/dist/primitives/defaults.d.ts.map +1 -0
  177. package/dist/primitives/defaults.js +70 -0
  178. package/dist/primitives/defaults.js.map +1 -0
  179. package/dist/primitives/icons.d.ts +6 -0
  180. package/dist/primitives/icons.d.ts.map +1 -0
  181. package/dist/primitives/icons.js +6 -0
  182. package/dist/primitives/icons.js.map +1 -0
  183. package/dist/primitives/index.d.ts +5 -0
  184. package/dist/primitives/index.d.ts.map +1 -0
  185. package/dist/primitives/index.js +5 -0
  186. package/dist/primitives/index.js.map +1 -0
  187. package/dist/primitives/photonContext.d.ts +3 -0
  188. package/dist/primitives/photonContext.d.ts.map +1 -0
  189. package/dist/primitives/photonContext.js +3 -0
  190. package/dist/primitives/photonContext.js.map +1 -0
  191. package/dist/primitives/select.d.ts +7 -0
  192. package/dist/primitives/select.d.ts.map +1 -0
  193. package/dist/primitives/select.js +16 -0
  194. package/dist/primitives/select.js.map +1 -0
  195. package/dist/primitives/types.d.ts +288 -0
  196. package/dist/primitives/types.d.ts.map +1 -0
  197. package/dist/primitives/types.js +3 -0
  198. package/dist/primitives/types.js.map +1 -0
  199. package/dist/primitives/useProvidedPhoton.d.ts +8 -0
  200. package/dist/primitives/useProvidedPhoton.d.ts.map +1 -0
  201. package/dist/primitives/useProvidedPhoton.js +8 -0
  202. package/dist/primitives/useProvidedPhoton.js.map +1 -0
  203. package/dist/styles.css +653 -0
  204. package/dist/test-utils/contract.d.ts +17 -0
  205. package/dist/test-utils/contract.d.ts.map +1 -0
  206. package/dist/test-utils/contract.js +282 -0
  207. package/dist/test-utils/contract.js.map +1 -0
  208. package/package.json +76 -4
  209. package/src/adapters/index.ts +1 -0
  210. package/src/adapters/tailwind.tsx +572 -0
  211. package/src/analytics/PhotonAnalyticsProvider.test.tsx +191 -0
  212. package/src/analytics/PhotonAnalyticsProvider.tsx +96 -0
  213. package/src/analytics/fieldInteraction.test.tsx +184 -0
  214. package/src/analytics/fieldInteraction.tsx +148 -0
  215. package/src/analytics/tracker.test.ts +52 -0
  216. package/src/analytics/tracker.ts +42 -0
  217. package/src/analytics/viewed.ts +27 -0
  218. package/src/components/ChangeList.tsx +85 -0
  219. package/src/components/PhotonOrder.test.tsx +256 -0
  220. package/src/components/PhotonOrder.tsx +508 -0
  221. package/src/components/PhotonPatient.tsx +188 -0
  222. package/src/components/PhotonPrescription.test.tsx +116 -0
  223. package/src/components/PhotonPrescription.tsx +45 -0
  224. package/src/components/ScreeningAlerts.tsx +28 -0
  225. package/src/components/SignedIn.tsx +144 -0
  226. package/src/components/activity.ts +23 -0
  227. package/src/components/display.ts +278 -0
  228. package/src/components/flow.ts +67 -0
  229. package/src/components/format.ts +73 -0
  230. package/src/components/index.ts +5 -0
  231. package/src/components/open.ts +100 -0
  232. package/src/components/order/DraftOrderPrompt.test.tsx +80 -0
  233. package/src/components/order/DraftOrderPrompt.tsx +81 -0
  234. package/src/components/patient/Benefits.tsx +133 -0
  235. package/src/components/patient/Clinical.tsx +215 -0
  236. package/src/components/patient/IntakeForm.test.tsx +89 -0
  237. package/src/components/patient/IntakeForm.tsx +564 -0
  238. package/src/components/patient/Summary.test.tsx +106 -0
  239. package/src/components/patient/Summary.tsx +330 -0
  240. package/src/components/patient/fields.tsx +158 -0
  241. package/src/components/patient/query.ts +83 -0
  242. package/src/components/prescription/PrescriptionCard.test.tsx +85 -0
  243. package/src/components/prescription/PrescriptionCard.tsx +347 -0
  244. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.test.tsx +85 -0
  245. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.tsx +41 -0
  246. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.test.tsx +209 -0
  247. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.tsx +99 -0
  248. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.test.tsx +456 -0
  249. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.tsx +238 -0
  250. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.test.ts +63 -0
  251. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.ts +15 -0
  252. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.test.tsx +75 -0
  253. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.tsx +41 -0
  254. package/src/components/prescription/TreatmentSearch/TreatmentPicker.tsx +375 -0
  255. package/src/components/prescription/TreatmentSearch/TreatmentSearch.test.tsx +699 -0
  256. package/src/components/prescription/TreatmentSearch/TreatmentSearch.tsx +220 -0
  257. package/src/components/schemaErrors.test.ts +49 -0
  258. package/src/components/schemaErrors.ts +33 -0
  259. package/src/components/search.tsx +377 -0
  260. package/src/hooks/index.ts +4 -0
  261. package/src/hooks/useLatestCallback.ts +16 -0
  262. package/src/hooks/useLive.ts +21 -0
  263. package/src/hooks/useOnChange.ts +34 -0
  264. package/src/hooks/useSession.ts +11 -0
  265. package/src/index.ts +21 -0
  266. package/src/meta/version.test.ts +10 -0
  267. package/src/meta/version.ts +4 -0
  268. package/src/primitives/PhotonProvider.tsx +168 -0
  269. package/src/primitives/defaults.tsx +432 -0
  270. package/src/primitives/icons.tsx +37 -0
  271. package/src/primitives/index.ts +4 -0
  272. package/src/primitives/photonContext.ts +4 -0
  273. package/src/primitives/select.ts +15 -0
  274. package/src/primitives/types.ts +288 -0
  275. package/src/primitives/useProvidedPhoton.ts +21 -0
  276. package/src/styles.css +653 -0
  277. package/src/test-utils/contract.tsx +453 -0
@@ -0,0 +1,42 @@
1
+ import type { AnalyticsProperties, Photon } from "@photonhealth/client";
2
+
3
+ export type { AnalyticsProperties };
4
+
5
+ export interface QueuedEvent {
6
+ event: string;
7
+ properties: AnalyticsProperties;
8
+ }
9
+
10
+ export const MAX_QUEUED_EVENTS = 50;
11
+
12
+ /** Holds the cards' events until the prescriber's sign-in is accepted, then sends them with `ph.analytics.track`. */
13
+ export class Tracker {
14
+ #ready = false;
15
+ #queue: QueuedEvent[] = [];
16
+
17
+ constructor(private readonly ph: Photon) {}
18
+
19
+ setReady(ready: boolean) {
20
+ this.#ready = ready;
21
+ if (ready) this.#flush();
22
+ }
23
+
24
+ track(queued: QueuedEvent) {
25
+ if (!this.ph.analytics) return;
26
+ if (this.#queue.length >= MAX_QUEUED_EVENTS) return;
27
+ this.#queue.push(queued);
28
+ if (this.#ready) this.#flush();
29
+ }
30
+
31
+ #flush() {
32
+ for (const { event, properties } of this.#queue.splice(0)) void this.ph.analytics?.track(event, properties);
33
+ }
34
+ }
35
+
36
+ const trackersByPhoton = new WeakMap<Photon, Tracker>();
37
+
38
+ export function trackerFor(ph: Photon): Tracker {
39
+ let tracker = trackersByPhoton.get(ph);
40
+ if (!tracker) trackersByPhoton.set(ph, (tracker = new Tracker(ph)));
41
+ return tracker;
42
+ }
@@ -0,0 +1,27 @@
1
+ import type { AnalyticsProperties } from "./tracker.ts";
2
+
3
+ interface CardProps {
4
+ order?: unknown;
5
+ patient?: unknown;
6
+ prescription?: unknown;
7
+ locked?: boolean;
8
+ fixed?: boolean;
9
+ }
10
+
11
+ export function viewedPropertiesFromCardProps(props: CardProps): AnalyticsProperties {
12
+ const order = asObject(props.order);
13
+ const linesToAdd = asObject(order?.prescriptions)?.add;
14
+ const lines = Array.isArray(linesToAdd) ? linesToAdd.map(asObject) : [];
15
+ return {
16
+ hasOrderPrefill: props.order != null,
17
+ hasPatientPrefill: props.patient != null || order?.patient != null,
18
+ hasPrescriptionPrefill: props.prescription != null,
19
+ numTemplateIds: lines.filter((line) => typeof line?.templateId === "string").length,
20
+ numPrescriptionIds: lines.filter((line) => typeof line?.id === "string").length,
21
+ locked: Boolean(props.locked),
22
+ fixed: Boolean(props.fixed),
23
+ };
24
+ }
25
+
26
+ const asObject = (value: unknown): Record<string, unknown> | null =>
27
+ value && typeof value === "object" ? (value as Record<string, unknown>) : null;
@@ -0,0 +1,85 @@
1
+ import { useComponents } from "../primitives/index.ts";
2
+ import type { ChangeOption, ClassifiedChanges } from "@photonhealth/client";
3
+ import { orderedOptions } from "./display.ts";
4
+ import { SearchResults, type SearchHandle } from "./search.tsx";
5
+ import { useState } from "react";
6
+
7
+ /**
8
+ * Renders any ChangeSet the same way in every component:
9
+ * needsChoice → the server's options as search results (closest first; onChoose re-sends)
10
+ * blocking → critical, with the server's reason
11
+ * advisories → dismissible info (COST_SAVINGS, PRODUCT_PREFERENCE, …)
12
+ * Applied INFO changes are not shown — they already took effect. `handleRef` reaches the first choice's
13
+ * results — from the field that asked (e.g. Add allergy), so its keys work on them.
14
+ */
15
+ export function ChangeList({
16
+ changes,
17
+ onChoose,
18
+ busy,
19
+ handleRef,
20
+ onBack,
21
+ }: {
22
+ changes: ClassifiedChanges;
23
+ onChoose?: (option: ChangeOption) => void;
24
+ busy?: boolean;
25
+ handleRef?: { current: SearchHandle | null };
26
+ /** Esc in the first choice's results: back to the field that asked. */
27
+ onBack?: () => void;
28
+ }) {
29
+ const { Alert, Stack } = useComponents();
30
+ const [dismissed, setDismissed] = useState<Set<string>>(new Set());
31
+ // The server can report the same problem once per affected field; show it once.
32
+ const unique = <T extends { key: string; label: string; reason: string | null }>(list: T[]) => {
33
+ const seen = new Set<string>();
34
+ return list.filter((c) => {
35
+ const id = `${c.label}|${c.reason ?? ""}`;
36
+ return seen.has(id) ? false : (seen.add(id), true);
37
+ });
38
+ };
39
+ const needsChoice = unique(changes.needsChoice);
40
+ const blocking = unique(changes.blocking);
41
+ const advisories = unique(changes.advisories).filter((c) => !dismissed.has(c.key));
42
+ if (!needsChoice.length && !blocking.length && !advisories.length) return null;
43
+
44
+ return (
45
+ <Stack gap="2">
46
+ {needsChoice.map((change, i) => {
47
+ // Only options that say where their pick goes can be sent back.
48
+ const options = orderedOptions({ options: change.options.filter((o) => o.path) }, change.inputText);
49
+ return (
50
+ <SearchResults
51
+ key={`${change.key}-${i}`}
52
+ label={change.label}
53
+ busy={busy}
54
+ disabled={!onChoose}
55
+ results={options.map((o, j) => ({
56
+ key: String(j),
57
+ label: o.displayName,
58
+ description: o.reason ?? undefined,
59
+ }))}
60
+ onPick={(key) => onChoose?.(options[Number(key)]!)}
61
+ handleRef={i === 0 ? handleRef : undefined}
62
+ onBack={i === 0 ? onBack : undefined}
63
+ />
64
+ );
65
+ })}
66
+ {blocking.map((change, i) => (
67
+ <Alert
68
+ key={`${change.key}-${i}`}
69
+ tone="critical"
70
+ title={change.label}
71
+ description={change.reason ?? undefined}
72
+ />
73
+ ))}
74
+ {advisories.map((change) => (
75
+ <Alert
76
+ key={change.key}
77
+ tone="info"
78
+ title={change.label}
79
+ description={change.reason ?? undefined}
80
+ onDismiss={() => setDismissed((d) => new Set(d).add(change.key))}
81
+ />
82
+ ))}
83
+ </Stack>
84
+ );
85
+ }
@@ -0,0 +1,256 @@
1
+ // @vitest-environment jsdom
2
+ import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
3
+ import { afterEach, describe, expect, it } from "vitest";
4
+ import {
5
+ change,
6
+ fakeNetworkApiClient,
7
+ listedOrder,
8
+ orders,
9
+ payload,
10
+ rx,
11
+ treatment,
12
+ } from "@photonhealth/client/testing";
13
+ import { PhotonOrder, type PhotonOrderProps } from "../index.ts";
14
+ import {
15
+ choices,
16
+ draftOrder,
17
+ option,
18
+ order,
19
+ patient,
20
+ pickDrug,
21
+ typeIdentity,
22
+ withPhoton,
23
+ } from "../../test/cards.fixtures.tsx";
24
+
25
+ // The order around its prescriptions: Send, Remove, and a line searched before the order has a patient.
26
+
27
+ afterEach(cleanup);
28
+
29
+ const lisinoprilTablets10And20Mg = choices(
30
+ option("Lisinopril Oral Tablet 10 MG", "med_10"),
31
+ option("Lisinopril Oral Tablet 20 MG", "med_20"),
32
+ );
33
+
34
+ describe("PhotonOrder prescriptions", () => {
35
+ it("a picked product waits for Add before the order can be sent; Remove leaves a fresh line to search in", async () => {
36
+ const component = renderComponent({
37
+ lookupPrescriptions: lisinoprilTablets10And20Mg,
38
+ script: {
39
+ SyncPrescription: () =>
40
+ payload(
41
+ "prescription",
42
+ "PrescriptionPayload",
43
+ rx({ id: "rx_lis", treatment: { ...rx().treatment, id: "med_10", name: "Lisinopril", ndc: null } }),
44
+ ),
45
+ },
46
+ });
47
+ await component.typeIntoTreatment("lisinopril");
48
+ await pickDrug("Lisinopril Oral Tablet 10 MG");
49
+ await screen.findByLabelText("Directions (sig)");
50
+ // No primary yet: Send waits for Add.
51
+ expect(document.querySelectorAll('.prx-button[data-variant="primary"]')).toHaveLength(0);
52
+ expect(screen.queryByRole("button", { name: /^Send prescriptions? to patient$/ })).toBeNull();
53
+
54
+ fireEvent.click(screen.getByRole("button", { name: "Remove prescription" }));
55
+ expect((await component.findTreatmentField()).value).toBe("");
56
+ expect(screen.queryByRole("heading", { name: "Lisinopril" })).toBeNull();
57
+ expect(screen.queryByRole("button", { name: "Remove prescription" })).toBeNull(); // nothing to take off yet
58
+ });
59
+
60
+ it("while a line chooses a drug, ⌘↵ doesn't send — the order would go without that line", async () => {
61
+ const component = renderComponent({
62
+ photonOrderProps: { order: "ord_1" },
63
+ lookupPrescriptions: lisinoprilTablets10And20Mg,
64
+ script: {
65
+ GetOrder: () => payload("order", "OrderPayload", order()),
66
+ SubmitOrder: () => payload("order", "OrderPayload", order("SUBMITTED")),
67
+ },
68
+ });
69
+ const send = await screen.findByRole("button", { name: /^Send prescriptions? to patient$/ });
70
+ await waitFor(() => expect(send).toHaveProperty("disabled", false)); // the one line is signed
71
+
72
+ fireEvent.click(screen.getByRole("button", { name: "Add another prescription" }));
73
+ const meds = await screen.findAllByLabelText("Treatment");
74
+ fireEvent.change(meds.at(-1)!, { target: { value: "lisinopril" } });
75
+ await screen.findByRole("list", { name: "Pick a treatment" });
76
+ expect(screen.queryByRole("button", { name: /^Send prescriptions? to patient$/ })).toBeNull();
77
+ fireEvent.keyDown(window, { key: "Enter", metaKey: true });
78
+ fireEvent.keyDown(window, { key: "Enter", ctrlKey: true });
79
+ await new Promise((resolve) => setTimeout(resolve, 0)); // a send would have started by now
80
+ expect(component.names()).not.toContain("SubmitOrder");
81
+ });
82
+
83
+ it("searches drugs before a patient is set (never drafts), then attaches the pick once a patient is chosen", async () => {
84
+ const metformin = payload("prescription", "PrescriptionPayload", null, {
85
+ changes: [
86
+ // Expected without a patient, and not shown.
87
+ change({
88
+ key: "patient",
89
+ label: "Patient",
90
+ status: "REJECTED",
91
+ severity: "BLOCKING",
92
+ reason: "patient is required.",
93
+ }),
94
+ change({
95
+ status: "AMBIGUOUS",
96
+ severity: "BLOCKING",
97
+ category: "DISAMBIGUATION",
98
+ options: [
99
+ option("metFORMIN HCl Oral Tablet 500 MG", "med_met500"),
100
+ option("metFORMIN HCl Oral Tablet 1000 MG", "med_met1000"),
101
+ ],
102
+ }),
103
+ ],
104
+ } as never);
105
+ const component = renderComponent({
106
+ photonOrderProps: {},
107
+ lookupPrescriptions: metformin,
108
+ script: {
109
+ SyncPatient: () => ({
110
+ patient: {
111
+ __typename: "AmbiguousPatientMatch",
112
+ reason: "weak",
113
+ candidates: [{ id: "pat_1", firstName: "Ada", lastName: "Lovelace", dateOfBirth: "1990-01-01" }],
114
+ },
115
+ }),
116
+ SyncPrescription: () => payload("prescription", "PrescriptionPayload", rx({ id: "rx_attached" })),
117
+ },
118
+ });
119
+
120
+ // No patient yet — the line is already there and searches.
121
+ const med = await component.findTreatmentField();
122
+ expect(screen.queryByLabelText("Quantity")).toBeNull(); // dispense details wait for a drug
123
+ expect(screen.queryByLabelText("Directions (sig)")).toBeNull();
124
+ expect(screen.queryByRole("button", { name: "Add another prescription" })).toBeNull(); // not until it has a drug
125
+ fireEvent.change(med, { target: { value: "metformin" } });
126
+ fireEvent.keyDown(med, { key: "Enter" });
127
+ await pickDrug("metFORMIN HCl Oral Tablet 500 MG");
128
+ expect(await screen.findByRole("heading", { name: "metFORMIN HCl" })).toBeTruthy();
129
+ expect(screen.queryByText("patient is required.")).toBeNull();
130
+ expect(screen.getByRole("button", { name: "Add another prescription" })).toBeTruthy();
131
+ // Directions (sig), then quantity / unit / days / refills, then DAW.
132
+ const [sig, qty, daw] = ["Directions (sig)", "Quantity", "Dispense as written"].map((l) =>
133
+ screen.getByLabelText(l),
134
+ );
135
+ expect(sig!.compareDocumentPosition(qty!) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
136
+ expect(qty!.compareDocumentPosition(daw!) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
137
+
138
+ fireEvent.change(sig!, { target: { value: "1 tab PO BID" } });
139
+ fireEvent.blur(sig!);
140
+ expect(component.wasPrescriptionDrafted()).toBe(false); // nothing drafted without a patient
141
+
142
+ await typeIdentity("Ada", "Lovelace", "1990-01-01");
143
+ fireEvent.click(await screen.findByRole("option", { name: /Ada Lovelace/ }));
144
+ expect(await component.waitForDraftedPrescription()).toMatchObject({
145
+ patient: { id: "pat_1" },
146
+ treatment: { id: "med_met500" },
147
+ instructions: "1 tab PO BID",
148
+ });
149
+ expect(component.calls.filter((c) => c.name === "LookupPrescription").every((c) => !("patient" in c.vars))).toBe(
150
+ true,
151
+ );
152
+ });
153
+ });
154
+
155
+ describe("PhotonOrder and the patient's draft order", () => {
156
+ const metoprolol = rx({ id: "rx_9", treatment: treatment({ id: "trt_9", name: "Metoprolol Tartrate" }) });
157
+ const withDraft: NetworkScript = {
158
+ Orders: () => orders([listedOrder({ id: "ord_9", prescriptions: [metoprolol] })]),
159
+ GetOrder: () => payload("order", "OrderPayload", { ...order(), id: "ord_9", prescriptions: [metoprolol] }),
160
+ GetPrescription: () => payload("prescription", "PrescriptionPayload", metoprolol),
161
+ UpdateOrder: () => payload("order", "OrderPayload", { ...order(), id: "ord_9", prescriptions: [] }),
162
+ };
163
+
164
+ it("shows the draft's prescriptions read-only and asks first; Continue makes them the order's lines", async () => {
165
+ const component = renderComponent({ script: withDraft });
166
+ expect(await screen.findByText("Would you like to continue this order?")).toBeTruthy();
167
+ expect(screen.getByRole("heading", { name: "Metoprolol Tartrate" })).toBeTruthy();
168
+ expect(screen.queryByLabelText("Treatment")).toBeNull(); // nothing to write in until the prescriber decides
169
+ expect(screen.queryByRole("button", { name: "Remove prescription" })).toBeNull();
170
+
171
+ fireEvent.click(screen.getByRole("button", { name: "Continue" }));
172
+ // The draft as it is, with a way to add to it — no empty line in the way.
173
+ expect(await screen.findByRole("button", { name: "Add another prescription" })).toBeTruthy();
174
+ expect(screen.queryByText("Would you like to continue this order?")).toBeNull();
175
+ expect(screen.getByRole("heading", { name: "Metoprolol Tartrate" })).toBeTruthy();
176
+ expect(screen.queryByLabelText("Treatment")).toBeNull();
177
+ expect(component.names()).toContain("GetOrder");
178
+ expect(component.names()).not.toContain("SyncOrder");
179
+ });
180
+
181
+ it("a continued draft whose prescriptions are signed can be sent as it is", async () => {
182
+ const signedMetoprolol = { ...metoprolol, signing: { ...metoprolol.signing, state: "SIGNED" as const } };
183
+ renderComponent({
184
+ script: {
185
+ ...withDraft,
186
+ Orders: () => orders([listedOrder({ id: "ord_9", prescriptions: [signedMetoprolol] })]),
187
+ GetOrder: () =>
188
+ payload("order", "OrderPayload", { ...order(), id: "ord_9", prescriptions: [signedMetoprolol] }),
189
+ GetPrescription: () => payload("prescription", "PrescriptionPayload", signedMetoprolol),
190
+ },
191
+ });
192
+ fireEvent.click(await screen.findByRole("button", { name: "Continue" }));
193
+ expect(await screen.findByRole("button", { name: "Send prescription to patient" })).toBeTruthy();
194
+ expect(screen.getByRole("button", { name: "Add another prescription" })).toBeTruthy();
195
+ });
196
+
197
+ it("a draft with no prescriptions isn't shown: the order starts with its empty line", async () => {
198
+ const component = renderComponent({
199
+ script: { ...withDraft, Orders: () => orders([listedOrder({ id: "ord_9", prescriptions: [] })]) },
200
+ });
201
+ await component.findTreatmentField();
202
+ await waitFor(() => expect(component.names()).toContain("Orders"));
203
+ expect(screen.queryByText("Would you like to continue this order?")).toBeNull();
204
+ });
205
+
206
+ it("Start a new order empties the draft and goes on with an empty line", async () => {
207
+ const component = renderComponent({ script: withDraft });
208
+ fireEvent.click(await screen.findByRole("button", { name: "Start a new order" }));
209
+ await component.findTreatmentField();
210
+ expect(screen.queryByText("Would you like to continue this order?")).toBeNull();
211
+ expect(screen.queryByRole("heading", { name: "Metoprolol Tartrate" })).toBeNull();
212
+ expect(component.calls.find((c) => c.op === "UpdateOrder")?.vars).toMatchObject({ prescriptions: { none: true } });
213
+ expect(component.names()).not.toContain("GetOrder");
214
+ });
215
+ });
216
+
217
+ type NetworkScript = Parameters<typeof fakeNetworkApiClient>[0];
218
+
219
+ type RenderComponentOptions = {
220
+ photonOrderProps?: PhotonOrderProps;
221
+ lookupPrescriptions?: unknown;
222
+ script?: NetworkScript;
223
+ };
224
+
225
+ function renderComponent({
226
+ photonOrderProps = { patient: "pat_1" },
227
+ lookupPrescriptions = null,
228
+ script = {},
229
+ }: RenderComponentOptions = {}) {
230
+ const { client, calls, names } = fakeNetworkApiClient({
231
+ ...draftOrder(),
232
+ GetPatient: () => payload("patient", "PatientPayload", patient),
233
+ LookupPrescription: () => lookupPrescriptions,
234
+ SyncPrescription: () => payload("prescription", "PrescriptionPayload", rx({ id: "rx_1" })),
235
+ ...script,
236
+ });
237
+ render(<PhotonOrder {...photonOrderProps} />, { wrapper: withPhoton(client) });
238
+
239
+ const findTreatmentField = () => screen.findByLabelText("Treatment") as Promise<HTMLInputElement>;
240
+ return {
241
+ calls,
242
+ names,
243
+ findTreatmentField,
244
+ typeIntoTreatment: async (text: string) => {
245
+ const med = await findTreatmentField();
246
+ med.focus();
247
+ fireEvent.change(med, { target: { value: text } });
248
+ return med;
249
+ },
250
+ wasPrescriptionDrafted: () => names().includes("SyncPrescription"),
251
+ waitForDraftedPrescription: async () => {
252
+ await waitFor(() => expect(names()).toContain("SyncPrescription"));
253
+ return calls.find((c) => c.name === "SyncPrescription")!.vars;
254
+ },
255
+ };
256
+ }