Skip to content

Commit 6776aac

Browse files
committed
code review
1 parent 777dea6 commit 6776aac

File tree

13 files changed

+141
-103
lines changed

13 files changed

+141
-103
lines changed

src/components/inputs/ui-select-extended/ui-select-extended.component.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,6 @@ const UiSelectExtended: React.FC<FormFieldInputProps> = ({ field, errors, warnin
169169
itemToString={(item) => item?.display}
170170
selectedItem={selectedItem}
171171
placeholder={isSearchable ? t('search', 'Search') + '...' : null}
172-
<<<<<<< HEAD
173-
=======
174-
shouldFilterItem={({ item, inputValue }) => {
175-
if (!inputValue || items.find((item) => item.uuid == field.value)) {
176-
// Carbon's initial call at component mount
177-
return true;
178-
}
179-
return item.display?.toLowerCase().includes(inputValue.toLowerCase());
180-
}}
181-
>>>>>>> 5510d0b ((feat) O3-3367 Add support for person attributes)
182172
onChange={({ selectedItem }) => {
183173
isProcessingSelection.current = true;
184174
setFieldValue(selectedItem?.uuid);

src/hooks/useFormJson.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,21 @@ async function refineFormJson(
123123
schemaTransformers: FormSchemaTransformer[] = [],
124124
formSessionIntent?: string,
125125
): Promise<FormSchema> {
126-
removeInlineSubForms(formJson, formSessionIntent);
127-
// apply form schema transformers
128-
for (let transformer of schemaTransformers) {
129-
const draftForm = await transformer.transform(formJson);
130-
formJson = draftForm;
131-
}
132-
setEncounterType(formJson);
133-
return applyFormIntent(formSessionIntent, formJson);
126+
await removeInlineSubForms(formJson, formSessionIntent);
127+
const transformedFormJson = await schemaTransformers.reduce(async (form, transformer) => {
128+
const currentForm = await form;
129+
if (isPromise(transformer.transform(currentForm))) {
130+
return transformer.transform(currentForm);
131+
} else {
132+
return transformer.transform(currentForm);
133+
}
134+
}, Promise.resolve(formJson));
135+
setEncounterType(transformedFormJson);
136+
return applyFormIntent(formSessionIntent, transformedFormJson);
137+
}
138+
139+
function isPromise(value: any): value is Promise<any> {
140+
return value && typeof value.then === 'function';
134141
}
135142

136143
/**

src/hooks/usePersonAttributes.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { openmrsFetch, type PersonAttribute, restBaseUrl } from '@openmrs/esm-framework';
22
import { useEffect, useState } from 'react';
3+
import { type FormSchema } from '../types';
34

4-
export const usePersonAttributes = (patientUuid: string) => {
5+
export const usePersonAttributes = (patientUuid: string, formJson: FormSchema) => {
56
const [personAttributes, setPersonAttributes] = useState<Array<PersonAttribute>>([]);
67
const [isLoading, setIsLoading] = useState(true);
78
const [error, setError] = useState(null);
89

910
useEffect(() => {
10-
if (patientUuid) {
11+
if (formJson.meta?.personAttributes?.hasPersonAttributeFields && patientUuid) {
1112
openmrsFetch(`${restBaseUrl}/patient/${patientUuid}?v=custom:(attributes)`)
1213
.then((response) => {
13-
setPersonAttributes(response?.data?.attributes);
14+
setPersonAttributes(response.data?.attributes);
1415
setIsLoading(false);
1516
})
1617
.catch((error) => {

src/processors/encounter/encounter-form-processor.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ function useCustomHooks(context: Partial<FormProcessorContextProps>) {
3939
const [isLoading, setIsLoading] = useState(true);
4040
const { encounter, isLoading: isLoadingEncounter } = useEncounter(context.formJson);
4141
const { encounterRole, isLoading: isLoadingEncounterRole } = useEncounterRole();
42-
const { isLoading: isLoadingPatientPrograms, patientPrograms } = usePatientPrograms(
42+
const { isLoadingPatientPrograms, patientPrograms } = usePatientPrograms(context.patient?.id, context.formJson);
43+
const { isLoading: isLoadingPersonAttributes, personAttributes } = usePersonAttributes(
4344
context.patient?.id,
4445
context.formJson,
4546
);
46-
const { isLoading: isLoadingPersonAttributes, personAttributes } = usePersonAttributes(context.patient?.id);
4747

4848
useEffect(() => {
4949
setIsLoading(isLoadingPatientPrograms || isLoadingEncounter || isLoadingEncounterRole || isLoadingPersonAttributes);
@@ -170,9 +170,9 @@ export class EncounterFormProcessor extends FormProcessor {
170170

171171
// save person attributes
172172
try {
173-
const personattributes = preparePersonAttributes(context.formFields, context.location?.uuid);
174-
const savedPrograms = await savePersonAttributes(context.patient, personattributes);
175-
if (savedPrograms?.length) {
173+
const personAttributes = preparePersonAttributes(context.formFields, context.location?.uuid);
174+
const savedAttributes = await savePersonAttributes(context.patient, personAttributes);
175+
if (savedAttributes?.length) {
176176
showSnackbar({
177177
title: translateFn('personAttributesSaved', 'Person attribute(s) saved successfully'),
178178
kind: 'success',
@@ -181,12 +181,12 @@ export class EncounterFormProcessor extends FormProcessor {
181181
}
182182
} catch (error) {
183183
const errorMessages = extractErrorMessagesFromResponse(error);
184-
return Promise.reject({
184+
throw {
185185
title: translateFn('errorSavingPersonAttributes', 'Error saving person attributes'),
186186
description: errorMessages.join(', '),
187187
kind: 'error',
188188
critical: true,
189-
});
189+
};
190190
}
191191

192192
// save encounter

src/processors/encounter/encounter-processor-helper.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,7 @@ export function saveAttachments(fields: FormField[], encounter: OpenmrsEncounter
155155
}
156156

157157
export function savePersonAttributes(patient: fhir.Patient, attributes: PersonAttribute[]) {
158-
return attributes.map((personAttribute) => {
159-
return savePersonAttribute(personAttribute, patient.id);
160-
});
158+
return attributes.map((personAttribute) => savePersonAttribute(personAttribute, patient.id));
161159
}
162160

163161
export function getMutableSessionProps(context: FormContextProps) {

src/registry/inbuilt-components/control-templates.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ export const controlTemplates: Array<ControlTemplate> = [
5050
},
5151
},
5252
},
53-
{
54-
name: 'person-attribute-location',
55-
datasource: {
56-
name: 'person_attribute_location_datasource',
57-
},
58-
},
5953
];
6054

6155
export const getControlTemplate = (name: string) => {

src/registry/inbuilt-components/inbuiltDataSources.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { LocationDataSource } from '../../datasources/location-data-source';
55
import { ProviderDataSource } from '../../datasources/provider-datasource';
66
import { SelectConceptAnswersDatasource } from '../../datasources/select-concept-answers-datasource';
77
import { EncounterRoleDataSource } from '../../datasources/encounter-role-datasource';
8-
import { PersonAttributeLocationDataSource } from '../../datasources/person-attribute-datasource';
98

109
/**
1110
* @internal
@@ -36,8 +35,8 @@ export const inbuiltDataSources: Array<RegistryItem<DataSource<any>>> = [
3635
component: new EncounterRoleDataSource(),
3736
},
3837
{
39-
name: 'person_attribute_location_datasource',
40-
component: new PersonAttributeLocationDataSource(),
38+
name: 'person-attribute-location',
39+
component: new LocationDataSource(),
4140
},
4241
];
4342

src/registry/inbuilt-components/inbuiltTransformers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PersonAttributesTransformer } from '../../transformers/person-attributes-transformer';
12
import { DefaultFormSchemaTransformer } from '../../transformers/default-schema-transformer';
23
import { type FormSchemaTransformer } from '../../types';
34
import { type RegistryItem } from '../registry';
@@ -7,4 +8,8 @@ export const inbuiltFormTransformers: Array<RegistryItem<FormSchemaTransformer>>
78
name: 'DefaultFormSchemaTransformer',
89
component: DefaultFormSchemaTransformer,
910
},
11+
{
12+
name: 'PersonAttributesTransformer',
13+
component: PersonAttributesTransformer,
14+
},
1015
];

src/registry/inbuilt-components/template-component-map.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,4 @@ export const templateToComponentMap = [
2525
name: 'encounter-role',
2626
baseControlComponent: UiSelectExtended,
2727
},
28-
{
29-
name: 'person_attribute_location_datasource',
30-
baseControlComponent: UiSelectExtended,
31-
},
3228
];

src/registry/registry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,10 @@ export async function getRegisteredFieldValueAdapter(type: string): Promise<Form
171171
}
172172

173173
export async function getRegisteredFormSchemaTransformers(): Promise<FormSchemaTransformer[]> {
174-
const transformers: FormSchemaTransformer[] = [];
174+
const transformers: Array<FormSchemaTransformer> = [];
175175

176176
const cachedTransformers = registryCache.formSchemaTransformers;
177+
177178
if (Object.keys(cachedTransformers).length) {
178179
return Object.values(cachedTransformers);
179180
}

0 commit comments

Comments
 (0)