@olaboot/esm-patient-registration-app 9.2.0

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 (250) hide show
  1. package/ADDRESS_CONFIGURATION.md +152 -0
  2. package/IDENTIFIER_CONFIGURATION.md +142 -0
  3. package/IMPLEMENTATION_SUMMARY.md +111 -0
  4. package/QUICK_START.md +95 -0
  5. package/README.md +7 -0
  6. package/address-required-fields-config.json +26 -0
  7. package/dist/126.js +1 -0
  8. package/dist/15.js +1 -0
  9. package/dist/1564.js +1 -0
  10. package/dist/1567.js +1 -0
  11. package/dist/1845.js +1 -0
  12. package/dist/1953.js +1 -0
  13. package/dist/200.js +1 -0
  14. package/dist/200.js.map +1 -0
  15. package/dist/215.js +1 -0
  16. package/dist/2178.js +1 -0
  17. package/dist/250.js +1 -0
  18. package/dist/250.js.map +1 -0
  19. package/dist/2523.js +1 -0
  20. package/dist/2523.js.map +1 -0
  21. package/dist/2566.js +1 -0
  22. package/dist/2586.js +1 -0
  23. package/dist/2586.js.map +1 -0
  24. package/dist/2716.js +1 -0
  25. package/dist/2716.js.map +1 -0
  26. package/dist/2759.js +1 -0
  27. package/dist/2821.js +6 -0
  28. package/dist/2821.js.map +1 -0
  29. package/dist/3089.js +1 -0
  30. package/dist/3089.js.map +1 -0
  31. package/dist/3230.js +1 -0
  32. package/dist/3441.js +1 -0
  33. package/dist/3565.js +1 -0
  34. package/dist/3571.js +1 -0
  35. package/dist/3571.js.map +1 -0
  36. package/dist/3746.js +1 -0
  37. package/dist/3925.js +1 -0
  38. package/dist/3946.js +1 -0
  39. package/dist/4024.js +1 -0
  40. package/dist/4024.js.map +1 -0
  41. package/dist/4744.js +1 -0
  42. package/dist/4744.js.map +1 -0
  43. package/dist/4809.js +1 -0
  44. package/dist/4894.js +1 -0
  45. package/dist/4970.js +1 -0
  46. package/dist/4970.js.map +1 -0
  47. package/dist/5130.js +1 -0
  48. package/dist/5187.js +1 -0
  49. package/dist/5491.js +1 -0
  50. package/dist/5491.js.map +1 -0
  51. package/dist/5595.js +1 -0
  52. package/dist/5961.js +1 -0
  53. package/dist/6133.js +1 -0
  54. package/dist/634.js +1 -0
  55. package/dist/634.js.map +1 -0
  56. package/dist/6456.js +1 -0
  57. package/dist/6466.js +1 -0
  58. package/dist/6613.js +1 -0
  59. package/dist/6783.js +1 -0
  60. package/dist/7073.js +38 -0
  61. package/dist/7073.js.map +1 -0
  62. package/dist/7154.js +1 -0
  63. package/dist/7154.js.map +1 -0
  64. package/dist/7348.js +1 -0
  65. package/dist/7439.js +1 -0
  66. package/dist/7439.js.map +1 -0
  67. package/dist/7543.js +1 -0
  68. package/dist/7607.js +1 -0
  69. package/dist/772.js +1 -0
  70. package/dist/7984.js +1 -0
  71. package/dist/7984.js.map +1 -0
  72. package/dist/8538.js +1 -0
  73. package/dist/8538.js.map +1 -0
  74. package/dist/8599.js +1 -0
  75. package/dist/8727.js +1 -0
  76. package/dist/8847.js +1 -0
  77. package/dist/9015.js +1 -0
  78. package/dist/906.js +1 -0
  79. package/dist/9065.js +1 -0
  80. package/dist/9182.js +1 -0
  81. package/dist/9339.js +1 -0
  82. package/dist/9453.js +1 -0
  83. package/dist/9833.js +1 -0
  84. package/dist/9833.js.map +1 -0
  85. package/dist/9856.js +1 -0
  86. package/dist/9856.js.map +1 -0
  87. package/dist/9920.js +1 -0
  88. package/dist/9938.js +1 -0
  89. package/dist/9943.js +1 -0
  90. package/dist/9943.js.map +1 -0
  91. package/dist/main.js +10 -0
  92. package/dist/main.js.map +1 -0
  93. package/dist/olaboot-esm-patient-registration-app.js +5 -0
  94. package/dist/olaboot-esm-patient-registration-app.js.buildmanifest.json +1627 -0
  95. package/dist/olaboot-esm-patient-registration-app.js.map +1 -0
  96. package/dist/routes.json +1 -0
  97. package/docs/images/patient-registration-hierarchy.png +0 -0
  98. package/example-config.json +14 -0
  99. package/jest.config.js +3 -0
  100. package/package.json +60 -0
  101. package/rspack.config.js +1 -0
  102. package/src/add-patient-link.extension.tsx +21 -0
  103. package/src/add-patient-link.scss +3 -0
  104. package/src/add-patient-link.test.tsx +16 -0
  105. package/src/config-schema.ts +507 -0
  106. package/src/constants.ts +14 -0
  107. package/src/declarations.d.ts +4 -0
  108. package/src/index.ts +59 -0
  109. package/src/nav-link.test.tsx +13 -0
  110. package/src/nav-link.tsx +10 -0
  111. package/src/offline.resources.ts +157 -0
  112. package/src/offline.ts +93 -0
  113. package/src/patient-photo.extension.tsx +11 -0
  114. package/src/patient-registration/before-save-prompt.component.tsx +72 -0
  115. package/src/patient-registration/field/__mocks__/field.resource.ts +60 -0
  116. package/src/patient-registration/field/address/address-field.component.tsx +186 -0
  117. package/src/patient-registration/field/address/address-hierarchy-levels.component.tsx +71 -0
  118. package/src/patient-registration/field/address/address-hierarchy.resource.tsx +157 -0
  119. package/src/patient-registration/field/address/address-hierarchy.test.tsx +296 -0
  120. package/src/patient-registration/field/address/address-search.component.tsx +87 -0
  121. package/src/patient-registration/field/address/address-search.scss +53 -0
  122. package/src/patient-registration/field/address/address-search.test.tsx +141 -0
  123. package/src/patient-registration/field/address/custom-address-field.component.tsx +32 -0
  124. package/src/patient-registration/field/cause-of-death/cause-of-death.component.tsx +98 -0
  125. package/src/patient-registration/field/custom-field.component.tsx +25 -0
  126. package/src/patient-registration/field/date-and-time-of-death/date-and-time-of-death.component.tsx +79 -0
  127. package/src/patient-registration/field/dob/dob.component.tsx +167 -0
  128. package/src/patient-registration/field/dob/dob.test.tsx +90 -0
  129. package/src/patient-registration/field/field.component.tsx +53 -0
  130. package/src/patient-registration/field/field.resource.ts +42 -0
  131. package/src/patient-registration/field/field.scss +171 -0
  132. package/src/patient-registration/field/field.test.tsx +330 -0
  133. package/src/patient-registration/field/gender/gender-field.component.tsx +54 -0
  134. package/src/patient-registration/field/gender/gender-field.test.tsx +99 -0
  135. package/src/patient-registration/field/id/id-field.component.tsx +136 -0
  136. package/src/patient-registration/field/id/id-field.test.tsx +121 -0
  137. package/src/patient-registration/field/id/identifier-selection-overlay.component.tsx +200 -0
  138. package/src/patient-registration/field/id/identifier-selection.scss +41 -0
  139. package/src/patient-registration/field/name/name-field.component.tsx +148 -0
  140. package/src/patient-registration/field/obs/obs-field.component.tsx +261 -0
  141. package/src/patient-registration/field/obs/obs-field.test.tsx +299 -0
  142. package/src/patient-registration/field/person-attributes/coded-person-attribute-field.component.tsx +120 -0
  143. package/src/patient-registration/field/person-attributes/coded-person-attribute-field.test.tsx +141 -0
  144. package/src/patient-registration/field/person-attributes/location-person-attribute-field.component.tsx +105 -0
  145. package/src/patient-registration/field/person-attributes/location-person-attribute-field.resource.tsx +48 -0
  146. package/src/patient-registration/field/person-attributes/person-attribute-field.component.tsx +100 -0
  147. package/src/patient-registration/field/person-attributes/person-attribute-field.test.tsx +193 -0
  148. package/src/patient-registration/field/person-attributes/person-attributes.resource.ts +20 -0
  149. package/src/patient-registration/field/person-attributes/text-person-attribute-field.component.tsx +58 -0
  150. package/src/patient-registration/field/person-attributes/text-person-attribute-field.test.tsx +90 -0
  151. package/src/patient-registration/field/phone/phone-field.component.tsx +17 -0
  152. package/src/patient-registration/form-manager.test.ts +91 -0
  153. package/src/patient-registration/form-manager.ts +443 -0
  154. package/src/patient-registration/input/basic-input/input/input.component.tsx +183 -0
  155. package/src/patient-registration/input/basic-input/input/input.test.tsx +72 -0
  156. package/src/patient-registration/input/basic-input/select/select-input.component.tsx +32 -0
  157. package/src/patient-registration/input/basic-input/select/select-input.test.tsx +49 -0
  158. package/src/patient-registration/input/combo-input/combo-input.component.tsx +130 -0
  159. package/src/patient-registration/input/combo-input/selection-tick.component.tsx +20 -0
  160. package/src/patient-registration/input/custom-input/autosuggest/autosuggest.component.tsx +187 -0
  161. package/src/patient-registration/input/custom-input/autosuggest/autosuggest.scss +62 -0
  162. package/src/patient-registration/input/custom-input/autosuggest/autosuggest.test.tsx +164 -0
  163. package/src/patient-registration/input/custom-input/identifier/identifier-input.component.tsx +193 -0
  164. package/src/patient-registration/input/custom-input/identifier/identifier-input.test.tsx +335 -0
  165. package/src/patient-registration/input/custom-input/identifier/utils.test.ts +81 -0
  166. package/src/patient-registration/input/custom-input/identifier/utils.ts +19 -0
  167. package/src/patient-registration/input/dummy-data/dummy-data-input.component.tsx +56 -0
  168. package/src/patient-registration/input/dummy-data/dummy-data-input.test.tsx +34 -0
  169. package/src/patient-registration/input/input.scss +122 -0
  170. package/src/patient-registration/patient-registration-context.ts +35 -0
  171. package/src/patient-registration/patient-registration-hooks.ts +376 -0
  172. package/src/patient-registration/patient-registration-utils.test.ts +33 -0
  173. package/src/patient-registration/patient-registration-utils.ts +214 -0
  174. package/src/patient-registration/patient-registration.component.tsx +266 -0
  175. package/src/patient-registration/patient-registration.resource.test.tsx +22 -0
  176. package/src/patient-registration/patient-registration.resource.ts +198 -0
  177. package/src/patient-registration/patient-registration.scss +103 -0
  178. package/src/patient-registration/patient-registration.test.tsx +580 -0
  179. package/src/patient-registration/patient-registration.types.ts +322 -0
  180. package/src/patient-registration/section/death-info/death-info-section.component.tsx +36 -0
  181. package/src/patient-registration/section/death-info/death-info-section.test.tsx +47 -0
  182. package/src/patient-registration/section/demographics/demographics-section.component.tsx +30 -0
  183. package/src/patient-registration/section/demographics/demographics-section.test.tsx +98 -0
  184. package/src/patient-registration/section/generic-section.component.tsx +17 -0
  185. package/src/patient-registration/section/patient-relationships/relationships-section.component.tsx +234 -0
  186. package/src/patient-registration/section/patient-relationships/relationships-section.test.tsx +113 -0
  187. package/src/patient-registration/section/patient-relationships/relationships.resource.tsx +78 -0
  188. package/src/patient-registration/section/patient-relationships/relationships.scss +35 -0
  189. package/src/patient-registration/section/section-wrapper.component.tsx +40 -0
  190. package/src/patient-registration/section/section.component.tsx +23 -0
  191. package/src/patient-registration/section/section.scss +21 -0
  192. package/src/patient-registration/ui-components/overlay/overlay.component.tsx +51 -0
  193. package/src/patient-registration/ui-components/overlay/overlay.scss +63 -0
  194. package/src/patient-registration/validation/patient-registration-validation.test.ts +205 -0
  195. package/src/patient-registration/validation/patient-registration-validation.ts +123 -0
  196. package/src/resource.ts +12 -0
  197. package/src/resources-context.ts +14 -0
  198. package/src/root.component.tsx +63 -0
  199. package/src/root.scss +7 -0
  200. package/src/routes.json +61 -0
  201. package/src/widgets/cancel-patient-edit.modal.tsx +33 -0
  202. package/src/widgets/cancel-patient-edit.test.tsx +22 -0
  203. package/src/widgets/delete-identifier-confirmation.modal.tsx +48 -0
  204. package/src/widgets/delete-identifier-confirmation.test.tsx +32 -0
  205. package/src/widgets/edit-patient-details-button.component.tsx +33 -0
  206. package/src/widgets/edit-patient-details-button.scss +3 -0
  207. package/src/widgets/edit-patient-details-button.test.tsx +35 -0
  208. package/translations/am.json +120 -0
  209. package/translations/ar.json +120 -0
  210. package/translations/ar_SY.json +120 -0
  211. package/translations/bn.json +120 -0
  212. package/translations/cs.json +120 -0
  213. package/translations/de.json +120 -0
  214. package/translations/en.json +120 -0
  215. package/translations/en_US.json +120 -0
  216. package/translations/es.json +120 -0
  217. package/translations/es_MX.json +120 -0
  218. package/translations/fr.json +120 -0
  219. package/translations/he.json +120 -0
  220. package/translations/hi.json +120 -0
  221. package/translations/hi_IN.json +120 -0
  222. package/translations/id.json +120 -0
  223. package/translations/it.json +120 -0
  224. package/translations/ka.json +120 -0
  225. package/translations/km.json +120 -0
  226. package/translations/ku.json +120 -0
  227. package/translations/ky.json +120 -0
  228. package/translations/lg.json +120 -0
  229. package/translations/ne.json +120 -0
  230. package/translations/pl.json +120 -0
  231. package/translations/pt.json +120 -0
  232. package/translations/pt_BR.json +120 -0
  233. package/translations/qu.json +120 -0
  234. package/translations/ro_RO.json +120 -0
  235. package/translations/ru_RU.json +120 -0
  236. package/translations/si.json +120 -0
  237. package/translations/sq.json +120 -0
  238. package/translations/sw.json +120 -0
  239. package/translations/sw_KE.json +120 -0
  240. package/translations/tr.json +120 -0
  241. package/translations/tr_TR.json +120 -0
  242. package/translations/uk.json +120 -0
  243. package/translations/uz.json +120 -0
  244. package/translations/uz@Latn.json +120 -0
  245. package/translations/uz_UZ.json +120 -0
  246. package/translations/vi.json +120 -0
  247. package/translations/zh.json +120 -0
  248. package/translations/zh_CN.json +120 -0
  249. package/translations/zh_TW.json +120 -0
  250. package/tsconfig.json +5 -0
@@ -0,0 +1,141 @@
1
+ import React from 'react';
2
+ import userEvent from '@testing-library/user-event';
3
+ import { screen } from '@testing-library/react';
4
+ import { Formik, Form, useFormikContext } from 'formik';
5
+ import { renderWithContext } from 'tools';
6
+ import { type Resources } from '../../../offline.resources';
7
+ import { PatientRegistrationContextProvider } from '../../patient-registration-context';
8
+ import { getDefaultsFromConfigSchema, useConfig } from '@openmrs/esm-framework';
9
+ import { useAddressHierarchy, useOrderedAddressHierarchyLevels } from './address-hierarchy.resource';
10
+ import { type RegistrationConfig, esmPatientRegistrationSchema } from '../../../config-schema';
11
+ import { mockedAddressTemplate, mockedAddressOptions, mockedOrderedFields } from '__mocks__';
12
+ import { ResourcesContextProvider } from '../../../resources-context';
13
+ import AddressSearchComponent from './address-search.component';
14
+
15
+ const mockUseConfig = jest.mocked(useConfig<RegistrationConfig>);
16
+ const mockUseAddressHierarchy = jest.mocked(useAddressHierarchy);
17
+ const mockUseOrderedAddressHierarchyLevels = jest.mocked(useOrderedAddressHierarchyLevels);
18
+ const mockUseFormikContext = useFormikContext as jest.Mock;
19
+
20
+ jest.mock('./address-hierarchy.resource', () => ({
21
+ ...(jest.requireActual('./address-hierarchy.resource') as jest.Mock),
22
+ useAddressHierarchy: jest.fn(),
23
+ useOrderedAddressHierarchyLevels: jest.fn(),
24
+ }));
25
+
26
+ jest.mock('../../patient-registration.resource', () => ({
27
+ ...(jest.requireActual('../../../patient-registration.resource') as jest.Mock),
28
+ useAddressHierarchy: jest.fn(),
29
+ }));
30
+
31
+ jest.mock('formik', () => ({
32
+ ...(jest.requireActual('formik') as jest.Mock),
33
+ useFormikContext: jest.fn(() => ({})),
34
+ }));
35
+
36
+ const allFields = mockedAddressTemplate.lines
37
+ .flat()
38
+ .filter((field) => field.isToken === 'IS_ADDR_TOKEN')
39
+ .map(({ codeName, displayText }) => ({
40
+ id: codeName,
41
+ name: codeName,
42
+ label: displayText,
43
+ }));
44
+ const orderMap = Object.fromEntries(mockedOrderedFields.map((field, indx) => [field, indx]));
45
+ allFields.sort((existingField1, existingField2) => orderMap[existingField1.name] - orderMap[existingField2.name]);
46
+
47
+ async function renderAddressHierarchy(addressTemplate = mockedAddressTemplate) {
48
+ const mockResourcesContextValue = { addressTemplate } as unknown as Resources;
49
+
50
+ await renderWithContext(
51
+ <Formik initialValues={{}} onSubmit={null}>
52
+ <Form>
53
+ <PatientRegistrationContextProvider value={{ setFieldValue: jest.fn() } as any}>
54
+ <AddressSearchComponent addressLayout={allFields} />
55
+ </PatientRegistrationContextProvider>
56
+ </Form>
57
+ </Formik>,
58
+ ResourcesContextProvider,
59
+ mockResourcesContextValue,
60
+ );
61
+ }
62
+
63
+ const setFieldValue = jest.fn();
64
+
65
+ describe('Testing address search bar', () => {
66
+ beforeEach(() => {
67
+ mockUseConfig.mockReturnValue({
68
+ ...getDefaultsFromConfigSchema(esmPatientRegistrationSchema),
69
+ fieldConfigurations: {
70
+ address: {
71
+ useAddressHierarchy: {
72
+ enabled: true,
73
+ useQuickSearch: true,
74
+ searchAddressByLevel: false,
75
+ },
76
+ requiredAddressFields: [],
77
+ },
78
+ } as RegistrationConfig['fieldConfigurations'],
79
+ });
80
+ mockUseOrderedAddressHierarchyLevels.mockReturnValue({
81
+ orderedFields: mockedOrderedFields,
82
+ isLoadingFieldOrder: false,
83
+ errorFetchingFieldOrder: null,
84
+ });
85
+ mockUseFormikContext.mockReturnValue({
86
+ setFieldValue,
87
+ });
88
+ });
89
+
90
+ it('should render the search bar', () => {
91
+ mockUseAddressHierarchy.mockReturnValue({
92
+ addresses: [],
93
+ error: null,
94
+ isLoading: false,
95
+ });
96
+
97
+ renderAddressHierarchy();
98
+
99
+ const searchbox = screen.getByRole('searchbox');
100
+ expect(searchbox).toBeInTheDocument();
101
+
102
+ const ul = screen.queryByRole('list');
103
+ expect(ul).not.toBeInTheDocument();
104
+ });
105
+
106
+ it("should render only the results for the search term matched address' parents", async () => {
107
+ const user = userEvent.setup();
108
+
109
+ mockUseAddressHierarchy.mockReturnValue({
110
+ addresses: mockedAddressOptions,
111
+ error: null,
112
+ isLoading: false,
113
+ });
114
+
115
+ renderAddressHierarchy();
116
+
117
+ const searchString = 'nea';
118
+ const separator = ' > ';
119
+ const options: Set<string> = new Set();
120
+
121
+ mockedAddressOptions.forEach((address) => {
122
+ const values = address.split(separator);
123
+ values.forEach((val, index) => {
124
+ if (val.toLowerCase().includes(searchString.toLowerCase())) {
125
+ options.add(values.slice(0, index + 1).join(separator));
126
+ }
127
+ });
128
+ });
129
+
130
+ const addressOptions = [...options];
131
+ addressOptions.forEach(async (address) => {
132
+ const optionElement = screen.getByText(address);
133
+ expect(optionElement).toBeInTheDocument();
134
+ await user.click(optionElement);
135
+ const values = address.split(separator);
136
+ allFields.map(({ name }, index) => {
137
+ expect(setFieldValue).toHaveBeenCalledWith(`address.${name}`, values?.[index]);
138
+ });
139
+ });
140
+ });
141
+ });
@@ -0,0 +1,32 @@
1
+ import React from 'react';
2
+ import classNames from 'classnames';
3
+ import { Field } from 'formik';
4
+ import { useTranslation } from 'react-i18next';
5
+ import { Input } from '../../input/basic-input/input/input.component';
6
+ import styles from '../field.scss';
7
+ import { type FieldDefinition } from '../../../config-schema';
8
+
9
+ export interface AddressFieldProps {
10
+ fieldDefinition: FieldDefinition;
11
+ }
12
+
13
+ export const AddressField: React.FC<AddressFieldProps> = ({ fieldDefinition }) => {
14
+ const { t } = useTranslation();
15
+
16
+ return (
17
+ <div className={classNames(styles.customField, styles.halfWidthInDesktopView)}>
18
+ <Field name={fieldDefinition.id}>
19
+ {({ field, form: { touched, errors }, meta }) => {
20
+ return (
21
+ <Input
22
+ id={fieldDefinition.id}
23
+ labelText={t(`${fieldDefinition.label}`, `${fieldDefinition.label}`)}
24
+ required={fieldDefinition?.validation?.required ?? false}
25
+ {...field}
26
+ />
27
+ );
28
+ }}
29
+ </Field>
30
+ </div>
31
+ );
32
+ };
@@ -0,0 +1,98 @@
1
+ import React, { useMemo } from 'react';
2
+ import classNames from 'classnames';
3
+ import { Field, useField } from 'formik';
4
+ import { useTranslation } from 'react-i18next';
5
+ import { InlineNotification, Layer, Select, SelectItem, SelectSkeleton, TextInput } from '@carbon/react';
6
+ import { useConfig } from '@openmrs/esm-framework';
7
+ import { type RegistrationConfig } from '../../../config-schema';
8
+ import { useConceptAnswers } from '../field.resource';
9
+ import styles from '../field.scss';
10
+
11
+ export const CauseOfDeathField: React.FC = () => {
12
+ const { t } = useTranslation();
13
+ const { fieldConfigurations, freeTextFieldConceptUuid } = useConfig<RegistrationConfig>();
14
+ const [deathCause, deathCauseMeta] = useField('deathCause');
15
+
16
+ const conceptUuid = fieldConfigurations?.causeOfDeath?.conceptUuid;
17
+ const required = fieldConfigurations?.causeOfDeath?.required;
18
+
19
+ const {
20
+ data: conceptAnswers,
21
+ isLoading: isLoadingConceptAnswers,
22
+ error: errorLoadingConceptAnswers,
23
+ } = useConceptAnswers(conceptUuid);
24
+
25
+ const answers = useMemo(() => {
26
+ if (!isLoadingConceptAnswers && conceptAnswers) {
27
+ return conceptAnswers.map((answer) => ({ ...answer, label: answer.display }));
28
+ }
29
+ return [];
30
+ }, [conceptAnswers, isLoadingConceptAnswers]);
31
+
32
+ if (isLoadingConceptAnswers) {
33
+ return (
34
+ <div className={classNames(styles.customField, styles.halfWidthInDesktopView)}>
35
+ <h4 className={styles.productiveHeading02Light}>{t('causeOfDeathInputLabel', 'Cause of death')}</h4>
36
+ <SelectSkeleton />
37
+ </div>
38
+ );
39
+ }
40
+
41
+ return (
42
+ <div className={classNames(styles.customField, styles.halfWidthInDesktopView)}>
43
+ <h4 className={styles.productiveHeading02Light}>{t('causeOfDeathInputLabel', 'Cause of death')}</h4>
44
+ {errorLoadingConceptAnswers || !conceptUuid ? (
45
+ <InlineNotification
46
+ hideCloseButton
47
+ kind="error"
48
+ title={t('errorFetchingCodedCausesOfDeath', 'Error fetching coded causes of death')}
49
+ subtitle={t('refreshOrContactAdmin', 'Try refreshing the page or contact your system administrator')}
50
+ />
51
+ ) : (
52
+ <>
53
+ <Field name="deathCause">
54
+ {({ field, form: { touched, errors }, meta }) => {
55
+ return (
56
+ <Layer>
57
+ <Select
58
+ {...field}
59
+ id="deathCause"
60
+ invalid={errors.deathCause && touched.deathCause}
61
+ invalidText={errors.deathCause?.message}
62
+ labelText={t('causeOfDeathInputLabel', 'Cause of Death')}
63
+ name="deathCause"
64
+ required={required}>
65
+ <SelectItem id="empty-default-option" value={null} text={t('selectAnOption', 'Select an option')} />
66
+ {answers.map((answer) => (
67
+ <SelectItem id={answer.uuid} key={answer.uuid} text={answer.label} value={answer.uuid} />
68
+ ))}
69
+ </Select>
70
+ </Layer>
71
+ );
72
+ }}
73
+ </Field>
74
+ {deathCause.value === freeTextFieldConceptUuid && (
75
+ <div className={styles.nonCodedCauseOfDeath}>
76
+ <Field name="nonCodedCauseOfDeath">
77
+ {({ field, form: { touched, errors }, meta }) => {
78
+ return (
79
+ <Layer>
80
+ <TextInput
81
+ {...field}
82
+ id="nonCodedCauseOfDeath"
83
+ invalid={errors?.nonCodedCauseOfDeath && touched.nonCodedCauseOfDeath}
84
+ invalidText={errors?.nonCodedCauseOfDeath?.message}
85
+ labelText={t('nonCodedCauseOfDeath', 'Non-coded cause of death')}
86
+ placeholder={t('enterNonCodedCauseOfDeath', 'Enter non-coded cause of death')}
87
+ />
88
+ </Layer>
89
+ );
90
+ }}
91
+ </Field>
92
+ </div>
93
+ )}
94
+ </>
95
+ )}
96
+ </div>
97
+ );
98
+ };
@@ -0,0 +1,25 @@
1
+ import React from 'react';
2
+ import { useConfig } from '@openmrs/esm-framework';
3
+ import { AddressField } from './address/custom-address-field.component';
4
+ import { ObsField } from './obs/obs-field.component';
5
+ import { PersonAttributeField } from './person-attributes/person-attribute-field.component';
6
+ import { type RegistrationConfig } from '../../config-schema';
7
+
8
+ export interface CustomFieldProps {
9
+ name: string;
10
+ }
11
+
12
+ export function CustomField({ name }: CustomFieldProps) {
13
+ const config = useConfig<RegistrationConfig>();
14
+ const fieldDefinition = config.fieldDefinitions.filter((def) => def.id == name)[0];
15
+
16
+ if (fieldDefinition.type === 'person attribute') {
17
+ return <PersonAttributeField fieldDefinition={fieldDefinition} />;
18
+ } else if (fieldDefinition.type === 'obs') {
19
+ return <ObsField fieldDefinition={fieldDefinition} />;
20
+ } else if (fieldDefinition.type === 'address') {
21
+ return <AddressField fieldDefinition={fieldDefinition} />;
22
+ } else {
23
+ return <div>Error: Unknown field type {fieldDefinition.type}</div>;
24
+ }
25
+ }
@@ -0,0 +1,79 @@
1
+ import React, { useCallback } from 'react';
2
+ import classNames from 'classnames';
3
+ import dayjs from 'dayjs';
4
+ import { Layer, SelectItem, TimePicker, TimePickerSelect } from '@carbon/react';
5
+ import { useTranslation } from 'react-i18next';
6
+ import { useField } from 'formik';
7
+ import { OpenmrsDatePicker } from '@openmrs/esm-framework';
8
+ import { usePatientRegistrationContext } from '../../patient-registration-context';
9
+ import type { FormValues } from '../../patient-registration.types';
10
+ import styles from '../field.scss';
11
+
12
+ export const DateAndTimeOfDeathField: React.FC = () => {
13
+ const { t } = useTranslation();
14
+
15
+ return (
16
+ <div className={classNames(styles.dodField, styles.halfWidthInDesktopView)}>
17
+ <h4 className={styles.productiveHeading02Light}>{t('deathDateInputLabel', 'Date of Death')}</h4>
18
+ <span>
19
+ <DeathDateField />
20
+ <DeathTimeField />
21
+ </span>
22
+ </div>
23
+ );
24
+ };
25
+
26
+ function DeathDateField() {
27
+ const { values, setFieldValue } = usePatientRegistrationContext();
28
+ const [deathDate, deathDateMeta] = useField<keyof FormValues>('deathDate');
29
+ const { t } = useTranslation();
30
+ const today = dayjs().hour(23).minute(59).second(59).toDate();
31
+ const onDateChange = useCallback(
32
+ (selectedDate: Date) => {
33
+ setFieldValue(
34
+ 'deathDate',
35
+ selectedDate ? dayjs(selectedDate).hour(0).minute(0).second(0).millisecond(0).toDate() : undefined,
36
+ );
37
+ },
38
+ [setFieldValue],
39
+ );
40
+
41
+ return (
42
+ <Layer>
43
+ <OpenmrsDatePicker
44
+ {...deathDate}
45
+ id="deathDate"
46
+ invalidText={t(deathDateMeta.error)}
47
+ invalid={!!(deathDateMeta.touched && deathDateMeta.error)}
48
+ isRequired={values.isDead}
49
+ labelText={t('deathDateInputLabel', 'Date of death')}
50
+ maxDate={today}
51
+ onChange={onDateChange}
52
+ />
53
+ </Layer>
54
+ );
55
+ }
56
+
57
+ function DeathTimeField() {
58
+ const { t } = useTranslation();
59
+ const [deathTimeField, deathTimeMeta] = useField<keyof FormValues>('deathTime');
60
+ const [deathTimeFormatField, deathTimeFormatMeta] = useField<keyof FormValues>('deathTimeFormat');
61
+
62
+ return (
63
+ <Layer>
64
+ <TimePicker
65
+ {...deathTimeField}
66
+ id="time-picker"
67
+ labelText={t('timeOfDeathInputLabel', 'Time of death (hh:mm)')}
68
+ className={styles.timeOfDeathField}
69
+ pattern="^(1[0-2]|0?[1-9]):([0-5]?[0-9])$"
70
+ invalid={!!(deathTimeMeta.touched && deathTimeMeta.error)}
71
+ invalidText={t(deathTimeMeta.error)}>
72
+ <TimePickerSelect {...deathTimeFormatField} id="time-format-picker" aria-label={t('timeFormat', 'Time Format')}>
73
+ <SelectItem value="AM" text="AM" />
74
+ <SelectItem value="PM" text="PM" />
75
+ </TimePickerSelect>
76
+ </TimePicker>
77
+ </Layer>
78
+ );
79
+ }
@@ -0,0 +1,167 @@
1
+ import React, { type ChangeEvent, useCallback } from 'react';
2
+ import { ContentSwitcher, Layer, Switch, TextInput } from '@carbon/react';
3
+ import { useTranslation } from 'react-i18next';
4
+ import { useField } from 'formik';
5
+ import { OpenmrsDatePicker, useConfig } from '@openmrs/esm-framework';
6
+ import { usePatientRegistrationContext } from '../../patient-registration-context';
7
+ import { type RegistrationConfig } from '../../../config-schema';
8
+ import styles from '../field.scss';
9
+
10
+ const calcBirthdate = (yearDelta, monthDelta, dateOfBirth) => {
11
+ const { enabled, month, dayOfMonth } = dateOfBirth.useEstimatedDateOfBirth;
12
+ const startDate = new Date();
13
+ const resultMonth = new Date(startDate.getFullYear() - yearDelta, startDate.getMonth() - monthDelta, 1);
14
+ const daysInResultMonth = new Date(resultMonth.getFullYear(), resultMonth.getMonth() + 1, 0).getDate();
15
+ const resultDate = new Date(
16
+ resultMonth.getFullYear(),
17
+ resultMonth.getMonth(),
18
+ Math.min(startDate.getDate(), daysInResultMonth),
19
+ );
20
+ return enabled ? new Date(resultDate.getFullYear(), month, dayOfMonth) : resultDate;
21
+ };
22
+
23
+ export const DobField: React.FC = () => {
24
+ const { t } = useTranslation();
25
+ const {
26
+ fieldConfigurations: { dateOfBirth },
27
+ } = useConfig<RegistrationConfig>();
28
+ const allowEstimatedBirthDate = dateOfBirth?.allowEstimatedDateOfBirth;
29
+ const [{ value: dobUnknown }] = useField('birthdateEstimated');
30
+ const [birthdate, birthdateMeta] = useField('birthdate');
31
+ const [yearsEstimated, yearsEstimateMeta] = useField('yearsEstimated');
32
+ const [monthsEstimated, monthsEstimateMeta] = useField('monthsEstimated');
33
+ const { setFieldValue, setFieldTouched } = usePatientRegistrationContext();
34
+ const today = new Date();
35
+
36
+ const onToggle = useCallback(
37
+ (e: { name?: string | number }) => {
38
+ setFieldValue('birthdateEstimated', e.name === 'unknown');
39
+ setFieldValue('birthdate', '');
40
+ setFieldValue('yearsEstimated', 0);
41
+ setFieldValue('monthsEstimated', '');
42
+ setFieldTouched('birthdateEstimated', true, false);
43
+ },
44
+ [setFieldTouched, setFieldValue],
45
+ );
46
+
47
+ const onDateChange = useCallback(
48
+ (birthdate: Date) => {
49
+ setFieldValue('birthdate', birthdate);
50
+ setFieldTouched('birthdate', true, false);
51
+ },
52
+ [setFieldValue, setFieldTouched],
53
+ );
54
+
55
+ const onEstimatedYearsChange = useCallback(
56
+ (ev: ChangeEvent<HTMLInputElement>) => {
57
+ const years = +ev.target.value;
58
+
59
+ if (!isNaN(years) && years < 140 && years >= 0) {
60
+ setFieldValue('yearsEstimated', years);
61
+ setFieldValue('birthdate', calcBirthdate(years, monthsEstimateMeta.value, dateOfBirth));
62
+ }
63
+ },
64
+ [setFieldValue, dateOfBirth, monthsEstimateMeta.value],
65
+ );
66
+
67
+ const onEstimatedMonthsChange = useCallback(
68
+ (ev: ChangeEvent<HTMLInputElement>) => {
69
+ const months = +ev.target.value;
70
+
71
+ if (!isNaN(months)) {
72
+ setFieldValue('monthsEstimated', months);
73
+ setFieldValue('birthdate', calcBirthdate(yearsEstimateMeta.value, months, dateOfBirth));
74
+ }
75
+ },
76
+ [setFieldValue, dateOfBirth, yearsEstimateMeta.value],
77
+ );
78
+
79
+ const updateBirthdate = useCallback(() => {
80
+ const months = +monthsEstimateMeta.value % 12;
81
+ const years = +yearsEstimateMeta.value + Math.floor(monthsEstimateMeta.value / 12);
82
+ setFieldValue('yearsEstimated', years);
83
+ setFieldValue('monthsEstimated', months > 0 ? months : '');
84
+ setFieldValue('birthdate', calcBirthdate(years, months, dateOfBirth));
85
+ setFieldTouched('yearsEstimated', true, false);
86
+ setFieldTouched('monthsEstimated', true, false);
87
+ setFieldTouched('birthdate', true, false);
88
+ }, [setFieldValue, setFieldTouched, monthsEstimateMeta, yearsEstimateMeta, dateOfBirth]);
89
+
90
+ return (
91
+ <div className={styles.halfWidthInDesktopView}>
92
+ <h4 className={styles.productiveHeading02Light}>{t('birthFieldLabelText', 'Birth')}</h4>
93
+ {(allowEstimatedBirthDate || dobUnknown) && (
94
+ <div className={styles.dobField}>
95
+ <div className={styles.dobContentSwitcherLabel}>
96
+ <span className={styles.label01}>{t('dobToggleLabelText', 'Date of birth known?')}</span>
97
+ </div>
98
+ <ContentSwitcher size="md" onChange={onToggle} selectedIndex={dobUnknown ? 1 : 0}>
99
+ <Switch name="known" text={t('yes', 'Yes')} />
100
+ <Switch name="unknown" text={t('no', 'No')} />
101
+ </ContentSwitcher>
102
+ </div>
103
+ )}
104
+ <Layer>
105
+ {!dobUnknown ? (
106
+ <div className={styles.dobField}>
107
+ <OpenmrsDatePicker
108
+ id="birthdate"
109
+ data-testid="birthdate"
110
+ {...birthdate}
111
+ onChange={onDateChange}
112
+ onBlur={() => setFieldTouched('birthdate', true, false)}
113
+ maxDate={today}
114
+ labelText={t('dateOfBirthLabelText', 'Date of birth')}
115
+ isInvalid={!!(birthdateMeta.touched && birthdateMeta.error)}
116
+ invalidText={t(birthdateMeta.error)}
117
+ value={birthdate.value}
118
+ />
119
+ </div>
120
+ ) : (
121
+ <div className={styles.grid}>
122
+ <div className={styles.dobField}>
123
+ <TextInput
124
+ id="yearsEstimated"
125
+ type="number"
126
+ name={yearsEstimated.name}
127
+ onChange={onEstimatedYearsChange}
128
+ labelText={t('estimatedAgeInYearsLabelText', 'Estimated age in years')}
129
+ invalid={!!(yearsEstimateMeta.touched && yearsEstimateMeta.error)}
130
+ invalidText={yearsEstimateMeta.error && t(yearsEstimateMeta.error)}
131
+ value={yearsEstimated.value}
132
+ min={0}
133
+ required
134
+ {...yearsEstimated}
135
+ onBlur={(e) => {
136
+ yearsEstimated.onBlur(e);
137
+ setFieldTouched('yearsEstimated', true, false);
138
+ updateBirthdate();
139
+ }}
140
+ />
141
+ </div>
142
+ <div className={styles.dobField}>
143
+ <TextInput
144
+ id="monthsEstimated"
145
+ type="number"
146
+ name={monthsEstimated.name}
147
+ onChange={onEstimatedMonthsChange}
148
+ labelText={t('estimatedAgeInMonthsLabelText', 'Estimated age in months')}
149
+ invalid={!!(monthsEstimateMeta.touched && monthsEstimateMeta.error)}
150
+ invalidText={monthsEstimateMeta.error && t(monthsEstimateMeta.error)}
151
+ value={monthsEstimated.value}
152
+ min={0}
153
+ {...monthsEstimated}
154
+ required={!yearsEstimateMeta.value}
155
+ onBlur={(e) => {
156
+ monthsEstimated.onBlur(e);
157
+ setFieldTouched('monthsEstimated', true, false);
158
+ updateBirthdate();
159
+ }}
160
+ />
161
+ </div>
162
+ </div>
163
+ )}
164
+ </Layer>
165
+ </div>
166
+ );
167
+ };
@@ -0,0 +1,90 @@
1
+ import React from 'react';
2
+ import { Formik, Form } from 'formik';
3
+ import { render, screen } from '@testing-library/react';
4
+ import userEvent from '@testing-library/user-event';
5
+ import { getDefaultsFromConfigSchema, useConfig } from '@openmrs/esm-framework';
6
+ import { type RegistrationConfig, esmPatientRegistrationSchema } from '../../../config-schema';
7
+ import { PatientRegistrationContextProvider } from '../../patient-registration-context';
8
+ import { initialFormValues } from '../../patient-registration.component';
9
+ import { DobField } from './dob.component';
10
+
11
+ const mockUseConfig = jest.mocked(useConfig<RegistrationConfig>);
12
+
13
+ describe('Dob', () => {
14
+ beforeEach(() => {
15
+ mockUseConfig.mockReturnValue({
16
+ ...getDefaultsFromConfigSchema(esmPatientRegistrationSchema),
17
+ fieldConfigurations: {
18
+ dateOfBirth: {
19
+ allowEstimatedDateOfBirth: true,
20
+ useEstimatedDateOfBirth: { enabled: true, dayOfMonth: 0, month: 0 },
21
+ },
22
+ } as RegistrationConfig['fieldConfigurations'],
23
+ });
24
+ });
25
+
26
+ it('renders the fields in the birth section of the registration form', async () => {
27
+ render(
28
+ <Formik initialValues={{ birthdate: '' }} onSubmit={() => {}}>
29
+ <Form>
30
+ <PatientRegistrationContextProvider
31
+ value={{
32
+ identifierTypes: [],
33
+ values: initialFormValues,
34
+ validationSchema: null,
35
+ inEditMode: false,
36
+ setFieldValue: () => {},
37
+ setCapturePhotoProps: (value) => {},
38
+ setFieldTouched: () => {},
39
+ currentPhoto: '',
40
+ isOffline: false,
41
+ initialFormValues: initialFormValues,
42
+ }}>
43
+ <DobField />
44
+ </PatientRegistrationContextProvider>
45
+ </Form>
46
+ </Formik>,
47
+ );
48
+
49
+ expect(screen.getByRole('heading', { name: /birth/i })).toBeInTheDocument();
50
+ expect(screen.getByText(/date of birth known?/i)).toBeInTheDocument();
51
+ expect(screen.getByRole('tab', { name: /no/i })).toBeInTheDocument();
52
+ expect(screen.getByRole('tab', { name: /yes/i })).toBeInTheDocument();
53
+ expect(screen.getByRole('tab', { name: /yes/i })).toHaveAttribute('aria-selected', 'true');
54
+ expect(screen.getByRole('tab', { name: /no/i })).toHaveAttribute('aria-selected', 'false');
55
+ expect(screen.getByLabelText(/date of birth/i)).toBeInTheDocument();
56
+ });
57
+
58
+ it('typing in the date picker input sets the date of birth', async () => {
59
+ const user = userEvent.setup();
60
+
61
+ render(
62
+ <Formik initialValues={{ birthdate: '' }} onSubmit={() => {}}>
63
+ <Form>
64
+ <PatientRegistrationContextProvider
65
+ value={{
66
+ identifierTypes: [],
67
+ values: initialFormValues,
68
+ validationSchema: null,
69
+ inEditMode: false,
70
+ setFieldValue: () => {},
71
+ setCapturePhotoProps: (value) => {},
72
+ currentPhoto: '',
73
+ isOffline: false,
74
+ initialFormValues: initialFormValues,
75
+ setFieldTouched: () => {},
76
+ }}>
77
+ <DobField />
78
+ </PatientRegistrationContextProvider>
79
+ </Form>
80
+ </Formik>,
81
+ );
82
+
83
+ const dateOfBirthInput = screen.getByLabelText(/date of birth/i);
84
+ expect(dateOfBirthInput).toBeInTheDocument();
85
+ await user.clear(dateOfBirthInput);
86
+ await user.type(dateOfBirthInput, '10/10/2022');
87
+ // FIXME: Make the date input work
88
+ // expect(dateOfBirthInput).toHaveValue('10/10/2022');
89
+ });
90
+ });