@photonhealth/react 2.0.0-rc.2 → 2.0.0-rc.3

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 (179) hide show
  1. package/README.md +17 -1
  2. package/dist/analytics/PhotonAnalyticsProvider.d.ts +5 -2
  3. package/dist/analytics/PhotonAnalyticsProvider.d.ts.map +1 -1
  4. package/dist/analytics/PhotonAnalyticsProvider.js +34 -15
  5. package/dist/analytics/PhotonAnalyticsProvider.js.map +1 -1
  6. package/dist/analytics/cardLifecycle.d.ts +10 -0
  7. package/dist/analytics/cardLifecycle.d.ts.map +1 -0
  8. package/dist/analytics/cardLifecycle.js +43 -0
  9. package/dist/analytics/cardLifecycle.js.map +1 -0
  10. package/dist/analytics/lifecycleOutcomes.d.ts +8 -0
  11. package/dist/analytics/lifecycleOutcomes.d.ts.map +1 -0
  12. package/dist/analytics/lifecycleOutcomes.js +16 -0
  13. package/dist/analytics/lifecycleOutcomes.js.map +1 -0
  14. package/dist/analytics/tracker.d.ts +5 -3
  15. package/dist/analytics/tracker.d.ts.map +1 -1
  16. package/dist/analytics/tracker.js +10 -5
  17. package/dist/analytics/tracker.js.map +1 -1
  18. package/dist/components/PhotonOrder.d.ts +18 -18
  19. package/dist/components/PhotonOrder.d.ts.map +1 -1
  20. package/dist/components/PhotonOrder.js +17 -6
  21. package/dist/components/PhotonOrder.js.map +1 -1
  22. package/dist/components/PhotonPatient.d.ts +7 -35
  23. package/dist/components/PhotonPatient.d.ts.map +1 -1
  24. package/dist/components/PhotonPatient.js +3 -77
  25. package/dist/components/PhotonPatient.js.map +1 -1
  26. package/dist/components/PhotonPrescription.d.ts +14 -14
  27. package/dist/components/PhotonPrescription.d.ts.map +1 -1
  28. package/dist/components/PhotonPrescription.js +0 -1
  29. package/dist/components/PhotonPrescription.js.map +1 -1
  30. package/dist/components/open.d.ts +11 -13
  31. package/dist/components/open.d.ts.map +1 -1
  32. package/dist/components/open.js +7 -2
  33. package/dist/components/open.js.map +1 -1
  34. package/dist/components/patient/PatientCard/PatientCard.d.ts +30 -0
  35. package/dist/components/patient/PatientCard/PatientCard.d.ts.map +1 -0
  36. package/dist/components/patient/PatientCard/PatientCard.js +101 -0
  37. package/dist/components/patient/PatientCard/PatientCard.js.map +1 -0
  38. package/dist/components/patient/PatientCard/usePatientToShow.d.ts +22 -0
  39. package/dist/components/patient/PatientCard/usePatientToShow.d.ts.map +1 -0
  40. package/dist/components/patient/PatientCard/usePatientToShow.js +40 -0
  41. package/dist/components/patient/PatientCard/usePatientToShow.js.map +1 -0
  42. package/dist/components/patient/PatientIntakeForm.d.ts +16 -0
  43. package/dist/components/patient/PatientIntakeForm.d.ts.map +1 -0
  44. package/dist/components/patient/PatientIntakeForm.js +114 -0
  45. package/dist/components/patient/PatientIntakeForm.js.map +1 -0
  46. package/dist/components/patient/PatientSearch.d.ts +12 -0
  47. package/dist/components/patient/PatientSearch.d.ts.map +1 -0
  48. package/dist/components/patient/PatientSearch.js +148 -0
  49. package/dist/components/patient/PatientSearch.js.map +1 -0
  50. package/dist/components/patient/{Benefits.d.ts → PatientSummary/BenefitsInputForm.d.ts} +2 -2
  51. package/dist/components/patient/PatientSummary/BenefitsInputForm.d.ts.map +1 -0
  52. package/dist/components/patient/{Benefits.js → PatientSummary/BenefitsInputForm.js} +5 -5
  53. package/dist/components/patient/PatientSummary/BenefitsInputForm.js.map +1 -0
  54. package/dist/components/patient/{Clinical.d.ts → PatientSummary/ClinicalInputForm.d.ts} +13 -15
  55. package/dist/components/patient/PatientSummary/ClinicalInputForm.d.ts.map +1 -0
  56. package/dist/components/patient/{Clinical.js → PatientSummary/ClinicalInputForm.js} +11 -11
  57. package/dist/components/patient/PatientSummary/ClinicalInputForm.js.map +1 -0
  58. package/dist/components/patient/PatientSummary/CollapsedSummary.d.ts +5 -0
  59. package/dist/components/patient/PatientSummary/CollapsedSummary.d.ts.map +1 -0
  60. package/dist/components/patient/PatientSummary/CollapsedSummary.js +30 -0
  61. package/dist/components/patient/PatientSummary/CollapsedSummary.js.map +1 -0
  62. package/dist/components/patient/PatientSummary/PatientDetailsForm.d.ts +10 -0
  63. package/dist/components/patient/PatientSummary/PatientDetailsForm.d.ts.map +1 -0
  64. package/dist/components/patient/PatientSummary/PatientDetailsForm.js +65 -0
  65. package/dist/components/patient/PatientSummary/PatientDetailsForm.js.map +1 -0
  66. package/dist/components/patient/PatientSummary/PatientDetailsSchema.d.ts +53 -0
  67. package/dist/components/patient/PatientSummary/PatientDetailsSchema.d.ts.map +1 -0
  68. package/dist/components/patient/PatientSummary/PatientDetailsSchema.js +78 -0
  69. package/dist/components/patient/PatientSummary/PatientDetailsSchema.js.map +1 -0
  70. package/dist/components/patient/PatientSummary/PatientSummary.d.ts +46 -0
  71. package/dist/components/patient/PatientSummary/PatientSummary.d.ts.map +1 -0
  72. package/dist/components/patient/PatientSummary/PatientSummary.js +57 -0
  73. package/dist/components/patient/PatientSummary/PatientSummary.js.map +1 -0
  74. package/dist/components/patient/PatientSummary/SummaryHeader.d.ts +19 -0
  75. package/dist/components/patient/PatientSummary/SummaryHeader.d.ts.map +1 -0
  76. package/dist/components/patient/PatientSummary/SummaryHeader.js +22 -0
  77. package/dist/components/patient/PatientSummary/SummaryHeader.js.map +1 -0
  78. package/dist/components/patient/fields.d.ts +7 -25
  79. package/dist/components/patient/fields.d.ts.map +1 -1
  80. package/dist/components/patient/fields.js +11 -53
  81. package/dist/components/patient/fields.js.map +1 -1
  82. package/dist/components/patient/parsePatientSearchQuery.d.ts +16 -0
  83. package/dist/components/patient/parsePatientSearchQuery.d.ts.map +1 -0
  84. package/dist/components/patient/{query.js → parsePatientSearchQuery.js} +3 -15
  85. package/dist/components/patient/parsePatientSearchQuery.js.map +1 -0
  86. package/dist/components/prescription/PrescriptionCard.d.ts +14 -8
  87. package/dist/components/prescription/PrescriptionCard.d.ts.map +1 -1
  88. package/dist/components/prescription/PrescriptionCard.js +10 -3
  89. package/dist/components/prescription/PrescriptionCard.js.map +1 -1
  90. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts +6 -6
  91. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts.map +1 -1
  92. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js +6 -6
  93. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js.map +1 -1
  94. package/dist/components/schemaErrors.d.ts +3 -2
  95. package/dist/components/schemaErrors.d.ts.map +1 -1
  96. package/dist/components/schemaErrors.js +2 -1
  97. package/dist/components/schemaErrors.js.map +1 -1
  98. package/dist/components/search.d.ts +1 -1
  99. package/dist/components/search.js +1 -1
  100. package/dist/index.d.ts +1 -1
  101. package/dist/index.d.ts.map +1 -1
  102. package/dist/index.js +1 -1
  103. package/dist/index.js.map +1 -1
  104. package/dist/meta/version.js +1 -1
  105. package/package.json +5 -2
  106. package/src/analytics/PhotonAnalyticsProvider.test.tsx +0 -1
  107. package/src/analytics/PhotonAnalyticsProvider.tsx +75 -20
  108. package/src/analytics/cardLifecycle.test.tsx +272 -0
  109. package/src/analytics/cardLifecycle.ts +44 -0
  110. package/src/analytics/fieldInteraction.test.tsx +11 -9
  111. package/src/analytics/lifecycleOutcomes.test.ts +78 -0
  112. package/src/analytics/lifecycleOutcomes.ts +29 -0
  113. package/src/analytics/tracker.test.ts +1 -1
  114. package/src/analytics/tracker.ts +13 -6
  115. package/src/analytics/usePhotonAnalytics.test.tsx +106 -0
  116. package/src/components/PhotonOrder.test.tsx +146 -8
  117. package/src/components/PhotonOrder.tsx +37 -26
  118. package/src/components/PhotonPatient.tsx +8 -163
  119. package/src/components/PhotonPrescription.test.tsx +22 -18
  120. package/src/components/PhotonPrescription.tsx +14 -14
  121. package/src/components/ScreeningAlerts.test.tsx +81 -0
  122. package/src/components/open.ts +13 -16
  123. package/src/components/order/DraftOrderPrompt.test.tsx +3 -6
  124. package/src/components/patient/PatientCard/PatientCard.test.tsx +255 -0
  125. package/src/components/patient/PatientCard/PatientCard.tsx +177 -0
  126. package/src/components/patient/PatientCard/usePatientToShow.ts +44 -0
  127. package/src/components/patient/PatientIntakeForm.test.tsx +262 -0
  128. package/src/components/patient/PatientIntakeForm.tsx +222 -0
  129. package/src/components/patient/PatientSearch.test.tsx +204 -0
  130. package/src/components/patient/PatientSearch.tsx +221 -0
  131. package/src/components/patient/PatientSummary/BenefitsInputForm.test.tsx +164 -0
  132. package/src/components/patient/{Benefits.tsx → PatientSummary/BenefitsInputForm.tsx} +4 -4
  133. package/src/components/patient/PatientSummary/ClinicalInputForm.test.tsx +247 -0
  134. package/src/components/patient/{Clinical.tsx → PatientSummary/ClinicalInputForm.tsx} +23 -23
  135. package/src/components/patient/PatientSummary/CollapsedSummary.test.tsx +90 -0
  136. package/src/components/patient/PatientSummary/CollapsedSummary.tsx +46 -0
  137. package/src/components/patient/PatientSummary/PatientDetailsForm.test.tsx +201 -0
  138. package/src/components/patient/PatientSummary/PatientDetailsForm.tsx +320 -0
  139. package/src/components/patient/PatientSummary/PatientDetailsSchema.test.ts +296 -0
  140. package/src/components/patient/PatientSummary/PatientDetailsSchema.ts +95 -0
  141. package/src/components/patient/PatientSummary/PatientSummary.test.tsx +194 -0
  142. package/src/components/patient/PatientSummary/PatientSummary.tsx +225 -0
  143. package/src/components/patient/PatientSummary/SummaryHeader.test.tsx +183 -0
  144. package/src/components/patient/PatientSummary/SummaryHeader.tsx +85 -0
  145. package/src/components/patient/fields.tsx +12 -138
  146. package/src/components/patient/parsePatientSearchQuery.test.ts +107 -0
  147. package/src/components/patient/{query.ts → parsePatientSearchQuery.ts} +4 -21
  148. package/src/components/prescription/PrescriptionCard.test.tsx +174 -12
  149. package/src/components/prescription/PrescriptionCard.tsx +28 -14
  150. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.test.tsx +2 -5
  151. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.test.tsx +3 -6
  152. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.test.tsx +34 -37
  153. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.tsx +14 -14
  154. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.test.tsx +2 -5
  155. package/src/components/prescription/TreatmentSearch/TreatmentSearch.test.tsx +3 -7
  156. package/src/components/schemaErrors.ts +3 -2
  157. package/src/components/search.tsx +1 -1
  158. package/src/index.ts +1 -1
  159. package/src/meta/version.test.ts +1 -0
  160. package/src/meta/version.ts +1 -1
  161. package/dist/components/patient/Benefits.d.ts.map +0 -1
  162. package/dist/components/patient/Benefits.js.map +0 -1
  163. package/dist/components/patient/Clinical.d.ts.map +0 -1
  164. package/dist/components/patient/Clinical.js.map +0 -1
  165. package/dist/components/patient/IntakeForm.d.ts +0 -10
  166. package/dist/components/patient/IntakeForm.d.ts.map +0 -1
  167. package/dist/components/patient/IntakeForm.js +0 -333
  168. package/dist/components/patient/IntakeForm.js.map +0 -1
  169. package/dist/components/patient/Summary.d.ts +0 -48
  170. package/dist/components/patient/Summary.d.ts.map +0 -1
  171. package/dist/components/patient/Summary.js +0 -90
  172. package/dist/components/patient/Summary.js.map +0 -1
  173. package/dist/components/patient/query.d.ts +0 -37
  174. package/dist/components/patient/query.d.ts.map +0 -1
  175. package/dist/components/patient/query.js.map +0 -1
  176. package/src/components/patient/IntakeForm.test.tsx +0 -89
  177. package/src/components/patient/IntakeForm.tsx +0 -564
  178. package/src/components/patient/Summary.test.tsx +0 -106
  179. package/src/components/patient/Summary.tsx +0 -330
@@ -0,0 +1,107 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { parsePatientSearchQuery } from "./parsePatientSearchQuery.ts";
3
+
4
+ describe("parsePatientSearchQuery", () => {
5
+ describe("parse names", () => {
6
+ it("one word: a first name", () => {
7
+ expect(parsePatientSearchQuery("Ada")).toEqual({ name: { first: "Ada" } });
8
+ });
9
+
10
+ it("two words: first name, then last name", () => {
11
+ expect(parsePatientSearchQuery("Ada Lovelace")).toEqual({ name: { first: "Ada", last: "Lovelace" } });
12
+ });
13
+
14
+ it("extra spaces: ignored", () => {
15
+ expect(parsePatientSearchQuery(" Ada Lovelace ")).toEqual({ name: { first: "Ada", last: "Lovelace" } });
16
+ });
17
+
18
+ it("nothing typed: nothing parsed", () => {
19
+ expect(parsePatientSearchQuery(" ")).toEqual({});
20
+ });
21
+
22
+ it("three words: the first word is the first name, the rest is the last name", () => {
23
+ expect(parsePatientSearchQuery("Mary Ann Smith")).toEqual({ name: { first: "Mary", last: "Ann Smith" } });
24
+ });
25
+ });
26
+
27
+ describe("parse dates of birth to YYYY-MM-DD format", () => {
28
+ it("YYYY-MM-DD format: supported", () => {
29
+ expect(parsePatientSearchQuery("1990-01-01")).toEqual({ dateOfBirth: "1990-01-01" });
30
+ });
31
+
32
+ it("M/D/YYYY format: supported", () => {
33
+ expect(parsePatientSearchQuery("1/2/1990")).toEqual({ dateOfBirth: "1990-01-02" });
34
+ });
35
+
36
+ it("MM-DD-YYYY format: supported", () => {
37
+ expect(parsePatientSearchQuery("01-02-1990")).toEqual({ dateOfBirth: "1990-01-02" });
38
+ });
39
+
40
+ // Pins today's behavior
41
+ // TODO: return nothing for impossible dates
42
+ it("an impossible date: still sent as a date", () => {
43
+ expect(parsePatientSearchQuery("13/45/1990")).toEqual({ dateOfBirth: "1990-13-45" });
44
+ });
45
+
46
+ // Pins today's behavior
47
+ // TODO: return nothing for unsupported date formats
48
+ it("a two-digit year or an unpadded YYYY-M-D: not supported read as a name", () => {
49
+ expect(parsePatientSearchQuery("1/1/90")).toEqual({ name: { first: "1/1/90" } });
50
+ expect(parsePatientSearchQuery("1990-1-1")).toEqual({ name: { first: "1990-1-1" } });
51
+ });
52
+ });
53
+
54
+ describe("parse phone numbers to E.164", () => {
55
+ it("10 digits, however punctuated without spaces: supported", () => {
56
+ expect(parsePatientSearchQuery("4155550172")).toEqual({ phone: "+14155550172" });
57
+ expect(parsePatientSearchQuery("415-555-0172")).toEqual({ phone: "+14155550172" });
58
+ expect(parsePatientSearchQuery("(415)555-0172")).toEqual({ phone: "+14155550172" });
59
+ });
60
+
61
+ it("11 digits starting with 1: supported", () => {
62
+ expect(parsePatientSearchQuery("+14155550172")).toEqual({ phone: "+14155550172" });
63
+ });
64
+
65
+ // Pins today's behavior
66
+ // TODO: support a phone typed with spaces, e.g. "(415) 555-0172"
67
+ it("a phone with a space in it: read as a name", () => {
68
+ expect(parsePatientSearchQuery("(415) 555-0172")).toEqual({ name: { first: "(415)", last: "555-0172" } });
69
+ });
70
+
71
+ // Pins today's behavior
72
+ // TODO: decide what an 11-digit number not starting with 1 is
73
+ it("11 digits not starting with 1: sent as typed, not E.164", () => {
74
+ expect(parsePatientSearchQuery("25555550172")).toEqual({ phone: "25555550172" });
75
+ });
76
+
77
+ // Pins today's behavior
78
+ // TODO: return nothing for invalid phone numbers
79
+ it("fewer than 10 digits: read as a name", () => {
80
+ expect(parsePatientSearchQuery("5550172")).toEqual({ name: { first: "5550172" } });
81
+ });
82
+ });
83
+
84
+ describe("parse email", () => {
85
+ it("an email: parsed as is", () => {
86
+ expect(parsePatientSearchQuery("ada@example.com")).toEqual({ email: "ada@example.com" });
87
+ });
88
+ });
89
+
90
+ // TODO: we don't need to support combined inputs if it complicates code implementation
91
+ describe("parse combined inputs", () => {
92
+ it("date of birth with a name, in any order: both parsed", () => {
93
+ const expected = { name: { first: "Ada", last: "Lovelace" }, dateOfBirth: "1990-01-01" };
94
+ expect(parsePatientSearchQuery("Ada Lovelace 1/1/1990")).toEqual(expected);
95
+ expect(parsePatientSearchQuery("1/1/1990 Ada Lovelace")).toEqual(expected);
96
+ });
97
+
98
+ it("name, date of birth, phone and email at once: each part parsed", () => {
99
+ expect(parsePatientSearchQuery("Ada Lovelace 1/1/1990 4155550172 ada@example.com")).toEqual({
100
+ name: { first: "Ada", last: "Lovelace" },
101
+ dateOfBirth: "1990-01-01",
102
+ phone: "+14155550172",
103
+ email: "ada@example.com",
104
+ });
105
+ });
106
+ });
107
+ });
@@ -10,7 +10,7 @@
10
10
  */
11
11
  import { toE164 } from "../format.ts";
12
12
 
13
- export interface ParsedQuery {
13
+ export interface ParsedPatientSearchQuery {
14
14
  name?: { first?: string; last?: string };
15
15
  dateOfBirth?: string;
16
16
  phone?: string;
@@ -37,11 +37,11 @@ function asPhone(token: string): string | undefined {
37
37
 
38
38
  /**
39
39
  * What was typed, as the server takes it. Anything that reads as a date, a phone or an email is taken as
40
- * one; what's left is the name, first word first. A single word is a first name — `searchBoth` says when
40
+ * one; what's left is the name, first word first. A single word is a first name — PatientSearch's `searchBoth` says when
41
41
  * it's worth trying as a last name too.
42
42
  */
43
- export function parseQuery(text: string): ParsedQuery {
44
- const parsed: ParsedQuery = {};
43
+ export function parsePatientSearchQuery(text: string): ParsedPatientSearchQuery {
44
+ const parsed: ParsedPatientSearchQuery = {};
45
45
  const words: string[] = [];
46
46
  for (const token of text.trim().split(/\s+/).filter(Boolean)) {
47
47
  const date = asDate(token);
@@ -64,20 +64,3 @@ export function parseQuery(text: string): ParsedQuery {
64
64
  if (first) parsed.name = rest.length ? { first, last: rest.join(" ") } : { first };
65
65
  return parsed;
66
66
  }
67
-
68
- /** query as patient input */
69
- export const toSearchInput = ({ phone, email, ...demographic }: ParsedQuery) => ({
70
- demographic,
71
- contact: { ...(phone ? { phone } : {}), ...(email ? { email } : {}) },
72
- });
73
-
74
- /** Whether a query says anything the server can match on. */
75
- export const isSearchable = (q: ParsedQuery) =>
76
- Boolean(q.name?.first || q.name?.last || q.dateOfBirth || q.phone || q.email);
77
-
78
- /**
79
- * One word could be either name. It goes as a first name; if nothing comes back, it's worth asking again
80
- * as a last name — "Lovelace" is as likely as "Ada".
81
- */
82
- export const searchBoth = (q: ParsedQuery): ParsedQuery | null =>
83
- q.name?.first && !q.name.last && !q.dateOfBirth && !q.phone && !q.email ? { name: { last: q.name.first } } : null;
@@ -1,13 +1,24 @@
1
- // @vitest-environment jsdom
2
- import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
3
- import { afterEach, describe, expect, it } from "vitest";
4
- import { fakeNetworkApiClient, payload } from "@photonhealth/client/testing";
1
+ import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
2
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
3
+ import { alert, type change, fakeNetworkApiClient, payload, rx } from "@photonhealth/client/testing";
5
4
  import { PrescriptionCard } from "./PrescriptionCard.tsx";
6
- import { fakeNetworkApi, patient, tapDrug, withPhoton } from "../../../test/cards.fixtures.tsx";
5
+ import {
6
+ fakeNetworkApi,
7
+ patient,
8
+ screeningUnavailableChange,
9
+ tapDrug,
10
+ withPhoton,
11
+ } from "../../../test/cards.fixtures.tsx";
12
+ import {
13
+ meAsPrescriber,
14
+ recordAnalytics,
15
+ type RecordedAnalytics,
16
+ unmountAndRestoreFetch,
17
+ withAnalytics,
18
+ } from "../../../test/analytics.fixtures.tsx";
7
19
 
8
- // The prescription card end to end; its parts are tested beside them (TreatmentSearch, PrescriptionDetailsForm).
9
-
10
- afterEach(cleanup);
20
+ const allergyAlertTitle = "allergen · moderate";
21
+ const anyScreeningAlertTitle = /^(allergen|drug|condition) · /;
11
22
 
12
23
  describe("PrescriptionCard", () => {
13
24
  it("finds a drug, picks its form and strength, takes the server's directions, and is signed", async () => {
@@ -36,7 +47,7 @@ describe("PrescriptionCard", () => {
36
47
  expect(((await screen.findByLabelText("Directions (sig)")) as HTMLTextAreaElement).value).toBe("Take as directed");
37
48
  expect((screen.getByLabelText("Quantity") as HTMLInputElement).value).toBe("30");
38
49
 
39
- fireEvent.click(await screen.findByRole("button", { name: "Add" }));
50
+ fireEvent.click(await screen.findByRole("button", { name: "Sign" }));
40
51
  expect(await screen.findByText("Signed")).toBeTruthy();
41
52
  await waitFor(() => expect(screen.queryByLabelText("Directions (sig)")).toBeNull()); // signed is read-only
42
53
  expect(names()).toContain("SignPrescription");
@@ -56,12 +67,12 @@ describe("PrescriptionCard", () => {
56
67
  await screen.findByLabelText("Directions (sig)");
57
68
  fireEvent.change(screen.getByLabelText("Quantity"), { target: { value: "" } });
58
69
 
59
- fireEvent.click(screen.getByRole("button", { name: "Add" }));
70
+ fireEvent.click(screen.getByRole("button", { name: "Sign" }));
60
71
  expect(await screen.findByText("Required")).toBeTruthy();
61
72
  expect(names()).not.toContain("SignPrescription");
62
73
  });
63
74
 
64
- it("offers Add once a product is picked before there's a patient, and says a patient is required", async () => {
75
+ it("offers Sign once a product is picked before there's a patient, and says a patient is required", async () => {
65
76
  const { client, names } = fakeNetworkApiClient({
66
77
  ...fakeNetworkApi({ prescriptions: { amox: [["Amoxicillin", "Capsule", "500 MG"]] } }),
67
78
  });
@@ -77,9 +88,160 @@ describe("PrescriptionCard", () => {
77
88
  fireEvent.change(screen.getByLabelText("Quantity"), { target: { value: "21" } });
78
89
  fireEvent.change(screen.getByLabelText("Days supply"), { target: { value: "7" } });
79
90
 
80
- fireEvent.click(screen.getByRole("button", { name: "Add" }));
91
+ fireEvent.click(screen.getByRole("button", { name: "Sign" }));
81
92
  expect(await screen.findByText("A patient is required for signing")).toBeTruthy();
82
93
  expect(names()).not.toContain("SyncPrescription");
83
94
  expect(names()).not.toContain("SignPrescription");
84
95
  });
85
96
  });
97
+
98
+ describe("PrescriptionCard: screening alerts", () => {
99
+ it("a picked drug's alert shows above its directions, and Sign is still offered", async () => {
100
+ renderCardDraftingAmoxicillin({ screeningAlerts: [alert] });
101
+ await pickAmoxicillin();
102
+
103
+ const alertTitle = await screen.findByText(allergyAlertTitle);
104
+ const directions = await screen.findByLabelText("Directions (sig)");
105
+ expect(alertTitle.compareDocumentPosition(directions) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
106
+ expect(screen.getByText(`${alert.description} (amoxicillin)`)).toBeInTheDocument();
107
+ expect(screen.getByRole("button", { name: "Sign" })).toBeEnabled();
108
+ });
109
+
110
+ it("Sign signs a prescription that has alerts without asking for confirmation", async () => {
111
+ const { names } = renderCardDraftingAmoxicillin({ screeningAlerts: [alert] });
112
+ await pickAmoxicillin();
113
+ await screen.findByText(allergyAlertTitle);
114
+
115
+ fireEvent.click(await screen.findByRole("button", { name: "Sign" }));
116
+
117
+ expect(await screen.findByText("Signed")).toBeInTheDocument();
118
+ expect(names()).toContain("SignPrescription");
119
+ expect(screen.queryByRole("dialog")).toBeNull();
120
+ expect(screen.queryByRole("alertdialog")).toBeNull();
121
+ });
122
+
123
+ it("the alerts are no longer shown once the prescription is signed", async () => {
124
+ renderCardDraftingAmoxicillin({ screeningAlerts: [alert] });
125
+ await pickAmoxicillin();
126
+ await screen.findByText(allergyAlertTitle);
127
+
128
+ fireEvent.click(await screen.findByRole("button", { name: "Sign" }));
129
+
130
+ await screen.findByText("Signed");
131
+ await waitFor(() => expect(screen.queryByText(allergyAlertTitle)).toBeNull());
132
+ });
133
+
134
+ it("a locked prescription that isn't signed still shows its alerts", async () => {
135
+ renderCardOpening(rx({ screeningAlerts: [alert] }), { locked: true });
136
+ expect(await screen.findByText(allergyAlertTitle)).toBeInTheDocument();
137
+ expect(screen.queryByRole("button", { name: "Sign" })).toBeNull();
138
+ });
139
+
140
+ it("a signed prescription opened later shows none of its alerts", async () => {
141
+ renderCardOpening(rx({ screeningAlerts: [alert], signing: { ...rx().signing, state: "SIGNED" } }));
142
+ expect(await screen.findByText("Signed")).toBeInTheDocument();
143
+ expect(screen.queryByText(allergyAlertTitle)).toBeNull();
144
+ });
145
+
146
+ it("screening that failed while drafting shows a dismissible notice in the info tone and no alerts", async () => {
147
+ renderCardDraftingAmoxicillin({ screeningAlerts: null });
148
+ await pickAmoxicillin();
149
+
150
+ const notice = (await screen.findByText("Prescription screening")).closest<HTMLElement>("[data-tone]")!;
151
+ expect(notice).toHaveAttribute("data-tone", "info");
152
+ expect(within(notice).getByText("Prescription screening could not be completed.")).toBeInTheDocument();
153
+ expect(screen.queryByText(anyScreeningAlertTitle)).toBeNull();
154
+
155
+ fireEvent.click(within(notice).getByRole("button", { name: "Dismiss" }));
156
+ expect(screen.queryByText("Prescription screening")).toBeNull();
157
+ });
158
+
159
+ it("screening that failed while drafting doesn't stop Sign from signing", async () => {
160
+ const { names } = renderCardDraftingAmoxicillin({ screeningAlerts: null });
161
+ await pickAmoxicillin();
162
+ await screen.findByText("Prescription screening");
163
+
164
+ fireEvent.click(await screen.findByRole("button", { name: "Sign" }));
165
+
166
+ expect(await screen.findByText("Signed")).toBeInTheDocument();
167
+ expect(names()).toContain("SignPrescription");
168
+ });
169
+
170
+ it("a prescription whose screening failed, opened later, shows neither alerts nor the notice", async () => {
171
+ renderCardOpening(rx({ screeningAlerts: null }), { changes: [screeningUnavailableChange] });
172
+ expect(await screen.findByLabelText("Directions (sig)")).toBeInTheDocument();
173
+ expect(screen.queryByText(anyScreeningAlertTitle)).toBeNull();
174
+ expect(screen.queryByText("Prescription screening")).toBeNull();
175
+ });
176
+ });
177
+
178
+ describe("PrescriptionCard: screening alert analytics", () => {
179
+ let analytics: RecordedAnalytics;
180
+ beforeEach(() => {
181
+ analytics = recordAnalytics();
182
+ });
183
+ afterEach(unmountAndRestoreFetch);
184
+
185
+ it("signing a prescription that has alerts sends no screening alert event: Prescription Signed counts them", async () => {
186
+ const { client } = fakeNetworkApiClient({
187
+ ...meAsPrescriber,
188
+ GetPatient: () => payload("patient", "PatientPayload", patient),
189
+ ...fakeNetworkApi({ prescriptions: amoxicillinSearch, screeningAlerts: [alert] }),
190
+ });
191
+ render(<PrescriptionCard patient="pat_1" />, { wrapper: withAnalytics(client) });
192
+ await pickAmoxicillin();
193
+ await screen.findByText(allergyAlertTitle);
194
+
195
+ fireEvent.click(await screen.findByRole("button", { name: "Sign" }));
196
+ await screen.findByText("Signed");
197
+
198
+ await analytics.expectSentOnce("Field Interaction treatment focus");
199
+ await analytics.expectSentOnce("Field Interaction treatment change");
200
+ await analytics.expectSentOnce("Field Interaction treatment blur");
201
+ await analytics.expectSentOnce("Draft Prescription Added");
202
+ await analytics.expectSentOnce("Prescription Signed");
203
+ await analytics.expectEventCount(5);
204
+ expect(prescriptionSignedSent(analytics)).toMatchObject({ screeningAlertCount: 1 });
205
+ });
206
+ });
207
+
208
+ const amoxicillinSearch = { amox: [["Amoxicillin", "Capsule", "500 MG"]] } satisfies Parameters<
209
+ typeof fakeNetworkApi
210
+ >[0]["prescriptions"];
211
+
212
+ type ScreeningAlertsFromServer = Parameters<typeof fakeNetworkApi>[0]["screeningAlerts"];
213
+
214
+ function renderCardDraftingAmoxicillin({ screeningAlerts }: { screeningAlerts: ScreeningAlertsFromServer }) {
215
+ const { client, names } = fakeNetworkApiClient({
216
+ GetPatient: () => payload("patient", "PatientPayload", patient),
217
+ ...fakeNetworkApi({ prescriptions: amoxicillinSearch, screeningAlerts }),
218
+ });
219
+ render(<PrescriptionCard patient="pat_1" />, { wrapper: withPhoton(client) });
220
+ return { names };
221
+ }
222
+
223
+ function renderCardOpening(
224
+ prescription: ReturnType<typeof rx>,
225
+ { locked, changes = [] }: { locked?: boolean; changes?: ReturnType<typeof change>[] } = {},
226
+ ) {
227
+ const { client } = fakeNetworkApiClient({
228
+ GetPatient: () => payload("patient", "PatientPayload", patient),
229
+ GetPrescription: () => payload("prescription", "PrescriptionPayload", prescription, { changes } as never),
230
+ });
231
+ render(<PrescriptionCard prescription={prescription.id} locked={locked} />, { wrapper: withPhoton(client) });
232
+ }
233
+
234
+ async function pickAmoxicillin() {
235
+ const treatment = await screen.findByLabelText("Treatment");
236
+ treatment.focus();
237
+ fireEvent.change(treatment, { target: { value: "amox" } });
238
+ await tapDrug(/^Amoxicillin/);
239
+ }
240
+
241
+ function prescriptionSignedSent(analytics: RecordedAnalytics) {
242
+ const sent = analytics
243
+ .bodies()
244
+ .map((body) => JSON.parse(body) as { event: string; properties: Record<string, unknown> })
245
+ .find(({ event }) => event === "Prescription Signed");
246
+ return sent?.properties;
247
+ }
@@ -15,13 +15,15 @@ import { useLive, useOnChange } from "../../hooks/index.ts";
15
15
  import { useEffect, useRef, useState, type ReactNode } from "react";
16
16
  import { useHeldActivity } from "../activity.ts";
17
17
  import { SearchResults } from "../search.tsx";
18
- import { NotFound, openFailed, toOpen, useOpened, type Given } from "../open.ts";
18
+ import { NotFound, openFailed, toOpen, useOpened } from "../open.ts";
19
19
  import { treatmentHeading } from "../display.ts";
20
20
  import { dispenseSummary } from "../format.ts";
21
21
  import { ChangeList } from "../ChangeList.tsx";
22
22
  import { PrescriptionDetailsForm } from "./PrescriptionDetailsForm/PrescriptionDetailsForm.tsx";
23
23
  import { ScreeningAlerts } from "../ScreeningAlerts.tsx";
24
24
  import { TreatmentSearch, type TreatmentChoice } from "./TreatmentSearch/TreatmentSearch.tsx";
25
+ import { usePhotonAnalytics } from "../../analytics/PhotonAnalyticsProvider.tsx";
26
+ import { useSendDraftPrescriptionAdded } from "../../analytics/cardLifecycle.ts";
25
27
 
26
28
  /**
27
29
  * One prescription line, bound to a live Prescription: PhotonPrescription's card (which adds the analytics workflow
@@ -29,16 +31,23 @@ import { TreatmentSearch, type TreatmentChoice } from "./TreatmentSearch/Treatme
29
31
  */
30
32
  export interface PrescriptionCardProps {
31
33
  /**
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 } }`).
34
+ * The prescription, one of:
35
+ * - a live `Prescription`: used as is; anything else holding it sees the same changes
36
+ * - its id, `"rx_…"` or `{ id }`: gets the prescription
37
+ * - its id with fields, `{ id, … }`: updates the prescription
38
+ * - fields without an id, e.g. `{ templateId }` or `{ treatment: { name } }`: upserts the prescription
35
39
  */
36
- prescription?: Prescription | Given<PrescriptionInput> | null;
40
+ prescription?: Prescription | PrescriptionInput | string | null;
37
41
  /**
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).
42
+ * Who it's for, one of:
43
+ * - a live `Patient`: used as is
44
+ * - its id, `"pat_…"` or `{ id }`: gets the patient
45
+ * - its id with fields, `{ id, … }`: updates the patient
46
+ * - fields without an id: upserts the patient
47
+ *
48
+ * Until it's known, a new line only looks drugs up (a line from an order already knows).
40
49
  */
41
- patient?: Patient | Given<PatientInput> | null;
50
+ patient?: Patient | PatientInput | string | null;
42
51
  /** Read-only (e.g. on a finalized order). A signed prescription is always read-only. */
43
52
  locked?: boolean;
44
53
  /** The prescriber signed it on this card — once per prescription. */
@@ -74,6 +83,7 @@ export function StatusBadge({ rx }: { rx: PrescriptionRecord | null }) {
74
83
 
75
84
  // One empty list, so a line with no templates doesn't hand PrescriptionDetailsForm new options every render.
76
85
  const NO_TEMPLATES: readonly PrescriptionTemplate[] = [];
86
+ // At module level, not inline: useOpened re-runs its server call when these functions change.
77
87
  const prescriptionOf = (r: PrescriptionResult) => r.prescription;
78
88
  const patientOf = (r: PatientResult) => r.patient;
79
89
 
@@ -109,6 +119,8 @@ export function PrescriptionCard({
109
119
  const live = shared ?? opened ?? own;
110
120
  const snap = useLive(live);
111
121
  useOnChange(live, (rx) => rx.state, onPrescription);
122
+ useSendDraftPrescriptionAdded(live);
123
+ const analytics = usePhotonAnalytics();
112
124
  const signedHere = useRef(new Set<string>()); // onSigned: once per prescription
113
125
  useEffect(() => {
114
126
  if (live && patientId !== undefined)
@@ -118,13 +130,13 @@ export function PrescriptionCard({
118
130
  const rx = snap?.state ?? null;
119
131
  const changes = classify(snap?.changes ?? { applied: [], unapplied: [] });
120
132
  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.
133
+ // The error showing came from Sign, which signs (its title says so); the next save's error is a save's again.
122
134
  const [signFailed, setSignFailed] = useState(false);
123
135
  const busy = signing || snap?.status === "saving" || snap?.status === "loading";
124
136
  // Signing is per prescription, and a signed one is final here: no edits (any change would void the
125
137
  // signature). A different drug or sig is a new line.
126
138
  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
139
+ // Sign (the signature) is offered once the server left nothing open. Screening alerts don't stop it: signing is the
128
140
  // prescriber's acknowledgement of what they show (network-api can't acknowledge an alert yet).
129
141
  const canSign = Boolean(rx) && changes.settled;
130
142
  // The field whose change sent the call in flight shows a spinner — nowhere else, so nothing opens and
@@ -143,6 +155,7 @@ export function PrescriptionCard({
143
155
  };
144
156
  const add = () => {
145
157
  if (!live) return;
158
+ const alertsShownWhenSigning = openAlerts(rx);
146
159
  setSigning(true);
147
160
  setSignFailed(false);
148
161
  void live
@@ -150,6 +163,7 @@ export function PrescriptionCard({
150
163
  .then(() => {
151
164
  if (!live.signed || !live.id || signedHere.current.has(live.id)) return;
152
165
  signedHere.current.add(live.id);
166
+ void analytics.prescriptionSigned({ id: live.id, screeningAlerts: alertsShownWhenSigning });
153
167
  onSigned?.(live);
154
168
  })
155
169
  .catch(() => setSignFailed(true)) // a refusal or failure lands on the object (snap.error)
@@ -299,7 +313,7 @@ export function PrescriptionCard({
299
313
  </Stack>
300
314
  </CardBody>
301
315
  )}
302
- {/* Once a drug is set: the directions first, then quantity / unit / days / refills, then DAW — and Add, which
316
+ {/* Once a drug is set: the directions first, then quantity / unit / days / refills, then DAW — and Sign, which
303
317
  signs it (before a patient too: signing then says a patient is required). */}
304
318
  {!readOnly && (
305
319
  <PrescriptionDetailsForm
@@ -311,9 +325,9 @@ export function PrescriptionCard({
311
325
  spinning={spinning}
312
326
  save={save}
313
327
  status={<StatusBadge rx={rx} />}
314
- onAdd={add}
315
- adding={signing}
316
- addDisabled={snap?.status === "loading" || (Boolean(rx) && !canSign)}
328
+ onSign={add}
329
+ signing={signing}
330
+ signDisabled={snap?.status === "loading" || (Boolean(rx) && !canSign)}
317
331
  />
318
332
  )}
319
333
  </Card>
@@ -1,6 +1,5 @@
1
- // @vitest-environment jsdom
2
- import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
3
- import { afterEach, describe, expect, it, vi } from "vitest";
1
+ import { act, fireEvent, render, screen } from "@testing-library/react";
2
+ import { describe, expect, it, vi } from "vitest";
4
3
  import { DirectionsInput, type DirectionsInputProps } from "./DirectionsInput.tsx";
5
4
 
6
5
  const directions = "Take 1 capsule by mouth 3 times per day";
@@ -19,8 +18,6 @@ const renderInput = (props: Partial<DirectionsInputProps> = {}) => {
19
18
  };
20
19
 
21
20
  describe("DirectionsInput", () => {
22
- afterEach(() => cleanup());
23
-
24
21
  it("shows the directions", () => {
25
22
  const { input } = renderInput();
26
23
  expect(input.value).toBe(directions);
@@ -1,11 +1,10 @@
1
- // @vitest-environment jsdom
2
- import { act, cleanup, fireEvent, render, screen, within } from "@testing-library/react";
1
+ import { act, fireEvent, render, screen, within } from "@testing-library/react";
3
2
  import userEvent from "@testing-library/user-event";
4
3
  import { afterEach, describe, expect, it, vi } from "vitest";
5
4
  import { fakeNetworkApiClient } from "@photonhealth/client/testing";
6
5
  import { DispenseInputs, type DispenseInputsProps, type DispenseValues } from "./DispenseInputs.tsx";
7
6
  import {
8
- edited,
7
+ buildEditedEventsFor,
9
8
  leaveField,
10
9
  meAsPrescriber,
11
10
  recordAnalytics,
@@ -44,8 +43,6 @@ const unitOptions = () =>
44
43
  .map((option) => option.textContent);
45
44
 
46
45
  describe("DispenseInputs", () => {
47
- afterEach(() => cleanup());
48
-
49
46
  it("shows the dispense", () => {
50
47
  renderInputs();
51
48
  expect(input("Quantity").value).toBe("30");
@@ -196,7 +193,7 @@ describe("DispenseInputs", () => {
196
193
  await userEvent.selectOptions(screen.getByRole("combobox", { name: "Unit" }), "Tablet");
197
194
  leaveField();
198
195
 
199
- await analytics.expectEventLog([...edited("dispenseUnit")]);
196
+ await analytics.expectEventLog([...buildEditedEventsFor("dispenseUnit")]);
200
197
  expect(analytics.sentFor("dispenseUnit")).toEqual([
201
198
  expect.objectContaining({ hasValue: false, isOptional: false }),
202
199
  expect.objectContaining({ hasValue: true }),