Skip to content

Commit

Permalink
fix disappearence of config data on page reload (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
kajambiya authored Jan 10, 2024
1 parent c186fc7 commit 881309c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const question: OHRIFormField = {
concept: '160540AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
datasource: {
name: 'location_datasource',
config: {
tag: 'test-tag',
},
},
},
value: null,
Expand Down Expand Up @@ -162,4 +165,27 @@ describe('UISelectExtended Component', () => {
expect(screen.queryByText('Muyenga')).not.toBeInTheDocument();
});
});
it('Should set the correct value for the config parameter', async () => {
// Mock the data source fetch behavior
const expectedConfigValue = {
tag: 'test-tag',
};

// Mock the getRegisteredDataSource function
jest.mock('../../../registry/registry', () => ({
getRegisteredDataSource: jest.fn().mockResolvedValue({
fetchData: jest.fn().mockResolvedValue([]),
toUuidAndDisplay: (data) => data,
config: expectedConfigValue,
}),
}));

await act(async () => {
await renderForm({});
});
const config = question.questionOptions.datasource.config;

// Assert that the config is set with the expected configuration value
expect(config).toEqual(expectedConfigValue);
});
});
24 changes: 12 additions & 12 deletions src/components/inputs/ui-select-extended/ui-select-extended.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handler, onC
? question.questionOptions.datasource?.config
: getControlTemplate(question.questionOptions.rendering)?.datasource?.config,
);
getRegisteredDataSource(datasourceName ? datasourceName : question.questionOptions.rendering).then(ds =>
getRegisteredDataSource(datasourceName ? datasourceName : question.questionOptions.rendering).then((ds) =>
setDataSource(ds),
);
}, [question.questionOptions?.datasource]);
Expand All @@ -50,15 +50,15 @@ const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handler, onC
}
}, [question['submission']]);

const handleChange = value => {
const handleChange = (value) => {
setFieldValue(question.id, value);
onChange(question.id, value, setErrors, setWarnings);
question.value = handler?.handleFieldSubmission(question, value, encounterContext);
};

const debouncedSearch = debounce((searchterm, dataSource) => {
setIsLoading(true);
dataSource.fetchData(searchterm, config).then(dataItems => {
dataSource.fetchData(searchterm, config).then((dataItems) => {
setItems(dataItems.map(dataSource.toUuidAndDisplay));
setIsLoading(false);
});
Expand All @@ -68,21 +68,21 @@ const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handler, onC
// If not searchable, preload the items
if (dataSource && !isTrue(question.questionOptions.isSearchable)) {
setIsLoading(true);
dataSource.fetchData(null, config).then(dataItems => {
dataSource.fetchData(null, config).then((dataItems) => {
setItems(dataItems.map(dataSource.toUuidAndDisplay));
setIsLoading(false);
});
}
}, [dataSource]);
}, [dataSource, config]);

useEffect(() => {
if (dataSource && isTrue(question.questionOptions.isSearchable) && !isEmpty(searchTerm)) {
debouncedSearch(searchTerm, dataSource);
}
}, [dataSource, searchTerm]);
}, [dataSource, searchTerm, config]);

useEffect(() => {
getConceptNameAndUUID(question.questionOptions.concept).then(conceptTooltip => {
getConceptNameAndUUID(question.questionOptions.concept).then((conceptTooltip) => {
setConceptName(conceptTooltip);
});
}, [conceptName]);
Expand All @@ -102,7 +102,7 @@ const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handler, onC
label={question.label}
value={
field.value
? handler?.getDisplayValue(question, items.find(item => item.uuid == field.value)?.display)
? handler?.getDisplayValue(question, items.find((item) => item.uuid == field.value)?.display)
: field.value
}
conceptName={conceptName}
Expand All @@ -123,8 +123,8 @@ const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handler, onC
id={question.id}
titleText={question.label}
items={items}
itemToString={item => item?.display}
selectedItem={items.find(item => item.uuid == field.value)}
itemToString={(item) => item?.display}
selectedItem={items.find((item) => item.uuid == field.value)}
shouldFilterItem={({ item, inputValue }) => {
if (!inputValue) {
// Carbon's initial call at component mount
Expand All @@ -138,7 +138,7 @@ const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handler, onC
}}
disabled={question.disabled}
readOnly={question.readonly}
onInputChange={value => {
onInputChange={(value) => {
if (isProcessingSelection.current) {
// Notes:
// When the user selects a value, both the onChange and onInputChange functions are invoked sequentially.
Expand All @@ -157,7 +157,7 @@ const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handler, onC
<div>
<PreviousValueReview
value={previousValueForReview.value}
displayText={items.find(item => item.uuid == previousValueForReview.value)?.display}
displayText={items.find((item) => item.uuid == previousValueForReview.value)?.display}
setValue={handleChange}
/>
</div>
Expand Down

0 comments on commit 881309c

Please sign in to comment.