Skip to content

Commit e967865

Browse files
committed
code review
1 parent 5510d0b commit e967865

File tree

13 files changed

+132
-89
lines changed

13 files changed

+132
-89
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ const UiSelectExtended: React.FC<FormFieldInputProps> = ({ field, errors, warnin
170170
selectedItem={selectedItem}
171171
placeholder={isSearchable ? t('search', 'Search') + '...' : null}
172172
shouldFilterItem={({ item, inputValue }) => {
173-
if (!inputValue || items.find((item) => item.uuid == field.value)) {
173+
if (!inputValue) {
174174
// Carbon's initial call at component mount
175175
return true;
176176
}

src/hooks/useFormJson.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ async function refineFormJson(
125125
): Promise<FormSchema> {
126126
removeInlineSubForms(formJson, formSessionIntent);
127127
// apply form schema transformers
128-
for (let transformer of schemaTransformers) {
129-
const draftForm = await transformer.transform(formJson);
130-
formJson = draftForm;
131-
}
128+
schemaTransformers.reduce(
129+
async (form, transformer) => Promise.resolve(transformer.transform(await form)),
130+
Promise.resolve(formJson),
131+
);
132132
setEncounterType(formJson);
133133
return applyFormIntent(formSessionIntent, formJson);
134134
}

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: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ function useCustomHooks(context: Partial<FormProcessorContextProps>) {
4343
context.patient?.id,
4444
context.formJson,
4545
);
46-
const { isLoading: isLoadingPersonAttributes, personAttributes } = usePersonAttributes(context.patient?.id);
46+
const { isLoading: isLoadingPersonAttributes, personAttributes } = usePersonAttributes(
47+
context.patient?.id,
48+
context.formJson,
49+
);
4750

4851
useEffect(() => {
4952
setIsLoading(isLoadingPatientPrograms || isLoadingEncounter || isLoadingEncounterRole || isLoadingPersonAttributes);
@@ -170,9 +173,9 @@ export class EncounterFormProcessor extends FormProcessor {
170173

171174
// save person attributes
172175
try {
173-
const personattributes = preparePersonAttributes(context.formFields, context.location?.uuid);
174-
const savedPrograms = await savePersonAttributes(context.patient, personattributes);
175-
if (savedPrograms?.length) {
176+
const personAttributes = preparePersonAttributes(context.formFields, context.location?.uuid);
177+
const savedAttributes = await savePersonAttributes(context.patient, personAttributes);
178+
if (savedAttributes?.length) {
176179
showSnackbar({
177180
title: translateFn('personAttributesSaved', 'Person attribute(s) saved successfully'),
178181
kind: 'success',
@@ -181,12 +184,12 @@ export class EncounterFormProcessor extends FormProcessor {
181184
}
182185
} catch (error) {
183186
const errorMessages = extractErrorMessagesFromResponse(error);
184-
return Promise.reject({
187+
throw {
185188
title: translateFn('errorSavingPersonAttributes', 'Error saving person attributes'),
186189
description: errorMessages.join(', '),
187190
kind: 'error',
188191
critical: true,
189-
});
192+
};
190193
}
191194

192195
// save encounter

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

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

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

162160
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)