@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,201 @@
1
+ import { render, screen } from "@testing-library/react";
2
+ import userEvent from "@testing-library/user-event";
3
+ import { describe, expect, it, vi, type Mock } from "vitest";
4
+ import type { PatientRecord } from "@photonhealth/client";
5
+ import { PatientDetailsForm, type PatientDetailsInput } from "./PatientDetailsForm.tsx";
6
+ import { patientRecord as record } from "../../../../test/patientRecord.fixtures.ts";
7
+
8
+ type Contact = NonNullable<PatientRecord["contact"]>;
9
+
10
+ /** Ada Lovelace, born 1990-01-01, female, with the contact details a test passes. */
11
+ const ada = (contact: Partial<Contact> | null = null): PatientRecord => ({
12
+ ...record({ demographic: { dateOfBirth: "1990-01-01", sex: "FEMALE" } }),
13
+ contact: contact && { phone: null, email: null, address: null, ...contact },
14
+ });
15
+
16
+ const address = { street1: "1 Main St", street2: null, city: "Boston", state: "MA", postalCode: "02101" };
17
+
18
+ const renderForm = ({
19
+ patient = ada(),
20
+ addressRequest = null,
21
+ }: { patient?: PatientRecord; addressRequest?: string | null } = {}) => {
22
+ const onSave = vi.fn();
23
+ const user = userEvent.setup();
24
+ render(<PatientDetailsForm patient={patient} addressRequest={addressRequest} onSave={onSave} />);
25
+ return { user, onSave };
26
+ };
27
+
28
+ const field = (label: string) => screen.getByLabelText(label);
29
+
30
+ /** Replace a field's text, then move the cursor out of it. */
31
+ const retype = async (user: ReturnType<typeof userEvent.setup>, label: string, text: string) => {
32
+ await user.clear(field(label));
33
+ if (text) await user.type(field(label), text);
34
+ await user.tab();
35
+ };
36
+
37
+ /** What the one call to onSave sent. */
38
+ const sent = (onSave: Mock) => {
39
+ expect(onSave).toHaveBeenCalledOnce();
40
+ return onSave.mock.calls[0]![0] as PatientDetailsInput;
41
+ };
42
+
43
+ const clickSave = (user: ReturnType<typeof userEvent.setup>) =>
44
+ user.click(screen.getByRole("button", { name: "Save" }));
45
+
46
+ describe("PatientDetailsForm", () => {
47
+ describe("initial values", () => {
48
+ it("saved patient: fills in every field from the record, with the phone number formatted", () => {
49
+ renderForm({ patient: ada({ phone: "+14155550172", email: "ada@example.com", address }) });
50
+ expect(field("First name")).toHaveValue("Ada");
51
+ expect(field("Last name")).toHaveValue("Lovelace");
52
+ expect(field("Date of birth")).toHaveValue("1990-01-01");
53
+ expect(screen.getByRole("combobox", { name: "Sex" })).toHaveValue("FEMALE");
54
+ expect(field("Phone")).toHaveValue("+1 (415) 555-0172");
55
+ expect(field("Email")).toHaveValue("ada@example.com");
56
+ expect(field("Street")).toHaveValue("1 Main St");
57
+ expect(field("Apt / suite")).toHaveValue("");
58
+ expect(field("City")).toHaveValue("Boston");
59
+ expect(field("State")).toHaveValue("MA");
60
+ expect(field("ZIP")).toHaveValue("02101");
61
+ });
62
+
63
+ it("no demographic or contact details: every field starts empty", () => {
64
+ renderForm({ patient: record({ demographic: null }) });
65
+ for (const label of ["First name", "Last name", "Date of birth", "Phone", "Email", "Street", "City", "ZIP"]) {
66
+ expect(field(label)).toHaveValue("");
67
+ }
68
+ expect(screen.getByRole("combobox", { name: "Sex" })).toHaveValue("");
69
+ });
70
+ });
71
+
72
+ describe("phone", () => {
73
+ it("phone typed, then leaving the field: formats the phone number", async () => {
74
+ const { user } = renderForm();
75
+ await retype(user, "Phone", "415-555-0172");
76
+ expect(field("Phone")).toHaveValue("+1 (415) 555-0172");
77
+ });
78
+ });
79
+
80
+ // Pins today's behavior
81
+ // Question: The form checks a field once the prescriber has left it (react-hook-form's "onTouched"
82
+ // mode), then again on every keystroke. This was an arbitrary choice - open to only showing validation
83
+ // errors on Save instead.
84
+ describe("errors while typing", () => {
85
+ it("phone typed: no error until leaving the field, then the error updates on every keystroke", async () => {
86
+ const { user } = renderForm();
87
+ await user.type(field("Phone"), "415");
88
+ expect(field("Phone")).not.toHaveAccessibleDescription("Enter a valid phone number");
89
+
90
+ await user.tab();
91
+ expect(field("Phone")).toHaveAccessibleDescription("Enter a valid phone number");
92
+
93
+ await user.type(field("Phone"), "5550172");
94
+ expect(field("Phone")).not.toHaveAccessibleDescription("Enter a valid phone number");
95
+ });
96
+ });
97
+
98
+ // How Save connects the form to PatientDetailsSchema and changesToSave
99
+ describe("Save", () => {
100
+ it("nothing changed, clicking Save: calls onSave once with every field filled in, changed or not", async () => {
101
+ const { user, onSave } = renderForm({ patient: ada({ phone: "+14155550172" }) });
102
+ await clickSave(user);
103
+ expect(onSave).toHaveBeenCalledExactlyOnceWith({
104
+ demographic: { name: { first: "Ada", last: "Lovelace" }, dateOfBirth: "1990-01-01", sex: "FEMALE" },
105
+ contact: { phone: "+14155550172", email: "" },
106
+ });
107
+ });
108
+
109
+ it("every field changed, clicking Save: sends every field as PatientDetailsSchema parses it", async () => {
110
+ const { user, onSave } = renderForm();
111
+ await retype(user, "First name", "Augusta");
112
+ await retype(user, "Last name", "Byron");
113
+ await retype(user, "Date of birth", "1991-02-03");
114
+ await user.selectOptions(screen.getByRole("combobox", { name: "Sex" }), "MALE");
115
+ await retype(user, "Phone", "415-555-0172");
116
+ await retype(user, "Email", "ada@example.com");
117
+ await retype(user, "Street", " 1 Main St ");
118
+ await retype(user, "Apt / suite", "Apt 2");
119
+ await retype(user, "City", "Boston");
120
+ await retype(user, "State", "ma");
121
+ await retype(user, "ZIP", "02101");
122
+ await clickSave(user);
123
+ expect(onSave).toHaveBeenCalledExactlyOnceWith({
124
+ demographic: { name: { first: "Augusta", last: "Byron" }, dateOfBirth: "1991-02-03", sex: "MALE" },
125
+ contact: {
126
+ phone: "+14155550172",
127
+ email: "ada@example.com",
128
+ address: {
129
+ street1: "1 Main St",
130
+ street2: "Apt 2",
131
+ city: "Boston",
132
+ state: "MA",
133
+ postalCode: "02101",
134
+ country: "US",
135
+ },
136
+ },
137
+ });
138
+ });
139
+
140
+ it("phone on file cleared, clicking Save: sends an empty phone to clear it in network-api", async () => {
141
+ const { user, onSave } = renderForm({ patient: ada({ phone: "+14155550172" }) });
142
+ await retype(user, "Phone", "");
143
+ await clickSave(user);
144
+ expect(sent(onSave)).toMatchObject({ contact: { phone: "" } });
145
+ });
146
+
147
+ it("email on file cleared, clicking Save: sends an empty email to clear it in network-api", async () => {
148
+ const { user, onSave } = renderForm({ patient: ada({ email: "ada@example.com" }) });
149
+ await retype(user, "Email", "");
150
+ await clickSave(user);
151
+ expect(sent(onSave)).toMatchObject({ contact: { email: "" } });
152
+ });
153
+
154
+ // Documents that clearing a required field and clicking Save runs PatientDetailsSchema,
155
+ // shows its "Required" error on that field, and stops Save from sending anything.
156
+ it('last name cleared, clicking Save: shows "Required" on the last name, and onSave is not called', async () => {
157
+ const { user, onSave } = renderForm();
158
+ await user.clear(field("Last name"));
159
+ await clickSave(user);
160
+ expect(onSave).not.toHaveBeenCalled();
161
+ expect(field("Last name")).toHaveAccessibleDescription("Required");
162
+ });
163
+
164
+ // Documents that filling out a single address field causes
165
+ // other address fields to become required
166
+ it('only street filled in, clicking Save: shows "Required" on the untouched city, state and ZIP fields', async () => {
167
+ const { user, onSave } = renderForm();
168
+ await user.type(field("Street"), "1 Main St");
169
+ await clickSave(user);
170
+ expect(onSave).not.toHaveBeenCalled();
171
+ for (const label of ["City", "State", "ZIP"]) expect(field(label)).toHaveAccessibleDescription("Required");
172
+ });
173
+ });
174
+
175
+ // Pins today's behavior
176
+ // Sending an order the server refuses until the patient can be reached (a missing phone, email or address)
177
+ // expands PatientSummary and shows this warning above the contact fields. PhotonOrder passes the request down
178
+ // as `addressRequest`.
179
+ // TODO(TECH-1491): find a better place for this error than the patient's details form.
180
+ describe("contact request", () => {
181
+ it('order send refused until the patient can be reached, phone on file: shows "Address needed to send"', () => {
182
+ renderForm({ patient: ada({ phone: "+14155550172" }), addressRequest: "patient pat_1 has no address" });
183
+ expect(screen.getByText("Address needed to send")).toBeInTheDocument();
184
+ });
185
+
186
+ it('order send refused until the patient can be reached, no phone on file: shows "Phone needed to send"', () => {
187
+ renderForm({ addressRequest: "patient pat_1 has no address" });
188
+ expect(screen.getByText("Phone needed to send")).toBeInTheDocument();
189
+ });
190
+
191
+ it("order send refused until the patient can be reached: doesn't show the server's message, which can name internal ids", () => {
192
+ renderForm({ addressRequest: "patient pat_1 has no address" });
193
+ expect(screen.queryByText(/pat_1/)).not.toBeInTheDocument();
194
+ });
195
+
196
+ it("no contact request: shows no message", () => {
197
+ renderForm();
198
+ expect(screen.queryByText(/needed to send/)).not.toBeInTheDocument();
199
+ });
200
+ });
201
+ });
@@ -0,0 +1,320 @@
1
+ import { useComponents } from "../../../primitives/index.ts";
2
+ import { type PatientInput, type PatientRecord } from "@photonhealth/client";
3
+ import { zodResolver } from "@hookform/resolvers/zod";
4
+ import { Controller, useForm, type Resolver } from "react-hook-form";
5
+ import { formatPhone } from "../../format.ts";
6
+ import { noAutofill, SEX_OPTIONS } from "../fields.tsx";
7
+ import { TrackedField, useFieldInteractions } from "../../../analytics/fieldInteraction.tsx";
8
+ import { PatientDetailsSchema, type PatientDetails, type PatientDetailsFormValues } from "./PatientDetailsSchema.ts";
9
+
10
+ /** The part of an UpdatePatient that PatientDetailsForm sends. */
11
+ export type PatientDetailsInput = Pick<PatientInput, "demographic" | "contact">;
12
+
13
+ export function PatientDetailsForm({
14
+ patient,
15
+ addressRequest,
16
+ onSave,
17
+ }: {
18
+ patient: PatientRecord;
19
+ addressRequest: string | null;
20
+ /** Called by Save once every field is valid, with every field to send. */
21
+ onSave: (input: PatientDetailsInput) => void | Promise<void>;
22
+ }) {
23
+ const { Alert, Button, Grid, Input, Row, Select, Stack } = useComponents();
24
+ const phone = patient.contact?.phone ?? null;
25
+
26
+ const {
27
+ control,
28
+ handleSubmit,
29
+ formState: { isSubmitting },
30
+ } = useForm<PatientDetailsFormValues, unknown, PatientDetails>({
31
+ resolver: zodResolver(PatientDetailsSchema) as unknown as Resolver<
32
+ PatientDetailsFormValues,
33
+ unknown,
34
+ PatientDetails
35
+ >,
36
+ defaultValues: formValuesFrom(patient),
37
+ mode: "onTouched",
38
+ });
39
+
40
+ const fieldInteraction = useFieldInteractions("update_patient_form");
41
+
42
+ return (
43
+ <>
44
+ <Grid min="200px" gap="3">
45
+ <TrackedField formName="update_patient_form" fieldName="firstName" isOptional={false}>
46
+ <Controller
47
+ control={control}
48
+ name="firstName"
49
+ render={({ field, fieldState }) => (
50
+ <Input
51
+ label="First name"
52
+ name={field.name}
53
+ value={field.value}
54
+ onChange={field.onChange}
55
+ onBlur={field.onBlur}
56
+ invalid={fieldState.invalid}
57
+ errorMessage={fieldState.error?.message}
58
+ {...noAutofill}
59
+ />
60
+ )}
61
+ />
62
+ </TrackedField>
63
+ <TrackedField formName="update_patient_form" fieldName="lastName" isOptional={false}>
64
+ <Controller
65
+ control={control}
66
+ name="lastName"
67
+ render={({ field, fieldState }) => (
68
+ <Input
69
+ label="Last name"
70
+ name={field.name}
71
+ value={field.value}
72
+ onChange={field.onChange}
73
+ onBlur={field.onBlur}
74
+ invalid={fieldState.invalid}
75
+ errorMessage={fieldState.error?.message}
76
+ {...noAutofill}
77
+ />
78
+ )}
79
+ />
80
+ </TrackedField>
81
+ <TrackedField formName="update_patient_form" fieldName="dateOfBirth" isOptional={false}>
82
+ <Controller
83
+ control={control}
84
+ name="dateOfBirth"
85
+ render={({ field, fieldState }) => (
86
+ <Input
87
+ label="Date of birth"
88
+ type="date"
89
+ {...noAutofill}
90
+ name={field.name}
91
+ value={field.value}
92
+ onChange={field.onChange}
93
+ onBlur={field.onBlur}
94
+ invalid={fieldState.invalid}
95
+ errorMessage={fieldState.error?.message}
96
+ />
97
+ )}
98
+ />
99
+ </TrackedField>
100
+ <Controller
101
+ control={control}
102
+ name="sex"
103
+ render={({ field, fieldState }) => {
104
+ const sexInteractions = fieldInteraction("sex", { isOptional: false, hasValue: Boolean(field.value) });
105
+ return (
106
+ <div style={{ display: "contents" }} onFocus={sexInteractions.onFocus} onBlur={sexInteractions.onBlur}>
107
+ <Select
108
+ label="Sex"
109
+ placeholder="Select…"
110
+ options={SEX_OPTIONS}
111
+ value={field.value}
112
+ onChange={(sex) => {
113
+ sexInteractions.onChange(true);
114
+ field.onChange(sex);
115
+ }}
116
+ invalid={fieldState.invalid}
117
+ errorMessage={fieldState.error?.message}
118
+ />
119
+ </div>
120
+ );
121
+ }}
122
+ />
123
+ </Grid>
124
+ <Stack gap="2">
125
+ {/* The server's message can name internal ids ("patient pat_… has no address"); the title says it. A send
126
+ needs a phone; an address only when the server still asks with a phone on file. */}
127
+ {addressRequest && <Alert tone="warning" title={phone ? "Address needed to send" : "Phone needed to send"} />}
128
+ <Grid min="120px" gap="3">
129
+ <TrackedField formName="update_patient_form" fieldName="phone" isOptional>
130
+ <Controller
131
+ control={control}
132
+ name="phone"
133
+ render={({ field, fieldState }) => (
134
+ <Input
135
+ label="Phone"
136
+ type="tel"
137
+ inputMode="tel"
138
+ name={field.name}
139
+ value={field.value}
140
+ onChange={field.onChange}
141
+ // Leaving the field shows the number formatted, e.g. "+1 (415) 555-0172".
142
+ onBlur={() => {
143
+ field.onChange(formatPhone(field.value));
144
+ field.onBlur();
145
+ }}
146
+ invalid={fieldState.invalid}
147
+ errorMessage={fieldState.error?.message}
148
+ {...noAutofill}
149
+ />
150
+ )}
151
+ />
152
+ </TrackedField>
153
+ <TrackedField formName="update_patient_form" fieldName="email" isOptional>
154
+ <Controller
155
+ control={control}
156
+ name="email"
157
+ render={({ field, fieldState }) => (
158
+ <Input
159
+ label="Email"
160
+ type="email"
161
+ inputMode="email"
162
+ name={field.name}
163
+ value={field.value}
164
+ onChange={field.onChange}
165
+ onBlur={field.onBlur}
166
+ invalid={fieldState.invalid}
167
+ errorMessage={fieldState.error?.message}
168
+ {...noAutofill}
169
+ />
170
+ )}
171
+ />
172
+ </TrackedField>
173
+ </Grid>
174
+ <Stack gap="3">
175
+ <Grid min="120px" gap="3">
176
+ <TrackedField formName="update_patient_form" fieldName="address_street1" isOptional>
177
+ <Controller
178
+ control={control}
179
+ name="address.street1"
180
+ render={({ field, fieldState }) => (
181
+ <Input
182
+ label="Street"
183
+ name={field.name}
184
+ value={field.value}
185
+ onChange={field.onChange}
186
+ onBlur={field.onBlur}
187
+ invalid={fieldState.invalid}
188
+ errorMessage={fieldState.error?.message}
189
+ {...noAutofill}
190
+ />
191
+ )}
192
+ />
193
+ </TrackedField>
194
+ <TrackedField formName="update_patient_form" fieldName="address_street2" isOptional>
195
+ <Controller
196
+ control={control}
197
+ name="address.street2"
198
+ render={({ field, fieldState }) => (
199
+ <Input
200
+ label="Apt / suite"
201
+ name={field.name}
202
+ value={field.value}
203
+ onChange={field.onChange}
204
+ onBlur={field.onBlur}
205
+ invalid={fieldState.invalid}
206
+ errorMessage={fieldState.error?.message}
207
+ {...noAutofill}
208
+ />
209
+ )}
210
+ />
211
+ </TrackedField>
212
+ </Grid>
213
+ <Grid min="120px" gap="3">
214
+ <TrackedField formName="update_patient_form" fieldName="address_city" isOptional>
215
+ <Controller
216
+ control={control}
217
+ name="address.city"
218
+ render={({ field, fieldState }) => (
219
+ <Input
220
+ label="City"
221
+ name={field.name}
222
+ value={field.value}
223
+ onChange={field.onChange}
224
+ onBlur={field.onBlur}
225
+ invalid={fieldState.invalid}
226
+ errorMessage={fieldState.error?.message}
227
+ {...noAutofill}
228
+ />
229
+ )}
230
+ />
231
+ </TrackedField>
232
+ <TrackedField formName="update_patient_form" fieldName="address_state" isOptional>
233
+ <Controller
234
+ control={control}
235
+ name="address.state"
236
+ render={({ field, fieldState }) => (
237
+ <Input
238
+ label="State"
239
+ maxLength={2}
240
+ name={field.name}
241
+ value={field.value}
242
+ onChange={field.onChange}
243
+ onBlur={field.onBlur}
244
+ invalid={fieldState.invalid}
245
+ errorMessage={fieldState.error?.message}
246
+ {...noAutofill}
247
+ />
248
+ )}
249
+ />
250
+ </TrackedField>
251
+ <TrackedField formName="update_patient_form" fieldName="address_zip" isOptional>
252
+ <Controller
253
+ control={control}
254
+ name="address.postalCode"
255
+ render={({ field, fieldState }) => (
256
+ <Input
257
+ label="ZIP"
258
+ inputMode="numeric"
259
+ name={field.name}
260
+ value={field.value}
261
+ onChange={field.onChange}
262
+ onBlur={field.onBlur}
263
+ invalid={fieldState.invalid}
264
+ errorMessage={fieldState.error?.message}
265
+ {...noAutofill}
266
+ />
267
+ )}
268
+ />
269
+ </TrackedField>
270
+ </Grid>
271
+ </Stack>
272
+ </Stack>
273
+ <Row justify="end">
274
+ {/* Save waits for onSave, so it's disabled (`loading`) while the update is in flight. */}
275
+ <Button
276
+ variant="secondary"
277
+ loading={isSubmitting}
278
+ onClick={handleSubmit((details) => onSave(toPatientInput(details)))}
279
+ >
280
+ Save
281
+ </Button>
282
+ </Row>
283
+ </>
284
+ );
285
+ }
286
+
287
+ function formValuesFrom(patient: PatientRecord): PatientDetailsFormValues {
288
+ const demographic = patient.demographic;
289
+ const address = patient.contact?.address;
290
+ return {
291
+ firstName: demographic?.name.first ?? "",
292
+ lastName: demographic?.name.last ?? "",
293
+ dateOfBirth: demographic?.dateOfBirth ?? "",
294
+ sex: demographic?.sex ?? null,
295
+ phone: formatPhone(patient.contact?.phone),
296
+ email: patient.contact?.email ?? "",
297
+ address: {
298
+ street1: address?.street1 ?? "",
299
+ street2: address?.street2 ?? "",
300
+ city: address?.city ?? "",
301
+ state: address?.state ?? "",
302
+ postalCode: address?.postalCode ?? "",
303
+ },
304
+ };
305
+ }
306
+
307
+ function toPatientInput(details: PatientDetails): PatientDetailsInput {
308
+ return {
309
+ demographic: {
310
+ name: { first: details.firstName, last: details.lastName },
311
+ dateOfBirth: details.dateOfBirth,
312
+ sex: details.sex,
313
+ },
314
+ contact: {
315
+ phone: details.phone,
316
+ email: details.email,
317
+ ...(details.address && { address: details.address }),
318
+ },
319
+ };
320
+ }