Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OHRI-1838 Add support for capturing drugs as observations #121

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export interface OHRIFormQuestionOptions {
};
isDateTime?: { labelTrue: boolean; labelFalse: boolean };
usePreviousValueDisabled?: boolean;
datasource?: { id: string; config?: Record<string, any> };
datasource?: { name: string; config?: Record<string, any> };
isSearchable?: boolean;
}

Expand Down
24 changes: 17 additions & 7 deletions src/components/inputs/ui-select-extended/ui-select-extended.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import { PreviousValueReview } from '../../previous-value-review/previous-value-
import debounce from 'lodash-es/debounce';
import { useTranslation } from 'react-i18next';
import { getRegisteredDataSource } from '../../../registry/registry';
import { getControlTemplate } from '../../../registry/inbuilt-components/inbuiltControls';

export const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handler, onChange }) => {
const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handler, onChange }) => {
const { t } = useTranslation();
const [field, meta] = useField(question.id);
const { setFieldValue, encounterContext, fields } = React.useContext(OHRIFormContext);
Expand All @@ -28,9 +29,18 @@ export const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handl
const [inputValue, setInputValue] = useState('');
const isProcessingSelection = useRef(false);
const [dataSource, setDataSource] = useState(null);
const [config, setConfig] = useState({});

useEffect(() => {
getRegisteredDataSource(question.questionOptions?.datasource?.id).then(ds => setDataSource(ds));
const datasourceName = question.questionOptions?.datasource?.name;
setConfig(
datasourceName
? question.questionOptions.datasource?.config
: getControlTemplate(question.questionOptions.rendering)?.datasource?.config,
);
getRegisteredDataSource(datasourceName ? datasourceName : question.questionOptions.rendering).then(ds =>
setDataSource(ds),
);
}, [question.questionOptions?.datasource]);

useEffect(() => {
Expand All @@ -48,7 +58,7 @@ export const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handl

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

useEffect(() => {
// get the data source
if (dataSource && isTrue(question.questionOptions.isSearchable) && !isEmpty(searchTerm)) {
debouncedSearch(searchTerm, dataSource);
}
Expand Down Expand Up @@ -139,8 +148,7 @@ export const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handl
isProcessingSelection.current = false;
return;
}
setInputValue('');
setFieldValue(question.id, '');
setInputValue(value);
if (question.questionOptions['isSearchable']) {
setSearchTerm(value);
}
Expand All @@ -162,3 +170,5 @@ export const UISelectExtended: React.FC<OHRIFormFieldProps> = ({ question, handl
)
);
};

export default UISelectExtended;
18 changes: 18 additions & 0 deletions src/datasources/concept-data-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { openmrsFetch } from '@openmrs/esm-framework';
import { BaseOpenMRSDataSource } from './data-source';

export class ConceptDataSource extends BaseOpenMRSDataSource {
constructor() {
super('/ws/rest/v1/concept?name=&searchType=fuzzy&v=custom:(uuid,display)');
}

fetchData(searchTerm: string, config?: Record<string, any>): Promise<any[]> {
if (config?.class) {
let urlParts = this.url.split('name=');
this.url = `${urlParts[0]}&name=&class=${config.class}&${urlParts[1]}`;
}
return openmrsFetch(searchTerm ? `${this.url}&q=${searchTerm}` : this.url).then(({ data }) => {
return data.results;
});
}
}
5 changes: 2 additions & 3 deletions src/datasources/location-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { openmrsFetch } from '@openmrs/esm-framework';
import { BaseOpenMRSDataSource } from './data-source';

export class LocationDataSource extends BaseOpenMRSDataSource {
constructor(){
super("/ws/rest/v1/location?v=custom:(uuid,display)");
constructor() {
super('/ws/rest/v1/location?v=custom:(uuid,display)');
}

fetchData(searchTerm: string, config?: Record<string, any>): Promise<any[]> {
Expand All @@ -15,5 +15,4 @@ export class LocationDataSource extends BaseOpenMRSDataSource {
return data.results;
});
}

}
24 changes: 23 additions & 1 deletion src/registry/inbuilt-components/inbuiltControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@ import OHRIDropdown from '../../components/inputs/select/ohri-dropdown.component
import OHRITextArea from '../../components/inputs/text-area/ohri-text-area.component';
import OHRIText from '../../components/inputs/text/ohri-text.component';
import OHRIToggle from '../../components/inputs/toggle/ohri-toggle.component';
import { UISelectExtended } from '../../components/inputs/ui-select-extended/ui-select-extended';
import UISelectExtended from '../../components/inputs/ui-select-extended/ui-select-extended';
import { OHRIRepeat } from '../../components/repeat/ohri-repeat.component';
import { RegistryItem } from '../registry';

/**
* @internal
*/
const controlTemplates = [
{
name: 'drug',
baseControlComponent: UISelectExtended,
datasource: {
name: 'drug_datasource',
config: {
class: '8d490dfc-c2cc-11de-8d13-0010c6dffd0f',
kajambiya marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
];

export const inbuiltControls: Array<RegistryItem<React.ComponentType<OHRIFormFieldProps>>> = [
{
name: 'OHRIText',
Expand Down Expand Up @@ -108,4 +121,13 @@ export const inbuiltControls: Array<RegistryItem<React.ComponentType<OHRIFormFie
component: UISelectExtended,
type: 'ui-select-extended',
},
...controlTemplates.map(template => ({
name: `${template.name}Control`,
component: template.baseControlComponent,
type: template.name.toLowerCase(),
})),
];

export const getControlTemplate = (name: string) => {
return controlTemplates.find(template => template.name === name);
};
11 changes: 10 additions & 1 deletion src/registry/inbuilt-components/inbuiltDataSources.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataSource } from '../../api/types';
import { ConceptDataSource } from '../../datasources/concept-data-source';
import { LocationDataSource } from '../../datasources/location-data-source';
import { RegistryItem } from '../registry';

Expand All @@ -7,7 +8,15 @@ import { RegistryItem } from '../registry';
*/
export const inbuiltDataSources: Array<RegistryItem<DataSource<any>>> = [
{
name: 'concept_location',
name: 'location_datasource',
component: new LocationDataSource(),
},
{
name: 'drug_datasource',
component: new ConceptDataSource(),
},
];

export const validateInbuiltDatasource = (name: string) => {
return inbuiltDataSources.some(datasource => datasource.name === name);
};
18 changes: 13 additions & 5 deletions src/registry/registry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DataSource, FieldValidator, OHRIFormFieldProps, PostSubmissionAction, SubmissionHandler } from '../api/types';
import { getGlobalStore } from '@openmrs/esm-framework';
import { OHRIFormsStore } from '../constants';
import { inbuiltControls } from './inbuilt-components/inbuiltControls';
import { getControlTemplate, inbuiltControls } from './inbuilt-components/inbuiltControls';
import { inbuiltFieldSubmissionHandlers } from './inbuilt-components/inbuiltFieldSubmissionHandlers';
import { inbuiltValidators } from './inbuilt-components/inbuiltValidators';
import { inbuiltDataSources } from './inbuilt-components/inbuiltDataSources';
Expand Down Expand Up @@ -157,10 +157,18 @@ export async function getRegisteredDataSource(name: string): Promise<DataSource<
}
let ds = inbuiltDataSources.find(dataSource => dataSource.name === name)?.component;
if (!ds) {
const dataSourceImport = await getFormsStore()
.dataSources.find(ds => ds.name === name)
?.load?.();
ds = dataSourceImport.default;
const template = getControlTemplate(name);
if (template) {
ds = inbuiltDataSources.find(dataSource => dataSource.name === template.datasource.name)?.component;
} else {
const dataSourceImport = await getFormsStore()
.dataSources.find(ds => ds.name === name)
?.load?.();
if (!dataSourceImport) {
throw new Error('Datasource not found');
}
ds = dataSourceImport.default;
}
}
registryCache.dataSources[name] = ds;
return ds;
Expand Down
Loading